开发者

click event on a dynamic button inside an update panel on a usercontrol

开发者 https://www.devze.com 2023-03-02 14:22 出处:网络
I have a dynamically created button that is being added to an update panel that rests on a usercontrol. I\'m having an impossible time raising the click event for this button. Basically th开发者_高级运

I have a dynamically created button that is being added to an update panel that rests on a usercontrol. I'm having an impossible time raising the click event for this button. Basically th开发者_高级运维e scenario is as follows (simplified).

I'm generating the dynamic button here:

protected override void OnPreRender(System.EventArgs e)
{
     BuildDynamicControls();
     base.OnPreRender(e);
}

I've also tried building the button here:

protected override void OnInit(System.EventArgs e)
{
     BuildDynamicControls();
     base.OnInit(e);
}

Which calls:

private void BuildDynamicControls();
{
    Button button = new Button();
    button.ID = "test";
    button.click += new EventHandler(dynamicButton_Click);
    updatePanel.ContentTemplateContainer.Controls.Add(button);
}

and the click event:

void dynamicButton_Click
{
     // do stuff. Never hits the break point here.
}


Event handlers in ASP.NET get invoked long before the OnPreRender stage, so your button doesn't "exist" at the time when ASP.NET tries to figure out which button got clicked.

Try building your dynamic controls during the Init phase instead.


If you have a look at the page life cycle you'll find that the events in the page are triggered after the Load even and before the rendering.

So, in your example, the button will be created after the stage which responsible to trigger the events.

To solve that, move the call BuildDynamicControls() to the Load event handler in the page.

From MSDN:

Load: The Page object calls the OnLoad method on the Page object, and then recursively does the same for each child control until the page and all controls are loaded. The Load event of individual controls occurs after the Load event of the page. Use the OnLoad event method to set properties in controls and to establish database connections.

Control events:Use these events to handle specific control events, such as a Button control's Click event or a TextBox control's TextChanged event.

PreRender: Raised after the Page object has created all controls that are required in order to render the page, including child controls of composite controls. (To do this, the Page object calls EnsureChildControlsfor each control and for the page.)


This article describes the way to debug it. It is quite similar.

However, I noticed one issue with your code. Your base.OnInit(e) goes as a first line in overridden methods. Try switching them as below.

    protected override void OnInit(System.EventArgs e)
    {
base.OnInit(e);
         BuildDynamicControls();

    }


My oversimplified example ... was over simplified.

The problem was that each time the dynamic button was being generated it was receiving a brand new ID that differed from the ID of the button posting back.

Thanks all for the help.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号