简体   繁体   English

ASP.NET:用户控件中的按钮不回发

[英]ASP.NET: Button in user control not posting back

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. 但是我注意到,用户控件中的2个按钮在单击时并没有引起回发。 The drop down list does cause postback, though. 但是,下拉列表确实会导致回发。 Here is the rendered HTML: 这是呈现的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. 而且,如您所见,按钮的输入元素中没有呈现onclick属性。 Why not? 为什么不?

EDIT 编辑

Ronnie, why are you so dumb??? 罗尼,你为什么这么傻??? <input type="submit" /> doesn't use javascript to post a form! <input type="submit" />不使用javascript发布表单!

The is no Click even on the buttons because they are input type=submit and so do not require javascript to cause the postback. 即使在按钮上也不能单击,因为它们是输入type = submit,因此不需要javascript来引起回发。

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. 在这种情况下,您可能需要将CausesValidation设置为false。

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. 您是否在页面和控件的Page_Load中设置了断点,而不仅仅是在Click事件中设置了断点以检查其是否正常工作。

Have you done anything related to disabling ViewState? 您是否做了与禁用ViewState有关的任何事情?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM