ASP.NET: Button in user control not posting back

自闭症网瘾萝莉.ら 提交于 2019-12-13 01:10:06

问题


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="&larr;" 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="&rarr;" 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?

EDIT

Ronnie, why are you so dumb??? <input type="submit" /> doesn't use javascript to post a form!


回答1:


The is no Click even on the buttons because they are input type=submit and so do not require javascript to cause the postback.

Do you have any updatepanels in your page at all that could be causing the problem?

Do you have any validators on the page that may be stopping the postback? You might want to set CausesValidation to false if this is the case.

Have you set break points in the Page_Load of your page and control rather than just in the Click event to check that is working.

Have you done anything related to disabling ViewState?



来源:https://stackoverflow.com/questions/2698542/asp-net-button-in-user-control-not-posting-back

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!