1
votes

I have a user control on a page. The user control has a public property on it that I need to set after a button click. How do you do this? It appears that the control is rendered before the button click event fires, so setting the property has no effect.

Page: <%@ Register src="Email.ascx" tagname="Email" tagprefix="uc2" %>

protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
        EmailList.IsEditable = false;
}

protected void btn_Click(object sender, EventArgs e)
{
    EmailList.IsEditable = true;
}

User Control: public bool IsEditable { get; set; }

The public property gets set correctly when I set it in the page load event, but not on the button click. The button is used to change the form from read-only to edit mode. Is there a way to set a public property in the button click event? If so, how?

1
What does IsEditable do in the "set" implementation?Sunny

1 Answers

1
votes

The property is getting set just fine. The problem is where in your usercontrol you set it's controls to readonly/enabled.

If you want to be able to affect how the usercontrol gets rendered, you must do the logic where you're setting the readonly/enabled properties of the subcontrols in the Page_PreRender event of the usercontrol. This event is executed after the buttonclick events.

A must read on msdn: the page lifecycle. You'll notice the control events get processed after Page_Load and before Page_LoadComplete.