Wednesday, March 7, 2012

On Keypress show character at load event in Asp.net

Without taking any control, this Example will show how we can print the character on webpage when any key is pressed on keyboard
Here we will use javascript.
in .aspx page



<script type="text/javascript">
    document.onkeyup = KeyCheck;
    function KeyCheck(e)
    {
        var KeyID = (window.event) ? event.keyCode : e.keyCode;
         {          
                __doPostBack('__Page', KeyID);
         }
    }
</script>
//here ascii value of character will get when key is pressed


VB
in .vb page
 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load



Page.ClientScript.GetPostBackEventReference(Me, "")
 Dim eventArgs As String = Request("__EVENTARGUMENT")
   If eventArgs <> "" Then
 Response.Write(ChrW(Convert.ToInt32(eventArgs)))
 EndIf


End Sub
//here we will get keycode of character and get converted into character using Chrw keyword


C#
in .cs


protected void Page_Load(object sender, System.EventArgs e)
{
Page.ClientScript.GetPostBackEventReference(this, "");
string eventArgs = Request("__EVENTARGUMENT");
if (!string.IsNullOrEmpty(eventArgs)) 
{
Response.Write(ChrW(Convert.ToInt32(eventArgs)))
}
}


No comments:

Post a Comment