You must Sign In to post a response.
  • Category: JQuery

    Error in passing value from cs to jquery

    Hi,

    I want to pass value from cs file to jquery file

    following is working code

    cs file

    protected void Page_Load(object sender, EventArgs e)
    {

    if (!IsPostBack)
    {
    Heading="Baiju";
    }
    }

    public string Heading { get; set; }

    jquery file

    $(document).ready(function () {
    var title = "<%=Heading%>";
    alert(title);

    });

    this is working fine and alert message is Baiju

    problem is if i remove jquery file and write the code in another js file and call it in the page

    like

    <script type="text/javascript" language="javascript" src="bd.js">

    it will not work

    then alertmessage will be

    "<%=Heading%>";


    How to solve this


    Regards

    Baiju
  • #765258
    Hi

    external js canot find your Heading property

    so you can use try label for this


    <asp:Label ID="lblHeading " runat="server"></asp:Label>




    $(document).ready(function () {
    var title = "<%=lblHeading %>";
    alert(title);
    });

    or

    $(document).ready(function () {
    var title = "<%=lblHeading.ClientID %>";
    alert(title);
    });


    if you use external js means you must mention ClientID for get value.

    Name : Dotnet Developer-2015
    Email Id : kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #769681
    We can't access the sever side variable values from external JS files direclty, but we can do this with registering scripts from server side using Page.ClientScript.RegisterStartupScript() or Page.ClientScript.RegisterScriptBlock()


    protected void Page_Load(object sender, EventArgs e)
    {

    if (!IsPostBack)
    {
    Page.ClientScript.RegisterClientScriptBlock(GetType(String), "Load", "var Heading='Baiju';", True));
    }
    }

    public string Heading { get; set; }

    Now , you ca use or call Heading variable from outside of page .

    Orelse

    Try to keep the value in hidden variable at serverside and use that hidden field from external JS file

    Thanks!
    B.Ramana Reddy


  • Sign In to post your comments