Nat
Nat

Reputation: 749

How to add JavaScript from Code Behind C#?

How do I run this javascript from code behind C# during page load? Thanks a lot and many thanks in advance.

<script type="text/javascript">
document.onkeydown = function (event)
{
     event = (event || window.event);
     if (event.keyCode == 20)
     {
          alert('Caps not allow');
     }
}
</script>

Upvotes: 0

Views: 434

Answers (1)

Jeff Mergler
Jeff Mergler

Reputation: 1512

Try using the RegisterStartupScript method.

Example in c#:

private void Page_Load1(object sender, System.EventArgs e)
{
    string js = "document.onkeydown = function (event) { event = (event || window.event); if (event.keyCode == 20) { alert('Caps not allow'); } }";

    ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "capscheck", js, true);
}

Upvotes: 1

Related Questions