Cannot set a DefaultButton on a usercontrol

后端 未结 3 870
迷失自我
迷失自我 2021-01-26 05:26

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



        
相关标签:
3条回答
  • 2021-01-26 05:59

    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>
    
    0 讨论(0)
  • 2021-01-26 06:02
    this.Page.Form.DefaultButton = this.btnLogin.UniqueID;
    
    0 讨论(0)
  • 2021-01-26 06:21

    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>
    
    0 讨论(0)
提交回复
热议问题