Friday, July 15, 2011

call JavaScript - jQuery code from ASP.NET Server-Side

Default.aspx
--------------------
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Register Client Script Block with Updatepanel</title>
    <script src="JavaScript/jquery.min.js" type="text/javascript"></script>
</head>
<body>
    <form id="theForm" runat="server">
    <div>
        <asp:ScriptManager ID="sm" runat="server">
        </asp:ScriptManager>
        <asp:UpdatePanel ID="upPnl" runat="server">
            <ContentTemplate>
                <asp:Button ID="btnPostback" runat="server" Text="Standard Postback" OnClick="btnPostback_Click" />
                <asp:Button ID="btnAsynchPostback" runat="server" Text="Asynchronous Postback" OnClick="btnAsynchPostback_Click" />
            <asp:Label ID="lbl1" runat="server"></asp:Label>
            </ContentTemplate>
            <Triggers>
                <asp:PostBackTrigger ControlID="btnPostback" />
            </Triggers>
        </asp:UpdatePanel>
    </div>
    </form>
</body>
</html>
Default.aspx.cs
--------------------
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void btnPostback_Click(object sender, EventArgs e)
    {
        runjQueryCode("alert('After a standard postback.')");
        lbl1.Text = "This is  postback";
    }
    protected void btnAsynchPostback_Click(object sender, EventArgs e)
    {
        runjQueryCode("alert('After an asynchronous postback.')");
        lbl1.Text = "This is Asynchrounous postback";
    }
    private string getjQueryCode(string jsCodetoRun)
    {
        StringBuilder sb = new StringBuilder();
        sb.AppendLine("$(document).ready(function() {");
        sb.AppendLine(jsCodetoRun);
        sb.AppendLine(" });");
        return sb.ToString();
    }
    private void runjQueryCode(string jsCodetoRun)
    {
        ScriptManager requestSM = ScriptManager.GetCurrent(this);
        if (requestSM != null && requestSM.IsInAsyncPostBack)
        {
            ScriptManager.RegisterClientScriptBlock(this,
                                                    typeof(Page),
                                                    Guid.NewGuid().ToString(),
                                                    getjQueryCode(jsCodetoRun),
                                                    true);
        }
        else
        {
            ClientScript.RegisterClientScriptBlock(typeof(Page),
                                                   Guid.NewGuid().ToString(),
                                                   getjQueryCode(jsCodetoRun),
                                                   true);
        }
    }
}

Output:

-------------- javascript postback

0 comments:

Post a Comment