开发者

How can i catch onclick event of Dynamically loaded control?

I have a dropdownlist (on Page) which has OnSelectedIndexChange event thats Loads different Control (ascx) dynamically each time ( with LoadControl Command) - into the page.

Each Control Has a Button(runat=server) and TextBox(runat=server).

When i click on the button - i cant g开发者_Go百科et into the Onclick function .

How can i get into the OnClick Function of the Ascx ?

I know that each SelectedIndexChange its makes postback - so i know i have to save something in the viewstate. but i dont know how to save it and later get the values eneterd on the TexstBox. ( of Each ascx)


You need to add an event handler to the user control, like this:

public event EventHandler ButtonClick;

And in the click event of the button:

protected void Button1_Click(object sender, EventArgs e)
{
    if (this.ButtonClick != null)
        this.ButtonClick(this, e);
}

Then, from the page, you can get the click event like this:

<UC:MyUserControl ID="UserControl1" runat="server" OnButtonClick="UserControl1_ButtonClick" ... />

protected void UserControl1_ButtonClick(object sender, EventArgs e)
{
   //Handle the click event here
}

If you're loading the controls dynamically, then you'll need to make sure the controls are rehydrated after postback, and emulate the code above by assinging the event handler through code:

MyUserControl ctrl = (MyUserControl)this.LoadControl("...");
ctrl.ButtonClick += new EventHandler(UserControl1_ButtonClick);
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜