Showing posts with label JavaScript. Show all posts
Showing posts with label JavaScript. Show all posts

27 April, 2012

Call JavaScript from Asp.net code behind

Suppose you want to display confirmation message by javascript from asp.net code behind after saving data. Write the following code in your button event.

protected void btnSaved_Click(object sender, EventArgs e)
{

         string msg = "Data saved sucessfully";
         Page.ClientScript.RegisterStartupScript(this.GetType(), "Alert", "<script type=text/javascript<alert('" + msg + "')</script>");


}

Determine whether Caps Lock is on in ASP.net Page by JavaScript


Here I used a simple Textbox in ASP.net page and used Javascript to determine whether Caps Lock is on or off.
I have used the following Javascript code in between head  tag.
 function isCapslock(e) {

            e = (e) ? e : window.event;

            var charCode = false;

            if (e.which) {
                charCode = e.which;
            } else if (e.keyCode) {
                charCode = e.keyCode;
            }

            var shifton = false;
            if (e.shiftKey) {
                shifton = e.shiftKey;
            } else if (e.modifiers) {
                shifton = !!(e.modifiers & 4);
            }

            if (charCode >= 97 && charCode <= 122 && shifton) {
                document.getElementById("txtCapsMsg").value = "Caps Lock is on";
                return true;
            }

            if (charCode >= 65 && charCode <= 90 && !shifton) {
                document.getElementById("txtCapsMsg").value = "Caps Lock is on";
                return true;
            }

            document.getElementById("txtCapsMsg").value = "Caps Lock is off";
            return false;

        }

I have also used two text box in ASP.net page. One for input and another for displaying message whether Caps Lock is on or off.


<form id="form1" runat="server">
    <div>
        <table>
            <tr>
                <td>
                    Message :
                td>
                <td>
                    <input type="text" id="txtCapsMsg" />
                td>
            tr>
            <tr>
                <td>
                    Input Text :
                td>
                <td>
                    <asp:TextBox ID="txtInputCaps" runat="server" onkeypress="isCapslock(event)">asp:TextBox>
                td>
            tr>
        table>
    div>
    form>

Now input some text in input textbox. Message will be showed in txtCapsMsg textbox whether Caps Lock is on or off.