jam*_*ams 37 javascript c# asp.net
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Send" />
</ContentTemplate>
</asp:UpdatePanel>
Run Code Online (Sandbox Code Playgroud)
我必须执行Button1click事件时,用户按Enter key在Textbox1
Har*_*run 84
将表单放在asp.net面板控件中,并使用按钮Id设置其defaultButton属性.请参阅以下代码:
<asp:Panel ID="Panel1" runat="server" DefaultButton="Button1">
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Send" />
</ContentTemplate>
</asp:UpdatePanel>
</asp:Panel>
Run Code Online (Sandbox Code Playgroud)
希望对你有帮助...
Muh*_*tar 17
在aspx页面加载事件中,添加一个onkeypress框.
this.TextBox1.Attributes.Add(
"onkeypress", "button_click(this,'" + this.Button1.ClientID + "')");
Run Code Online (Sandbox Code Playgroud)
然后添加此javascript以评估按键,如果是"输入",则单击右键.
<script>
function button_click(objTextBox,objBtnID)
{
if(window.event.keyCode==13)
{
document.getElementById(objBtnID).focus();
document.getElementById(objBtnID).click();
}
}
</script>
Run Code Online (Sandbox Code Playgroud)
Codeproject对此有一个完整的解决方案:
http://www.codeproject.com/Articles/17241/Capturing-the-Enter-key-to-cause-a-button-click
正如文章所说:“决定哪种解决方案最适合您的需求”
================== 编辑的答案 ============================
The link mentioned above, talks about two ways of capturing the "Enter Key" event:
Javascript (bind the onKeyPress event to the object and create a javascript function to check which key was pressed and do your logic)
_Page_Load in code behind:_
//Add the javascript so we know where we want the enter key press to go
if (!IsPostBack)
{
txtboxFirstName.Attributes.Add("onKeyPress",
"doClick('" + btnSearch.ClientID + "',event)");
txtboxLastName.Attributes.Add("onKeyPress",
"doClick('" + btnSearch.ClientID + "',event)");
}
Run Code Online (Sandbox Code Playgroud)
Javascript code:
<SCRIPT type=text/javascript>
function doClick(buttonName,e)
{
//the purpose of this function is to allow the enter key to
//point to the correct button to click.
var key;
if(window.event)
key = window.event.keyCode; //IE
else
key = e.which; //firefox
if (key == 13)
{
//Get the button the user wants to have clicked
var btn = document.getElementById(buttonName);
if (btn != null)
{ //If we find the button click it
btn.click();
event.keyCode = 0
}
}
}
</SCRIPT>
Run Code Online (Sandbox Code Playgroud)
Panel Control
<asp:Panel ID="panSearch" runat="server" DefaultButton="btnSearch2" Width="100%" >
<asp:TextBox ID="txtboxFirstName2" runat="server" ></asp:TextBox>
</asp:Panel>
Run Code Online (Sandbox Code Playgroud)
Quoting:
Notice that the Panel tag has a property called DefaultButton. You set this property to the button ID of the button you want to be clicked on an Enter key press event. So any text box inside of the Panel will direct its Enter key press to the button set in the DefaultButton property of the Panel