Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

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 (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#.
like image 941
Bathiya Priyadarshana Avatar asked Sep 21 '11 06:09

Bathiya Priyadarshana


People also ask

Why button click is not working in asp net?

Try to go into Design mode in Visual Studio, locate the button and double click the button that should setup the event. Otherwise once the button is selected in Design more, go to the properties and try setting it from there.

How can create button click event dynamically in asp net?

public form1() { foreach (Panel pl in Container. Components) { pl. Click += Panel_Click; } } private void Panel_Click(object sender, EventArgs e) { // Process the panel clicks here int index = Panels. FindIndex(a => a == sender); ... }

How can we avoid button click event from firing when page refresh occurs in asp net?

One way to prevent this from happening is to use Response. Redirect("same_page") to the same page after the event logic. This will force the page to reload and thereafter doing any further page refreshes would not call the button click event.

Why is my click event not working in usercontrol?

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. Try a sandbox test.

Why doesn't the click event fire when I click a button?

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.

How do I get click event to fire in page_load?

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.

How do I know if a button event is not firing?

How do you know the event is not firing? check the CausesValidation property of your button. set it to False if its true. Also check your asp.net page code file link is exactly same in which you have put the event. Is the button inside a template control (such as an <asp:Repeater> or <asp:GridView> )?


2 Answers

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
}
like image 121
Valamas Avatar answered Nov 13 '22 17:11

Valamas


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;
        } 
    }
}
like image 41
Trogvar Avatar answered Nov 13 '22 17:11

Trogvar