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 the 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="|<" Enabled="<%# (_gridView.PageIndex > 0) %>" />
<asp:Button ID="PrevButton" runat="server" OnClick="PrevButton_Click" Text="<" Enabled="<%# (_gridView.PageIndex > 0) %>" />
<%# _gridView.PageIndex+1 %> of <%# _gridView.PageCount %>
<asp:Button ID="NextButton" runat="server" OnClick="NextButton_Click" Text=">" Enabled="<%# (_gridView.PageIndex < _gridView.PageCount-1) %>" />
<asp:Button ID="LastButton" runat="server" OnClick="LastButton_Click" Text=">|" 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?
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.
…