Canceling the default submit button in ASP.NET

最后都变了- 提交于 2019-11-26 16:49:32

问题


I have an ASP.NET application where there's a few ASP.NET buttons and several plain HTML buttons. Anytime there's a textbox where a user hits enter, the ASP.NET button tries to submit the form.

I know I can change the defaultButton, but I don't want there to be any default button. I just want it so when the user presses enter it doesn't do anything.

I've tried setting defaultButton to blank, but that doesn't seem to work. How do I prevent the form from being submitted by the ASP.NET button when enter is pressed?


回答1:


You can set the button's UseSubmitBehavior = false

btnCategory.UseSubmitBehavior = false;



回答2:


Here is what I used to fix this problem.

<form runat="server" defaultbutton="DoNothing">
        <asp:Button ID="DoNothing" runat="server" Enabled="false" style="display: none;" />



回答3:


I also had this problem with an ImageButton on my MasterPage (the Logout button) being set as the default button (so whenever someone pressed Enter, it would log them out). I solved it by using the following line in the Page_Load event on every child page, which is a bit of a work-around, but it works:

Form.DefaultButton = cmdSubmit.UniqueID;

Hope this helps someone else.




回答4:


<asp:TextBox ID="TXT_Quality" runat="server" Width="257px" 
onkeypress="return key_Pressed(event, this);">
</asp:TextBox>   

function key_Pressed(e, textarea)
 {
    var code = (e.keyCode ? e.keyCode : e.which);
    if(code == 13) 
    { 
       return false;
    }
    return true;
 }


来源:https://stackoverflow.com/questions/1473388/canceling-the-default-submit-button-in-asp-net

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