Friday, April 13, 2012

Programmatically initiate javascript from code behind

Occassionally, situations may arise in which you want to initiate javascript from the C# code behind of your web application.  In the following simple example, the code in the Page Load event is triggering a simple alert message to fire; however, the sky is the limit with all the possibilities that this opens up:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        if (!ClientScript.IsStartupScriptRegistered(this.GetType(),"alert"))
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('All leases must be re-assigned to another parent before the delete operation may continue.  You may use this view to re-assign those leases.');", true);
        }
    }
}

Enjoy!

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.