Cannot set a DefaultButton on a usercontrol

2020-05-02 03:37发布

问题:

I am trying set a button on a user control as defaultbutton on a page but its not working. Here is the code for my user control

<asp:Panel ID="pnlTopicPicker"  runat="server" DefaultButton="btnSubmit">
    <div class="PadBottom">
        <div id="divTopic" class="FloatLeft PadDiv">
            <div class="SectionHeader FloatLeft PadRightMediumSmall">Topic:</div>
            <asp:DropDownList ID="ddlSelectedTopic" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlSelectedTopic_OnSelectedIndexChanged" >
            </asp:DropDownList>
        </div>
        <div class="FloatLeft PadLeftMediumSmall">
               <asp:Button ID="btnClear" runat="server" Text="Clear" OnClick="btnClear_Click" CssClass="DefaultButton" />
        </div>
        <div class="FloatRight PadRightMediumSmall">
               <asp:Button ID="btnSubmit" runat="server" Text="View Report" OnClick="btnSubmit_Click" OnClientClick="return ViewReportSubmit(event);" CssClass="DefaultButton" />
        </div>
     </div>
</asp:Panel>

I am using the above control : TopicPicker in a page:

<asp:UpdatePanel ID="pnlFind" runat="server" UpdateMode="Conditional">
            <ContentTemplate>
            <div class="PadLeft">
                <Incapnet:TopicPicker ID="incTopicPicker" runat="server" OnClearClick="btnClear_Click" OnSubmitClick="btnSubmit_Click" OnSelectedTopicChanged="ddlSelectedTopic_Changed" />
            </div>
            <div class="ClearBoth" />
            <div class="PadTop PadLeft" >
                <asp:GridView ID="gvFindGrid" runat="server" AutoGenerateColumns="False" OnRowDataBound="gvFindGrid_RowDataBound" 
                         CssClass="ContrastTable UFWideTable"  GridLines="None">
                    <AlternatingRowStyle CssClass="AlternateRow" />
                    <RowStyle CssClass="Row" />
                    <HeaderStyle CssClass="HeaderRow" />
                    <EmptyDataTemplate>
                        No Records Found
                    </EmptyDataTemplate>
                    <Columns>
                    ... 
                    ...
        </ContentTemplate>
    </asp:UpdatePanel>

Now when I hit enter, on the page, I want the "btnSubmit" to be executed, which is not happening.

How do I get this working.

Thanks,

回答1:

under what circumstances are you expecting the btnSubmit Click event to fire?

I ask because the event will only be triggered if the Enter key is pressed when a control which implements the IButtonControl interface has focus.

This would include (among others) TextBox and DropDownList.

If you select your DropDownList and then hit enter it should fire the event. But just clicking any element (such as a DIV) and hitting enter would not.


Edit (Based on comment)

Its a bit of a hack but you can achieve what you ask using the following javascript (uses JQuery):

<script type="text/javascript">
    $(document).keypress(function (e) {
        var code = (e.keyCode ? e.keyCode : e.which);
        if (code == 13) {
            $('#<%= btnSubmit.ClientID %>').click();
        }
    });
</script>


回答2:

Try adding the following to the button:

<asp:Button ID="Button1" runat="sever" UseSubmitBehavior="true" ... >

If that doesn't help, try adding this to your markup (for IE):

<!-- Fix for IE bug submit on pressing "Enter" -->
<div style="display:none">
    <input type="text" name="hiddenText"/>
</div>


回答3:

this.Page.Form.DefaultButton = this.btnLogin.UniqueID;


标签: asp.net