5

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 (i.e. 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#.
Bathiya Priyadarshana
  • 1,325
  • 6
  • 22
  • 35
  • Some additional information, like some code would help. – JConstantine Sep 21 '11 at 06:39
  • Is AutoEventWireup="true" in the <%@ Page %> directive at the top of the page? If you want to know what AutoEventWireup is refer to this:http://msdn.microsoft.com/en-us/library/59t350k3%28vs.71%29.aspx – Amir Sep 21 '11 at 06:46

3 Answers3

8

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
}
Valamas
  • 24,169
  • 25
  • 107
  • 177
  • Tried this. The problem with this is, the comboxbox SelectedIndexChanged event fires last with this change. Previously I loaded the User Controls within this combobox SelectedIndexChanged event handler since depending on the selected index I need to load different user controls. Now I changed the SelectedIndexChanged to just assign the current index to a public property in the code behind of the parent page and switch this property within the OnLoad() event handler to load the required User Control. But not working since SelectedIndexChanged is firing last. – Bathiya Priyadarshana Sep 21 '11 at 07:17
  • Can you access the combobox.SelectedIndex in OnLoad? If so, load the control. – Valamas Sep 21 '11 at 07:23
  • 1
    Thanks Valamas.. I can access the SelectedIndex within the OnLoad.. and it worked.. :) – Bathiya Priyadarshana Sep 21 '11 at 07:35
3

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;
        } 
    }
}
Trogvar
  • 856
  • 6
  • 17
  • +1 on providing a fine example. Lets hope the OP sees something not right after this. – Valamas Sep 21 '11 at 07:12
  • 1
    This works because you are loading the same user control on each postback. What I need to do is load different user controls depending on the selected index of the combobox. So I am loading the user control within the SelectedIndexChanged event handler of the combobox. – Bathiya Priyadarshana Sep 21 '11 at 07:20
  • I see know. Well, then this is your problem - that you're using the SelectedIndexChanged event. Instead you should do all the loading of dynamic controls during the Page_Load event. – Trogvar Sep 21 '11 at 07:31
  • I provided an extended example – Trogvar Sep 21 '11 at 07:37
  • @Trogvar: When you test, ensure the right UserControl said "Yay, im UC1 and you clicked me" or "Yay, im UC2 and you clicked me" and make sure the right one corresponds to the button clicked. – Valamas Sep 21 '11 at 07:40
  • I tested that, and the right user control corresponds to the click, and the correct message is shown. I didn't go as far as put down all source codes, as the changes are pretty obvious. So I don't quite understand the purpose your comment here – Trogvar Sep 21 '11 at 08:06
0

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.