Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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
}

Assign an ID for your dynamically created control -- otherwise no events will be fired.

Are you waiting for the download of full page before pressing the button?

The events are javascript functions hidden by ASP.Net in the page, that maybe not present at the time of your clicking.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top