开发者

UserControl OnClick events never occur

开发者 https://www.devze.com 2023-03-23 03:37 出处:网络
I have a web project in C#/asp.net.I have a UserControl with a GridView that has a Button coded into it.The button has an OnClick event which will result in it a redirect to another page.At this point

I have a web project in C#/asp.net. I have a UserControl with a GridView that has a Button coded into it. The button has an OnClick event which will result in it a redirect to another page. At this point in time, the onclick event never happens no matter how many times I press the button. However, if I hardcode a button into the page that is using the user control (with the event in its code behind), the onclick events work perfectly fine.

Also, when I move t开发者_如何转开发he button outside of the UserControl's GridView, it works fine. However, I am limited by space and need it to be placed in the pager bar along the bottom of the page.

<PagerTemplate>
     <table width="100%" cellpadding="0" cellspacing="0">
        <tr>
           <td align="left" valign="middle">
              <asp:Panel cssClass="PagerPanel" runat="server">
                 <asp:Button ID="FirstButton" runat="server" OnClick="FirstButton_Click" Text="|&lt;"  Enabled="<%# (_gridView.PageIndex > 0) %>" />
                 <asp:Button ID="PrevButton" runat="server" OnClick="PrevButton_Click" Text="&lt;" Enabled="<%# (_gridView.PageIndex > 0) %>" />
                 <%# _gridView.PageIndex+1  %> of <%# _gridView.PageCount %>
                 <asp:Button ID="NextButton" runat="server" OnClick="NextButton_Click" Text="&gt;" Enabled="<%# (_gridView.PageIndex < _gridView.PageCount-1) %>" />
                 <asp:Button ID="LastButton" runat="server" OnClick="LastButton_Click" Text="&gt;|" Enabled="<%# (_gridView.PageIndex < _gridView.PageCount-1) %>" />
              </asp:Panel>
           </td>

           <td>
           <asp:Button ID="Button1" OnClick="AddButton_Click" runat="server"       
              Text="Button" />
           </td>
        </tr>
     </table>
  </PagerTemplate>

UserControl Code Behind:

protected void AddButton_Click(object sender, EventArgs e)
{
    Response.Redirect("thisurl...");
}

Not sure what else I can do to handle this click event. Apparently the GridView is stopping it from firing somehow. Is there anyway that a UserControl's GridView can have a button that will handle onclick events?


The problem is because the events have to be bound to the control before the event handler can be executed, but because your button is dynamically added to the page by the template, it doesn't exist by the time the event is triggered.

There are many ways in which to fix this, but I would look into using EnsureChildControls.

http://msdn.microsoft.com/en-us/library/system.web.ui.control.ensurechildcontrols.aspx


You should add CommandName="Button" attribute to the asp:Button declaration and then wire up the the RowCommand event of the GridView. Then, check for command name (i.e. if (e.CommandName == "Button") in the OnRowCommand event handler.

<asp:Button ID="Button1" CommandName="Button" runat="server" Text="Button" />

...

protected void GridView_RowCommand(object sender, GridViewCommandEventArgs e)
{
  if (e.CommandName == "Button")
  {
    ...
  }
}
0

精彩评论

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