When you are filling out a text field in a web form and you hit enter, it is
not the same event as clicking on the submit button, even if there is only
one button on the page.
Example:
You have a simple web form containing a button, a textbox and a label. You
enter text into the textbox and click the submit button, which causes the
label.text control to display what was entered into the textbox. If you
click the submit button, everything is fine. If you just hit 'Enter', while
you are in the textbox control, the form seems to postback, but the
'Search_Click' event doesn't fire. Is there a way to trap this and/or
specify a 'default button'? I remember doing this in win forms in VB6. Is
there a .NET web form equivalent?
<form id="form1" method="post" runat="server">
<asp:TextBox id="search_string" autopostback="False" rows="1"
textmode="singleline" runat="server" />
<asp:Button id="search" text="Submit" OnClick="Search_Click"
runat="server" />
<asp:label id="message" runat="server" />
</form>
Thanks,
Rob
If IsPostBack() Then
message.Text = search_string.Text
End If
--
Shannon Calma, MCSD
Clarity Consulting
http://www.clarityknows.net
"Rob" <ryac...@telsource.com> wrote in message
news:OWBh$CKuBHA.2648@tkmsftngp05...
Thanks. I'm acutally sorting a datagrid, so the postback isn't always a
submission. Very weird that a second control will make it work. I'll have
to try that one, just for curiosity. I'll keep thinking.... Thanks for
your insight.
Rob
"Shannon Calma" <sca...@claritycon.com> wrote in message
news:#onUggKuBHA.2360@tkmsftngp05...
VERY oddly - Shannon's fix of adding another text box does fix the
problem in IE! But I can't have another textbox floating around, and
an "input type=hidden" doesn't seem to have the same effect...
If anyone can find a solution to this annoying bug, please post back.
Nick...
"Rob" <ryac...@telsource.com> wrote in message news:<ufBmPXLuBHA.2280@tkmsftngp05>...
Nick,
When a user presses enter with the cursor inside of a textbox (<input type=text>),
if there is some kind of a button on the form, IE will automatically post the page.
However, the type of postback depends on how many textboxes are present.
If there is only one textbox, IE will post the page without clicking the button.
In terms of postback data, this means that no post data is sent for the button.
On the server, it looks like the button was never clicked, and therefore no
event handler is fired.
If there is more than one textbox, IE chooses the first button on the form and
magically clicks it. Again, in terms of postback data, this means postback
data is sent for the button, something like "button1=button1". On the server
we detect that the button was clicked and fire the event.
In Beta 2 we had a special "default event handler" which fired when we could
not find the IPostBackEventHandler which caused the postback to occur.
In the case of one textbox and one button, this default event handler worked
well. However, overall it caused more confusion than it solved, and it has
been removed. Currently, when we cannot find the IPostBackEventHandler,
we run all Validators on the page, but do not choose a default handler to fire
an event on. In the future we are hoping to add more complete support for a
default button.
There are many possible workarounds, however, not all of will work in all
scenarios. Here are a few simple ones.
- Add another textbox. Use <input type=text style="visibility:hidden"> for an
easy fix. Note also that the hidden textbox will still take up space on the page.
- Use the change event on the textbox to do your processing. Note that this
will not work when the text has not changed on the client and the user presses
enter.
- Use script to trap the keypress and click the button:
<script runat=server language=c#>
public void button1_click (Object s, EventArgs e) {
Response.Write("click");
}
function clickButton() {
if (event.keyCode == 13) {
document.form.button1.click();
return false;
}
}
</script>
<form runat=server id=form >
<input type=text id="textbox1" runat=server onkeypress="return(clickButton());">
<input type=submit id="button1" runat=server OnServerClick=button1_click>
</form>
--
Thanks,
Carl Prothman
Microsoft Developer Support
This posting is provided "AS IS" with no warranties, and confers no rights.