简体   繁体   中英

Button click event not firing within use control in ASP .Net

I am developing an asp web page in which I have a drop down combobox and a place holder below that. When the user selects an item from the drop down combobox, a postback is done to the server side and server loads an asp user control to the place holder in this parent page. Everything upto now is working fine.

In the user control I have a button and the user control code behind is implemented to handle the button click event. The problem is, when I click this button, I can see that the postback is send to the server side (ie parent page Page_Load() is invoked in debug mode), but both the user control's Page_Load() or button click event handler is not invoked.

Please help..

Some additional information,

  1. My parent page is not an asp master page. Just a simple asp page.
  2. I am using VS2008 and .Net 3.5 SP1 and C#.

You need to ensure that you UserControl exists so the button click event is triggered when viewstate is rebuilt.

Loading your UserControl in the Page_Load will work the first time. When you click the button and the post_back occurs, Page_Load has not occurred yet. This means the UserControl will not exist, which mean the button does not exist for the event to be wired back up. So the UserControl with the button in it cannot be connected to the click event and the click event wont fire.

Recommend that your user control is loaded in this event.

protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);
    //-- Create your controls here
}

Try a sandbox test . In page_load, dynamically create a button with a click event in the Page_Load. You will see that the click event does not fire. Now move the button to the OnLoad event. The click event will fire. Also note, the click event will occur before the Page_Load event. Further proof that the button does not exist at the right time.

Another idea...

You are reloading the usercontrol on the page before the button event occurs. Ensure your LoadControl method is inside the If block

if (!IsPostBack)
{
    //load usercontrol
}

Default.aspx

<asp:PlaceHolder runat="server" ID="ph1">
</asp:PlaceHolder>

Default.aspx.cs

 protected void Page_Load(object sender, EventArgs e)
 {
     var ctl = LoadControl("Controls/UserControl.ascx");
     ph1.Controls.Add(ctl);
}

UserControl.ascx

<h3>User control</h3>
<asp:Button ID="btn1" runat="server" OnClick="btn1_Click" Text ="Click me" />

UserControl.ascx.cs

protected void btn1_Click(object s, EventArgs e)
{
    Response.Write("You clicked me, yay");
}

All works like a charm. I see the "You clicked me, yay" written when I click the button

Point of attention . If you try to load the controls dynamically in your example in the handler for SelectedItemChanged event of the dropdown control, it will fail, because of the way that lifecycle works for ASP.Net page. Instead you should handle such control creation in the PageLoad event of the page, like this example below Default.aspx

<asp:DropDownList ID="ddl1" runat="server" AutoPostBack="true">
    <asp:ListItem Value="0" Text="--select a value--" />
    <asp:ListItem Value="1" Text="User control 1" />
    <asp:ListItem Value="2" Text="User control 2" />
</asp:DropDownList>
<asp:PlaceHolder runat="server" ID="ph1">
</asp:PlaceHolder>   

Default.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
    if (IsPostBack)
    {
        switch (ddl1.SelectedValue)
        {
            case "1":
                var ctl = LoadControl("Controls/UserControl.ascx");
                ph1.Controls.Add(ctl);
                break;
            case "2":
                ctl = LoadControl("Controls/UserControl2.ascx");
                ph1.Controls.Add(ctl);
                break;
        } 
    }
}

In my particular case, I found that the problem was the UserControl ID (or rather the lack of).

When the UserControl was first instantiated, my button ID was ctl00$ctl02$btnContinue, but after the postback it had changed to ctl00$ctl03$btnContinue, therefore the button event handler didn't fire.

I instead added my UserControl with a fixed ID and the button now always loads with the ID ctl00$myUserControl$btnContinue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM