Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UserControl events not working for first time

I am calling my user control in a web page dynamically. For the first time when I click the button on user control, the event is not firing. When I click the same for a second time, the events are firing..

Can anyone help me?

like image 487
user98454 Avatar asked Nov 25 '25 06:11

user98454


2 Answers

Sounds like the hookup to the button OnClick event is not occurring on every page load, which is required.

Can you guarantee that the hookup is being performed when you add the control and after a poastback to the page? I always put my event hooks in the Page_Init or Page_Load event handlers and outside of any Postback check. Try putting a breakpoint on the Handler hook up and see if the breakpoint gets "hit" twice.

An event hookup for a button would look similar to:

protected void Page_Load(object sender, EventArgs e)
{
    btnSearch.Click += new EventHandler(btnSearch_Click); // breakpoint on this line
}
like image 140
Program.X Avatar answered Nov 26 '25 23:11

Program.X


The client id of the control is derived from the ID of all containing controls that are marked with the INamingContainer interface. So, make sure that ALL controls in the hierarchy have a fixed ID.

For example if you have a Button control inside a Repeater, the ID of the button and the repeater are concatenated to make the client ID for the button. So both the repeater and the button should have the same ID across postbacks.

You can compare the HTML for the first request with the HTML of the second request to quickly determine if this is the problem.

like image 21
Marnix van Valen Avatar answered Nov 26 '25 23:11

Marnix van Valen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!