Reputation: 75
I have an ASP.net textbox that the user needs to enter emails. I need to detect when the user types a semi-colon(;) then unhide a textbox and set the focus to that textbox.
Upvotes: 0
Views: 98
Reputation: 606
Disclaimer: This might seem a little "hacky" and I'm sure there are better ways of doing this.
You can use the onkeypress
of the textbox, set that to a JavaScript function. In the JavaScript function, programmatically "unhide" and give focus to your other textbox on the client-side. No post-back required.
JavaScript:
function checkForSemicolons(event) {
var txtEmail = document.getElementById("<%= txtEmail.ClientID %>");
if (event.keyCode === 59) {
// unhide other textbox and give focus to it
}
}
ASPX:
<asp:TextBox ID="txtEmail" onkeypress="checkForSemicolons(event)" runat="server" />
Upvotes: 2
Reputation: 56
OnKeypress event is fired from the client side, so you need do catch that from the client script.
You can use javascript to cause a postback and than send the parameters to the server-side, but usually you can handle all from the client. On jquery you would have somethig like this:
$("TextboxID").keypress(function(e){
//do something
//If you want to cause a postback use " __doPostBack('EventTaarget','Parameters');"
//you can capture the key that was pressed on e.keycode (for keycodes list, check: http://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes)
})
On the code behind you can add some code to capture this event on page_load.
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Me.Request("__EVENTTARGET") Is Nothing Then
'do something - you can use the Parameter with: Me.Request("__EVENTARGUMENT")
end if
end sub
Upvotes: 0