Custom page_load event called by user before button click event in aspx page

I want to assign a property value when a button is clicked in an aspx page and want to pass the value to the usercontrol and bind the data according to the property value. But the problem is, before the button click event is triggered, will the page_load function of the custom control get hit? is this any way to call the page_load of the user control again when the button is clicked or is there any other alternative? Thanks to

+2


source to share


2 answers


Using Page_Load from UserControl makes the control very dependent on the page lifecycle and therefore not very flexible. The best way is to add a public method to the control you call from the OnClick button. This method will then perform data binding.

Kind:



//MyPage.aspx
void Button_OnClick(object sender, EventArgs e)
{
MyUserControl.DataBind(MyTextBox.Text);
}

//MyUserControl.ascx
public void DataBind(string value)
{
UpdateView(value);
}

      

+2


source


It's kludgy, but if you need to, you can always trigger the Page_load event manually after the button has fired.



A better approach would be: depending on what code needs to be run after the button_click event, you can move it to another event handler, such as the OnPreRender method.

+1


source







All Articles