ASP.NET enter key and form submit with no javascript

怎甘沉沦 提交于 2019-12-08 07:29:09

问题


I have a form that I want to submit when the user presses the enter key. It works fine in Firefox, but not in IE. It is basically the same issue as this, except that I am not allowed to use any JavaScript: Enter button does not submit form (IE ONLY) ASP.NET

Unfortunately it looks like ASP.NET uses JavaScript to process the button postback. Is there a way around this issue that doesn't rely on JavaScript?


回答1:


The fix for this is what was listed in the bottom of the page of your linked post. IE needs to have an additional form field for some reason for this to work. I have used this in many projects, and it gets around the bug. Just add the following.

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



回答2:


My solution was this, a script that does click the login button.

Worked in all browsers!

public partial class Logon : UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Page.ClientScript.RegisterClientScriptBlock(GetType(), "Logon", "document.getElementById('id_LoginButton').click();", true);
    }
}


来源:https://stackoverflow.com/questions/706625/asp-net-enter-key-and-form-submit-with-no-javascript

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