I have a asp.net Text Box which accepts date in the format MM/DD/YYYY. After entering the date i will hit enter key.if enter key i need to execute server side code.How can w
Use the keydown event:
textBox.Attributes.Add("onKeyDown", "KeyDownHandler()");
Have KeyDownHandler()
check the pressed key and if correct, submit the surrounding form or invoke an AJAX request. See keyboard codes at Javascript Char Codes.
Alternatively, textbox ID can be injected into jQuery from .aspx
and assigned a direct keydown handler:
$('#<%=textBox.clientID%>').keydown(function (e) { /* Do server request; */ });
you set jquery tag
so using jquery u can try something like this
$("#field").keydown(function(e){
if(e.keyCode === 13)
{
// enter event
}
});
You can use the Panel.DefaultButton property to automatically click a button when the user presses Enter.
For example:
<asp:Panel Id="panel1" runat="server" DefaultButton="btnSubmit">
<asp:TextBox Id="txtDate" runat="server" />
<asp:Button Id="btnSubmit" Text="Submit" OnClick="btnSubmit_Click" runat="server" />
</asp:Panel>
If you don't want to show the button, you can set its style="display: none"
.
You can call this Javascript function to check whether enter key has been pressed or not.
function checkEnterKeyPress(e)
{
var key;
if(window.event)
key = window.event.keyCode; //IE
else
key = e.which; //firefox
if (key == 13){
alert(key);
}
}