Perform Button click event when user press Enter key in Textbox
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" run开发者_开发百科at="server" onclick="Button1_Click" Text="Send" />
</ContentTemplate>
</asp:UpdatePanel>
I have to perform Button1
click event when user press Enter key
in Textbox1
Put your form inside an asp.net panel control and set its defaultButton attribute with your button Id. See the code below:
<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>
Hope this will help you...
In the aspx page load event, add an onkeypress
to the box.
this.TextBox1.Attributes.Add(
"onkeypress", "button_click(this,'" + this.Button1.ClientID + "')");
Then add this javascript to evaluate the key press, and if it is "enter," click the right button.
<script>
function button_click(objTextBox,objBtnID)
{
if(window.event.keyCode==13)
{
document.getElementById(objBtnID).focus();
document.getElementById(objBtnID).click();
}
}
</script>
Codeproject has a complete solution for this:
http://www.codeproject.com/Articles/17241/Capturing-the-Enter-key-to-cause-a-button-click
and like the article says: "decide which solution best fits your needs"
=================== EDITED ANSWER ============================
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)");
}
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>
Panel Control
<asp:Panel ID="panSearch" runat="server" DefaultButton="btnSearch2" Width="100%" >
<asp:TextBox ID="txtboxFirstName2" runat="server" ></asp:TextBox>
</asp:Panel>
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
in the html code only, add a panel that contains the page's controls. Inside the panel, add a line DefaultButton = "buttonNameThatClicksAtEnter". See the example below, there should be nothing else required.
<asp:Panel runat="server" DefaultButton="Button1"> //add this!
//here goes all the page controls and the trigger button
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Send" />
</asp:Panel> //and this too!
You can do it with javascript/jquery:
<script>
function runScript(e) {
if (e.keyCode == 13) {
$("#myButton").click(); //jquery
document.getElementById("myButton").click(); //javascript
}
}
</script>
<asp:textbox id="txtUsername" runat="server" onkeypress="return runScript(event)" />
<asp:LinkButton id="myButton" text="Login" runat="server" />
You can try:
In HTML:
<asp:TextBox ID="TextBox1" runat="server" onKeyDown="submitButton(event)"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
And javascript:
function submitButton(event) {
if (event.which == 13) {
$('#Button1').trigger('click');
}
}
Code behind:
protected void Button1_Click(object sender, EventArgs e)
{
//load data and fill to gridview
} // fixed the function view for users
Hope this help
use Jquery or something here is example
of it http://riderdesign.com/articles/Check-username-availability-with-JQuery-and-ASP.NET.aspx i hope i will help you more
精彩评论