Enter in Textbox reloads page instead of submitting form
I'm sure this is simple and there's plenty of close-but-not-quite posts...
I've got a content page containing a LoginView which has got a Textbox and Button on the AnonymousTemplate. I enter "123" in the Textbox and press enter, the page reloads (I've put a trace in the Page_Load event) and the Textbox still contains "123". If I press my Button instead of pressing enter while in the Textbox, it works as I would expect. I've tried adding
Code:
ClientScript.RegisterHiddenField("__EVENTTARGET", "MyButtonName");
(suggested instead of Page.RegisterHiddenField by my compiler) but that made no difference.
What's going on? How do I get the expected behaviour instead of a page reload?
Thanks for your help!
Re: Enter in Textbox reloads page instead of submitting form
I ran into this kind of problem a little while ago. Here's what I did.
I defined a javascript function to handle the keypress event in the textbox. In the function, I test if it is enter key, and if so, I set __EVENTTARGET to some value and then submit the form.
In my Page_Load in the code behind, I check the value of __EVENTTARGET, and if it is what I set it to, then I call the same function that the submit button called.
my aspx page:
Code:
<script language="javascript">
function SubmitEnter(e)
{
if (e.keyCode ==13)
{
document.forms("Form1").__EVENTTARGET.value = "submit";
document.forms("Form1").submit();
}
}
</script>
... later on...
<asp:textbox id="txtEntry" runat="server" onkeypress="SubmitEnter(event);"></asp:textbox>
In my codebehind...
Code:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
if not (Page.IsPostBack) Then
Call LoadStuff() 'Load drop downs and stuff...
else
If (Page.Request.Params("__EVENTTARGET") = "submit") then
Call CheckInfo() 'This is the function my submit button also calls
end if
end if
End Sub
HTH
Ranthalion