Tuesday, March 13, 2012

Set property in Code-Behind from .aspx page

Hello there,

I have a number of aspx pages that are all the same but with different data in them. These pages are created by a content management system, and also retrieve some Database content based on a unique SQL statement.
Here are two .asp example pages so you can see what I am getting at:
http://www.cntraveller.co.uk/guides/England/
http://www.cntraveller.co.uk/guides/USA/

There is going to be one .dll file for all of these pages but I need to alter the SQL statment depending on which page you are viewing, for example for the Engand page it would be something like:
"SELECT * from Counties where Country = 'GB'" and the USA;
"SELECT * from Counties where Country = 'USA'".

My problem is I want to set somewhere in the .aspx page a variable that can be used in the SELECT statment. I have tried defining a function in the .aspx page like:


<script language="C#" runat="server">
void SetLBCountry()
{
this.strLBCountry = "GB";
}
</script>

and calling it from my .dll like this:


public strLBCountry;
private void Page_Load(object sender, System.EventArgs e)
{
SetLBCountry();
}

but it does not recognise the function when I compile it.

Anyone have any idea as to what I can do?

Thanks in advance,
Dave Asbury.You're going the wrong way, essentially you want the base class (codebehind) to call a sub only defined in a derived class (aspx page). It doesn't work that way. Why can't you just do this from the codebehind class?
Can you not simply parse the Request.Url to see what contry they are viewing from and then use some sort of select statement to get the relevant query string.. This could all be done in teh Page_Load method of the .dll
Thanks for your reply,

This is a good idea, but editors can create new folders and pages using the content management system and I don't want to have to edit the .dll to add in a new "If Request.Url = "blah blah" then strSQL = "SELECT BLAH" type statement.

I can see that there probably going to be other situations that require a similar solution too that I haven't come accross yet.

What I really would like is to be able to set a number of parameters in the .aspx page (this is the page that is created in the content management system from a template) that can be passed into the .dll - just like a form element or a querystring BUT that is set in the page the first time you view it (if you see what I mean!).

Any ideas??
Thanks for your reply,

I need to do it this way as I have my codebehind base class all set up and running fine, but I need all the different countries to use this one .dll but they all need different SQL statements.

The .aspx files are created by editorial staff using a content management system that uses a template and merges this with the data they put in to create the .aspx file.

What I want is for one of the fields they enter to be the key that is used to retreive further data from the database at run time - exactly as if I passed in a querystring or a form element - BUT I want this parameter to be in the page all the time so that when the page is viewed it is picked up and used inside the .dll file.

Do you see what I mean?
Still not sure if I get you fully, but could you an asp:textbox control to the page, you could set its text field to the value that you want and then set it Visiblity to false so that it doesn't show up on the page? you could then get the value of the textbox in the code behind page.
Yes!

That is exactly what I want, now you point it out I feel stupid for not seeing it.
Thanks for the help and sorry for taking up your time.

Dave.

0 comments:

Post a Comment