me again,
I have a current page containing a usercontrol which lists buildings.
Here is a screenshot: http://i40.tinypic.com/2eusoyt.png
Now, my mentor asked me to build a button which allows the user the make the page show the properties in 2 columns.
How did I try this?
I tried putting the following in my Page_load:
if (ViewState["numberOfColumns"] != null)
{
numberOfColumns= Int32.Parse(ViewState["numberOfColumns"].ToString());
}
else
{
ViewState["numberOfColumns"] = 1;
numberOfColumns= 1;
}
Then behind the button view I put this code:
protected void btnView_Click(object sender, EventArgs e)
{
switch(numberOfColumns)
{
case 1:
numberOfColumns= 2;
ViewState["numberOfColumns"] = numberOfColumns;
break;
case 2:
numberOfColumns= 1;
ViewState["numberOfColumns"] = numberOfColumns;
break;
}
}
But as I guessed this method needs one postback to set the sessionvariable, and another one to execute the pageload with the latest value.
I know there should be “a proper way” of doing this, but I can’t find it.
Any direct you guys could point me would be greatly appreciated.
Thanks in advance, Christophe
Okay,
This is how I did it.
The method building the output of the user control was called directly after the snippet above. It’s called “GetProperties()”.
So the problem was/is that according to the page cycle, .net first executes the Page_Load, and as last the control events.
So what I did was put the call to getProperties() for the first time (when IsPostback = false) in an if. So when you visit the page once, it will load the method, and after that no longer.
So, then I put the methodcall in my button, because when you click the button IsPostback = true.
This worked. Snippets below:
protected void Page_Load(object sender, EventArgs e)
{
//some generic stuff
And behind the button I did the following:
This works like a charm. Altho, I’d still like to know if this