WHAT ARE THE WAYS TO EMIT CLIENT-SIDE JAVASCRIPT FROM SERVER-SIDE CODE IN ASP. NET?

Amit Singh

Total Post:565

Points:2825
Posted by  Amit Singh
 1379  View(s)
Ratings:
Rate this:
  1. Amit Singh

    Post:565

    Points:2825
    What are the ways to emit client-side JavaScript from server-side code in ASP. NET?

    The Page object in ASP. NET has two methods that allow emitting of client-side JavaScript:
    RegisterClientScriptBlock and RegisterStartupScript.

    Example usage:

    Page.RegisterClientScriptBlock("ScriptKey", "<script language=javascript>" + "function TestFn() { alert('Clients-side JavaScript'); }</script>");
    Page.RegisterStartupScript("ScriptKey", "<script language=javascript>" + "function TestFn() { alert('Clients-side JavaScript'); }</script>");

    ScriptKey is used to suppress the same JavaScript from being emitted more than once. Multiple calls to RegisterClientScriptBlock or RegisterStartupScript with the same value of ScriptKey emit the script only once, on the first call.

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!