Reputation: 47975
Suppose, on Page_Load() of a page (WebForms) that I create this Control :
HtmlGenericControl optionBox = new HtmlGenericControl("div");
optionBox.Attributes["class"] = "class_1";
Than, a use will recall the page using a LinkButton. On the method called from this button, I change the class of my previous div :
protected void cmdCerca_Click(object sender, EventArgs e)
{
...
div.Attributes.Add("class", "class_2");
...
}
Well, if I watch on the rendered result, I'll see that the class of the div have been changed.
This means that, in the next call to this page (from this context, example calling cmdCerca_2_Click
), that object will be recovered from the View, getting class_2
, not class_1
.
But, this doesnt happens if, at the end of cmdCerca_Click
, I call the same page with Response.Redirect()
. Seems that the View will be lost.
Why? And how can I fix it?
Hope the question is clear.
Upvotes: 0
Views: 911
Reputation: 33867
You need to add the controls in the page init event, rather than load, in order to get them into the control tree.
You must be recreating this control on every postback? In this case, your default class will be set every time.
Upvotes: 2