ASP.NET: Button in user control not posting back
Posted
by Ronnie Overby
on Stack Overflow
See other posts from Stack Overflow
or by Ronnie Overby
Published on 2010-04-23T12:45:11Z
Indexed on
2010/04/23
13:03 UTC
Read the original article
Hit count: 451
I have a simple user control with this code:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="Pager.ascx.cs" Inherits="Pager" %>
<table style="width: 100%;">
<tr>
<td runat="server" id="PageControls">
<!-- This button has the problem: -->
<asp:Button ID="btnPrevPage" runat="server" Text="←" OnClick="btnPrevPage_Click" />
Page
<asp:DropDownList runat="server" ID="ddlPage" AutoPostBack="true" OnSelectedIndexChanged="ddlPage_SelectedIndexChanged" />
of
<asp:Label ID="lblTotalPages" runat="server" />
<!-- This button has the problem: -->
<asp:Button ID="btnNextPage" runat="server" Text="→" OnClick="btnNextPage_Click" />
</td>
<td align="right" runat="server" id="itemsPerPageControls">
<asp:Literal ID="perPageText1" runat="server" />
<asp:DropDownList ID="ddlItemsPerPage" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlItemsPerPage_SelectedIndexChanged" />
<asp:Literal ID="perPageText2" runat="server" />
</td>
</tr>
</table>
As you can see, the 2 buttons are wired to click events, which are defined correctly in the code-behind.
Now, here is how I include an instance of the control on my page:
<uc:Pager ID="Pager1" runat="server" TotalRecords="100" DisplayItemsPerPage="true"
ItemsPerPageChoices="10,25,50,100" ItemsPerPageFormatString="Sessions/Page: {0}"
PageSize="25" OnPageChanged="PageChanged" OnPageSizeChanged="PageChanged" />
I noticed though, that the 2 buttons in my user control weren't causing a post back when clicked. The drop down list does cause postback, though. Here is the rendered HTML:
<table style="width: 100%;">
<tr>
<td id="ctl00_MainContent_Pager1_PageControls" align="left">
<!-- No onclick event! Why? -->
<input type="submit" name="ctl00$MainContent$Pager1$btnPrevPage" value="?" id="ctl00_MainContent_Pager1_btnPrevPage" />
Page
<select name="ctl00$MainContent$Pager1$ddlPage" onchange="javascript:setTimeout('__doPostBack(\'ctl00$MainContent$Pager1$ddlPage\',\'\')', 0)"
id="ctl00_MainContent_Pager1_ddlPage">
<option value="1">1</option>
<option selected="selected" value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
</select>
of <span id="ctl00_MainContent_Pager1_lblTotalPages">6</span>
<!-- No onclick event! Why? -->
<input type="submit" name="ctl00$MainContent$Pager1$btnNextPage" value="?" id="ctl00_MainContent_Pager1_btnNextPage" />
</td>
<td id="ctl00_MainContent_Pager1_itemsPerPageControls" align="right">
Sessions/Page:
<select name="ctl00$MainContent$Pager1$ddlItemsPerPage" onchange="javascript:setTimeout('__doPostBack(\'ctl00$MainContent$Pager1$ddlItemsPerPage\',\'\')', 0)"
id="ctl00_MainContent_Pager1_ddlItemsPerPage">
<option value="10">10</option>
<option selected="selected" value="25">25</option>
<option value="50">50</option>
<option value="100">100</option>
</select>
</td>
</tr>
</table>
And, as you can see, there is no onclick attribute being rendered in the button's input elements. Why not?
© Stack Overflow or respective owner