想要< asp:textbox>仅在按下Enter键时回发 [英] Want <asp:textbox> to postback only when Enter key is pressed

查看:63
本文介绍了想要< asp:textbox>仅在按下Enter键时回发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello All,


我有一个带有一个Textbox(SearchTextBox)和一个ImageButton

(SearchButton)服务器控件的ASP.NET页面。用户可以在

SearchTextBox中键入搜索文本,然后单击SearchButton,Web服务器执行数据库

查询并显示结果。所有这一切都运行正常。


我希望用户能够在SearchTextBox中光标仍然是

时按Enter键并拥有SearchButton。点击事件触发(从而执行数据库查询并显示结果的
)。我有

SearchTextBox.AutoPostBack = False因为每次用户输入内容时我都不希望页面重新发布

;我只是想让它查询数据库和

当用户按下Enter键时显示结果。


我考虑过使用AddHandler SearchTextBox。 TextChanged,AddressOf

HandleEnterKey


使用私有子

Private Sub HandleEnterKey()
如果EnterKeyPressed然后

SearchButton_Click(SearchtextBox,e)

结束如果

结束子


但是我我不知道EventArgs传递的参数是什么,e。


有谁知道怎么做到这一点?有没有人有其他想法?


TIA,

-

Joe


VB.NET/C#/ASP.NET/VBA自动化/ VB / C ++ / Web和数据库开发

Hello All,

I have an ASP.NET page with one Textbox (SearchTextBox) and one ImageButton
(SearchButton) server controls. The user can type search text in
SearchTextBox and click SearchButton and the web server performs a database
query and displays the results. All of this works fine.

I want the user to be able to press the Enter key while the cursor is still
in SearchTextBox and have the SearchButton.Click event fire (thus performing
the database query and displaying the results). I have
SearchTextBox.AutoPostBack = False because I don''t want the page to repost
everytime the user types something; I just want it to query the database adn
display teh results when the user presses the Enter key.

I''ve thought about using AddHandler SearchTextBox.TextChanged, AddressOf
HandleEnterKey

using the private sub
Private Sub HandleEnterKey()
If EnterKeyPressed Then
SearchButton_Click(SearchtextBox, e)
End If
End Sub

but I don''t know what EventArgs to pass in for the parameter, e.

Does anyone know how to make this happen? Does anyone have any other ideas?

TIA,
--
Joe

VB.NET/C#/ASP.NET/VBA Automation/VB/C++/Web and DB development

推荐答案

让我们假设你有一个带有id =" TextBox1"的TextBox和一个按钮,带有

id =" btnSearch"

< asp:TextBox ID =" txtSearch"的onclick = QUOT; fnTrapKD( btnSearch);" Runat = server
Let''s assume you have a TextBox with id="TextBox1" and a button with
id="btnSearch"
<asp:TextBox ID="txtSearch" onclick="fnTrapKD(''btnSearch'');" Runat=server
< / asp:TextBox>
< asp:Button ID =" btnSearch" Runat = server Text =" search">< / asp:Button>


然后在您的aspx页面中添加一个Javascript片段,如下所示:


< script language =" javascript">

function fnTrapKD(btnName){

var btn = document.getElementById(btnName);

if(event.keyCode == 13)

{

event.returnValue = false;

event.cancel = true ;

btn.click();

}

}


< / script>


并且正常处理服务器端的btnSearch Onclick事件

搜索功能:

Private Sub btnSearch_Click(ByVal sender As System.Object,ByVal e As

System.EventArgs)处理btnSearch.Click

''实现数据库搜索

End Sub

-

HTH,

Phillip Williams
http://www.societopia.net
http://www.webswapp.com

" Joe"写道:

Hello All,

我有一个带有一个Textbox(SearchTextBox)和一个ImageButton
(SearchButton)服务器控件的ASP.NET页面。用户可以在SearchTextBox中键入搜索文本,然后单击SearchButton,Web服务器将执行数据库查询并显示结果。所有这一切都很好。

我希望用户能够在SearchTextBox中光标仍然是
时按Enter键并激活SearchButton.Click事件(从而执行
我已经考虑过使用AddHandler SearchTextBox.TextChanged,AddressOf
HandleEnterKey

使用私有子
Private Sub HandleEnterKey()
如果EnterKeyPressed那么
SearchButton_Click(SearchtextBox,e)
结束如果
结束Sub

但我不知道EventArgs传递的是参数,e。

有谁知道怎么做到这一点?有没有人有任何其他想法?

TIA,
-
Joe

VB.NET / C#/ ASP.NET / VBA Automation / VB / C ++ / Web和DB开发
</asp:TextBox> <asp:Button ID="btnSearch" Runat=server Text="search"></asp:Button>

then add a Javascript snippet in your aspx page like this:

<script language="javascript">
function fnTrapKD(btnName){
var btn = document.getElementById(btnName);
if (event.keyCode == 13)
{
event.returnValue=false;
event.cancel = true;
btn.click();
}
}

</script>

and handle the btnSearch Onclick event on the server side as normal for your
search function:
Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnSearch.Click
''implement the database search
End Sub
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"Joe" wrote:
Hello All,

I have an ASP.NET page with one Textbox (SearchTextBox) and one ImageButton
(SearchButton) server controls. The user can type search text in
SearchTextBox and click SearchButton and the web server performs a database
query and displays the results. All of this works fine.

I want the user to be able to press the Enter key while the cursor is still
in SearchTextBox and have the SearchButton.Click event fire (thus performing
the database query and displaying the results). I have
SearchTextBox.AutoPostBack = False because I don''t want the page to repost
everytime the user types something; I just want it to query the database adn
display teh results when the user presses the Enter key.

I''ve thought about using AddHandler SearchTextBox.TextChanged, AddressOf
HandleEnterKey

using the private sub
Private Sub HandleEnterKey()
If EnterKeyPressed Then
SearchButton_Click(SearchtextBox, e)
End If
End Sub

but I don''t know what EventArgs to pass in for the parameter, e.

Does anyone know how to make this happen? Does anyone have any other ideas?

TIA,
--
Joe

VB.NET/C#/ASP.NET/VBA Automation/VB/C++/Web and DB development



好吧,我会做的是


只需创建一个独立的函数进行搜索


Seach(字符串TexBoxValue)

{

//代码执行搜索数据库并在此处显示结果

}


从你的Serch Button点击事件中调用这个函数。


SearchButton_Click()

{

Seach(TexBoxName.text)

}


当按Enter键时有一个隐藏的控件[或文本框webcontrol

,大小为0]并设置一些值,比如说ENTERPRESSED; [区分一个

输入密钥并进行搜索]


在页面中_Load fucntion


Page_Load( )

{

if(Request.Forms [" HiddenControlName"]。Value =" ENTERPRESSED")

Seach(TexBoxName.text )

}


HTH


" Joe"写道:
Well, What I would do is

Just create an independed function to do the search

Seach(string TexBoxValue)
{
// code to perform Search on DB and display results here
}

Call this fucntion from your Serch Button click event.

SearchButton_Click ()
{
Seach(TexBoxName.text)
}

When Enter Key is pressed have a hidden control [or a textbox webcontrol
with size 0] and set some value, say "ENTERPRESSED" [to distinguish that an
enter key and do a search]

In the page_Load fucntion

Page_Load()
{
if(Request.Forms["HiddenControlName"].Value = "ENTERPRESSED")
Seach(TexBoxName.text)
}

HTH

"Joe" wrote:
Hello All,

我有一个带有一个Textbox(SearchTextBox)的ASP.NET页面和一个ImageButton
(SearchButton)服务器控件。用户可以在SearchTextBox中键入搜索文本,然后单击SearchButton,Web服务器将执行数据库查询并显示结果。所有这一切都很好。

我希望用户能够在SearchTextBox中光标仍然是
时按Enter键并激活SearchButton.Click事件(从而执行
我已经考虑过使用AddHandler SearchTextBox.TextChanged,AddressOf
HandleEnterKey

使用私有子
Private Sub HandleEnterKey()
如果EnterKeyPressed那么
SearchButton_Click(SearchtextBox,e)
结束如果
结束Sub

但我不知道EventArgs传递的是参数,e。

有谁知道怎么做到这一点?有没有人有任何其他想法?

TIA,
-
Joe

VB.NET / C#/ ASP.NET / VBA Automation / VB / C ++ / Web和DB开发
Hello All,

I have an ASP.NET page with one Textbox (SearchTextBox) and one ImageButton
(SearchButton) server controls. The user can type search text in
SearchTextBox and click SearchButton and the web server performs a database
query and displays the results. All of this works fine.

I want the user to be able to press the Enter key while the cursor is still
in SearchTextBox and have the SearchButton.Click event fire (thus performing
the database query and displaying the results). I have
SearchTextBox.AutoPostBack = False because I don''t want the page to repost
everytime the user types something; I just want it to query the database adn
display teh results when the user presses the Enter key.

I''ve thought about using AddHandler SearchTextBox.TextChanged, AddressOf
HandleEnterKey

using the private sub
Private Sub HandleEnterKey()
If EnterKeyPressed Then
SearchButton_Click(SearchtextBox, e)
End If
End Sub

but I don''t know what EventArgs to pass in for the parameter, e.

Does anyone know how to make this happen? Does anyone have any other ideas?

TIA,
--
Joe

VB.NET/C#/ASP.NET/VBA Automation/VB/C++/Web and DB development



Phillip,


谢谢。我95%在那里。如果填充框,这可以正常工作。如果它没有填充
,则Click事件永远不会触发。这意味着永远不会显示错误

消息。请参阅下面的代码。 lblSearchMsg

显示一条错误消息,要求用户输入一些要搜索的文本。


另外,我尝试使用事件的cancelBubble属性进行类似的操作

对象并没有用。取消和cancelBubble一样吗?


谢谢,


//////////////// /////////////////////////

SearchButton点击事件代码

//// /////////////////////////////////////

***** ****************************************

Private Sub SearchButton_Click(ByVal sender As System.Object,ByVal e As

System.Web.UI.ImageClickEventArgs)_

处理SearchButton.Click


如果Search.Text.Length = 0那么

hdnSearchText.Value = String.Empty

如果hdnSelectedCategory.Value.Length = 0那么

lblSearchMsg.Visible = True

Else

lblSearchMsg.Visible = False

PopulateClaimFormsDataGrid(hdnSortField.Value,

hdnSortOrder.Value)

结束如果

否则

hdnSearchText.Value = Search.Text

lblSearchMsg.Visible = False

PopulateClaimFormsDataGrid(hdnSortField.Value,hdnSortOrder.Value)

结束如果

结束子


**************************** *****************

-

Joe


VBA自动化/ VB / C ++ / Web和DB开发

" Phillip Williams"写道:
Phillip,

Thank you. I''m 95% there. If the box is populated, this works fine. If it
isn''t populated, the Click event never fires. This means that an error
message is never displayed. Please see the code below. lblSearchMsg
displays an error message that asks the user to enter some text to search for.

Also, I tried something similar using the cancelBubble property of the event
object and it didn''t work. Is cancel the same as cancelBubble ?

Thank you,

/////////////////////////////////////////
Code for SearchButton click event
/////////////////////////////////////////
*********************************************
Private Sub SearchButton_Click(ByVal sender As System.Object, ByVal e As
System.Web.UI.ImageClickEventArgs) _
Handles SearchButton.Click

If Search.Text.Length = 0 Then
hdnSearchText.Value = String.Empty
If hdnSelectedCategory.Value.Length = 0 Then
lblSearchMsg.Visible = True
Else
lblSearchMsg.Visible = False
PopulateClaimFormsDataGrid(hdnSortField.Value,
hdnSortOrder.Value)
End If
Else
hdnSearchText.Value = Search.Text
lblSearchMsg.Visible = False
PopulateClaimFormsDataGrid(hdnSortField.Value, hdnSortOrder.Value)
End If
End Sub

*********************************************
--
Joe

VBA Automation/VB/C++/Web and DB development
"Phillip Williams" wrote:
让我们假设你有一个带有id =" TextBox1"的TextBox。和一个按钮,其中
id =" btnSearch"
< asp:TextBox ID =" txtSearch"的onclick = QUOT; fnTrapKD( btnSearch);" Runat = server
Let''s assume you have a TextBox with id="TextBox1" and a button with
id="btnSearch"
<asp:TextBox ID="txtSearch" onclick="fnTrapKD(''btnSearch'');" Runat=server
< / asp:TextBox>
</asp:TextBox>


< asp:Button ID =" btnSearch" Runat = server Text =" search">< / asp:Button>

然后在您的aspx页面中添加一个Javascript代码段,如下所示:

< script language =" javascript">
函数fnTrapKD(btnName){
var btn = document.getElementById(btnName);
if(event.keyCode == 13)
{
event.returnValue = false;
event.cancel = true;
btn.click();
}
}

< /脚本>

并在服务器端处理btnSearch Onclick事件,正常用于
搜索功能:
Private Sub btnSearch_Click(ByVal sender As System.Object,ByVal e As < (> System.EventArgs)处理btnSearch.Click
''实现数据库搜索
End Sub
-
HTH,
Phillip Williams
< a rel =nofollowhref =http://www.societopia.nettarget =_ blank> http://www.societopia.net
http://www.webswapp.co m

" Joe"写道:


<asp:Button ID="btnSearch" Runat=server Text="search"></asp:Button>

then add a Javascript snippet in your aspx page like this:

<script language="javascript">
function fnTrapKD(btnName){
var btn = document.getElementById(btnName);
if (event.keyCode == 13)
{
event.returnValue=false;
event.cancel = true;
btn.click();
}
}

</script>

and handle the btnSearch Onclick event on the server side as normal for your
search function:
Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnSearch.Click
''implement the database search
End Sub
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"Joe" wrote:

Hello All,

我有一个带有一个Textbox(SearchTextBox)的ASP.NET页面和一个ImageButton
(SearchButton)服务器控件。用户可以在SearchTextBox中键入搜索文本,然后单击SearchButton,Web服务器将执行数据库查询并显示结果。所有这一切都很好。

我希望用户能够在SearchTextBox中光标仍然是
时按Enter键并激活SearchButton.Click事件(从而执行
我已经考虑过使用AddHandler SearchTextBox.TextChanged,AddressOf
HandleEnterKey

使用私有子
Private Sub HandleEnterKey()
如果EnterKeyPressed那么
SearchButton_Click(SearchtextBox,e)
结束如果
结束Sub

但我不知道EventArgs传递的是参数,e。

有谁知道怎么做到这一点?有没有人有任何其他想法?

TIA,
-
Joe

VB.NET / C#/ ASP.NET / VBA Automation / VB / C ++ / Web和DB开发
Hello All,

I have an ASP.NET page with one Textbox (SearchTextBox) and one ImageButton
(SearchButton) server controls. The user can type search text in
SearchTextBox and click SearchButton and the web server performs a database
query and displays the results. All of this works fine.

I want the user to be able to press the Enter key while the cursor is still
in SearchTextBox and have the SearchButton.Click event fire (thus performing
the database query and displaying the results). I have
SearchTextBox.AutoPostBack = False because I don''t want the page to repost
everytime the user types something; I just want it to query the database adn
display teh results when the user presses the Enter key.

I''ve thought about using AddHandler SearchTextBox.TextChanged, AddressOf
HandleEnterKey

using the private sub
Private Sub HandleEnterKey()
If EnterKeyPressed Then
SearchButton_Click(SearchtextBox, e)
End If
End Sub

but I don''t know what EventArgs to pass in for the parameter, e.

Does anyone know how to make this happen? Does anyone have any other ideas?

TIA,
--
Joe

VB.NET/C#/ASP.NET/VBA Automation/VB/C++/Web and DB development



这篇关于想要&lt; asp:textbox&gt;仅在按下Enter键时回发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆