最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - ASP.NET: Button in user control not posting back - Stack Overflow

programmeradmin3浏览0评论

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!

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!

Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Apr 23, 2010 at 12:45 Ronnie OverbyRonnie Overby 46.5k74 gold badges272 silver badges350 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

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?

发布评论

评论列表(0)

  1. 暂无评论