views:

276

answers:

5

Hello All,

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?

+1  A: 

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

Anton Gogolev
A: 

Hi Anton, Thanks for your quick reply.

I already assigned id's to my usercontrol..

still no use...

Have you steped in /break point on your event. and debuged it?
Syed Tayyab Ali
Post codebehind of your page then.
Anton Gogolev
+2  A: 

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
}
Program.X
A: 

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.

Eduardo Molteni
+2  A: 

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.

Marnix van Valen
thanks, this has solved a problem i've been struggling with!
Dean Madden