Need Enter key to trigger post

  • Thread starter Thread starter Mark B
  • Start date Start date
M

Mark B

I have a function that looks up a SQL table to see if a search term matches.
It works fine but so far there are two things yet to work:

1) After entering a search term and pressing Enter, nothing has been
happening. It has only been searching when I click the Search button.

2) Neither of the Response.Write's have been displaying any text.

Here's the code:

<body>
<form id="form1" runat="server">
<div>

<asp:TextBox ID="SearchBox" runat="server" Width="168px"
AutoPostBack="True"
TabIndex="1"></asp:TextBox>
&nbsp;<asp:Button ID="Button1" runat="server" Text="Search" TabIndex="2" />

</div>
</form>
</body>


Protected Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.Click

Response.Write("Test")
If fVerifySearch(Me.SearchBox.Text) = False Then
Response.Write("No record found.")
End If

End Sub
 
you specified autopostback on the textfield, so hitting return only fires the
change event.

-- bruce (sqlwork.com)
 
Hi Mark,

As for the first question, you can add some javascript for the
TextBox(input field)'s "onkeydown" event and post the form. Here are some
web articles mentioned this:

#Enter Key in ASP.NET - Complete Research
http://www.beansoftware.com/ASP.NET-Tutorials/Accept-Enter-Key.aspx

#Default Button Submissions in ASP.NET Pages
http://www.west-wind.com/WebLog/posts/1225.aspx

#Setting the default Button for a TextBox in ASP.NET
http://weblogs.asp.net/rajbk/archive/2003/12/11/setting-the-default-button-f
or-a-textbox-in-asp-net.aspx

For the second question, "Response.Write" will always output the parameter
from the begining of the response stream, therefore, your output will
generally display at the top of the page. Also, if you want to display some
message on page to users, I suggest you use the following means instead of
Response.Write:


You can put a Label control on the page. And then in your page's codebehind
use label.Text = "xxxx" to display the information. The advantage of this
is that the text content can be embeded correctly inside the HTML body/form
instead of at the begining of response stream.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.




--------------------
 
I just took AutoPostBack out. It didn't make a difference.

Also any idea why the response.write's haven't worked?

The entire code is below if you want to test it.
 
When I used to use FrontPage (a few weeks ago before downloading Visual Web
Developer 2008 Express), all I did was insert a button on a webpage and it
defaulted to be a submit button as well as automatically created a form. So
from there whenever I pressed Enter on the webpage it automatically
submitted.

Now with VWDExpress 2008 I tried to do the same thing; that is drag a button
and then a text box onto a blank webpage.

There seems to be no automatic creation of a form and a submit button. I'll
plan have a read through the articles you mentioned and see if they do allow
me to emulate the FrontPage generated functionality.

Thanks.
 
Thanks for your quick reply Mark,

For the front page behavior, it is actually abit related to the
webbrowser's handling of key press when you have focus on a textbox in
browser. When your page have some textbox and submit button on page, and if
you press enter key (when focus on a textbox), the browser will submit the
page/form. this is also true for ASP.NET page, however, ASP.NET button's
"click" event is not a simple form submit, so that won't work if you need
to invoke button's postback event.

Actually, in addition to the javascript approach(I mentioned eariler which
is the most reliable and common means), you can also use the
"defaultButton" feature in ASP.NET page. For example:

#set default button attribute for <form> to the ID of the button control
=================
<form id="form1" runat="server" defaultbutton="Button1" >
<div>

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" />

</div>
</form>
=============

You can also apply default button on a Panel control. e.g.

=============
<asp:panel defaultbutton=¡±btnOK¡± runat=¡±server¡±>
......
</asp:panel>
=============

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.


--------------------
 
Back
Top