Subscribe to Subscribers
Talk to Webmaster Tony John

Online Members

baskar
More...


Forums » .NET » ASP.NET »

Key Press Event for Textbox in Asp.net using c#


Posted Date: 17 Aug 2012      Posted By:: Vishnu Kumar B     Member Level: Bronze    Member Rank: 4163     Points: 1   Responses: 5



Dear All,


I need a Example code to handle Key press event for Text box in Asp.net using C#.

Thanks & Regards,
B. Vishnu Kumar




Responses

#684643    Author: Ravindran        Member Level: Diamond      Member Rank: 3     Date: 17/Aug/2012   Rating: 2 out of 52 out of 5     Points: 4

Vishnu,

Refer below code i allow only alpha character in textbox. so validate user input in keypress


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Javascript Validations</title>
<script language="javascript" type="text/javascript">
//Allow only alpha charecter a-z A-Z Space Delete BackSpace
function Alpha(evt) {
var charCode = (evt.which) ? evt.which : event.keyCode
if ((charCode > 64 && charCode < 91) || (charCode > 96 && charCode < 123) || charCode > 47 && charCode < 58 || charCode == 127 || charCode == 8 || charCode == 32) {
return true;
}
else {
return false;
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
Enter Emp Name<asp:TextBox ID="TextBox2" runat="server" onkeyPress="return Alpha(event);"></asp:TextBox>
</form>
</body>
</html>


Regards
N.Ravindran
Your Hard work never fails



 
#684645    Author: Gopi A      Member Level: Silver      Member Rank: 349     Date: 17/Aug/2012   Rating: 2 out of 52 out of 5     Points: 2

Hi Vishnu,

Could you please let us know what you need exactly. Then we can suggest according to you question.

-----------------------------------------------------------------------------
Regards,
Gopi A.
+91 9894315571
Skype:gopi.net



 
#684646    Author: Vishnu Kumar B      Member Level: Bronze      Member Rank: 4163     Date: 17/Aug/2012   Rating: 2 out of 52 out of 5     Points: 1

Dear All,

I am using a text box to enter the age of the person. According to that age value it has to calculate the Date of Birth by deducting that particular value in current date as per year/month/day. And this Date of birth should display in another text box. I need this operation when i enter the age value in the text box.

Thanks & Regards,
Vishnu Kumar B.






 
#684668    Author: saravanakumar      Member Level: Gold      Member Rank: 197     Date: 18/Aug/2012   Rating: 2 out of 52 out of 5     Points: 4

hi friend try this,

design side code,
 <asp:TextBox ID="txtqty" runat="server" TabIndex="6" 
AutoPostBack="true"
ontextchanged="txtqty_TextChanged"> </asp:TextBox>
<asp:RequiredFieldValidator ID="Rfvquty" runat="server" ControlToValidate="txtqty"
ErrorMessage="Enter the Material Quantity" ForeColor="Red" SetFocusOnError="True" InitialValue="0"
ValidationGroup="valnewmaterial">*</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" ControlToValidate="txtqty"
ErrorMessage="Please Enter Only Numbers" ValidationExpression="^\d+$" ValidationGroup="valnewmaterial">*</asp:RegularExpressionValidator>


server side code,

protected void txtqty_TextChanged(object sender, EventArgs e)
{
int age= int.Parse(txtqty.Text.Trim ());
txtspec.Text = DateTime.Now.AddYears(-age).Date.ToShortDateString();
}



 
#684675    Author: Prasad kulkarni        Member Level: Diamond      Member Rank: 8     Date: 18/Aug/2012   Rating: 2 out of 52 out of 5     Points: 2

"keypress" is client side event we can use Javascript for that.
check following example where we use keypress event


<script language="Javascript">
fucntion getKeycode()
{
if(event.keyCode == 13)
{
alert("You have pressed enter key");
return false;
}

}

<asp:TextBox id="txtKeypress" Runat="Server" onkeypress="return getKeycode()" />
</script>



hope it helps

Thanks
Koolprasd2003
[DotNetSpider MVM]



 
Post Reply
You must Sign In to post a response.

Next : File upload csv file into database using bo,bll and dal
Previous : CollapsiblePanelExtender Isssue on webserver
Return to Discussion Forum
Post New Message
Category:

Related Messages



Follow us on Twitter: https://twitter.com/dotnetspider

Active Members
TodayLast 7 Daysmore...

Awards & Gifts
Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
2005 - 2012 All Rights Reserved.
.NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
Articles, tutorials and all other content offered here is for educational purpose only.
We are not associated with Microsoft or its partners.