Dynamiclly added control i.e panel contains buttons event no firing
protected void container1ActiveTabChanged(object sender, EventArgs e)
{
if (container1.ActiveTabIndex == 0)
开发者_如何转开发 {
if (container1.Tabs[0].Controls.Count == 0)
{
container1.Tabs[0].Controls.Add(gdvEmployee);
}
}
if (container1.ActiveTabIndex == 1)
{
pnlEmployeeID.Visible = true;
pnlEmployeeInformation.Visible = false;
}
if (container1.ActiveTabIndex == 2)
{
pnlEmployeeInformation.Visible = true;
if (container1.Tabs[2].Controls.Count == 0)
{
container1.Tabs[2].Controls.Add(pnlEmployeeInformation);
}
}
this event not firing
protected void btnInsertClick(object sender, EventArgs e)
Why is my event not firing
I'm assuming you are adding the control which fires the event dynamically.
The most common cause of this issue is that the control that fires the event is dynamically added on every postback.
This causes the previous instance of the control to be replaced. This in turn causes the event to get "lost" as the control which triggered the event is now gone.
When an event is triggered it is "queued" and will execute after a page has been reloaded. Once the page has been reloaded the event is executed.
If you replace/recreate the control on every postback i.e in the page load event, you are destroying the trigger of the event and in turn destroy the event.
Ensure you are only adding the control ones when the page is created the very first time but do not re-add the control when Page.IsPostback is true.
Using dynamic controls requires some knowledge of the page life cycle to prevent confusion and endless hours of debugging.
If you are not adding a control dynamically, please feel free to elaborate where your event is triggered from and how the trigger is implemented. I'm sure we will be able to help you to pinpoint and solve the problem with any additional information you have.
Additional Reading
This explains all about page life cycles and view state.
Understanding ASP.NET View State
I'm just guessing, but you probably add the dynamic controls in the Page_Load stage. This is quite a common mistake. Try doing the same at the Init stage:
protected void Page_Init(object sender, EventArgs e)
{
// Your controls should generate here
}
精彩评论