简体   繁体   中英

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>

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM