Passing values to javascript from asp.net

Discussion in 'ASP .Net' started by Sobin Thomas, Aug 8, 2008.

  1. Sobin Thomas

    Sobin Thomas Guest

    Hi All....
    I have a variable "myVar" in my javascript.I want to set its value as a C#
    property whose value is set at run time..
    How can I do this?
    Thanks in advance....
    Sobin Thomas, Aug 8, 2008
    #1
    1. Advertising

  2. Hi Sobin,

    Two ways:
    1.
    -- c# code
    public string MyProperty
    {
    get
    {
    return "Whatever" + DateTime.Now.ToString();
    }
    }

    -- aspx code
    <script type="text/javascript">
    var myVar = '<%=MyProperty%>';
    alert(myVar);
    </script>

    2.
    protected void Page_PreRender(object sender, EventArgs e)
    {
    const string ScriptKey = "testScript";

    if (!ClientScript.IsClientScriptBlockRegistered(this.GetType(), ScriptKey))
    {
    string script = String.Format("var myVar = '{0}'", MyProperty);
    ClientScript.RegisterClientScriptBlock(this.GetType(), ScriptKey, script,
    true);
    }
    }

    public string MyProperty
    {
    get
    {
    return "Whatever" + DateTime.Now.ToString();
    }
    }

    HTH
    --
    Milosz


    "Sobin Thomas" wrote:

    > Hi All....
    > I have a variable "myVar" in my javascript.I want to set its value as a C#
    > property whose value is set at run time..
    > How can I do this?
    > Thanks in advance....
    >
    >
    >
    Milosz Skalecki [MCAD], Aug 8, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Virgil Hobbs
    Replies:
    0
    Views:
    504
    David Virgil Hobbs
    Jan 6, 2004
  2. arkam
    Replies:
    7
    Views:
    510
    arkam
    Apr 16, 2004
  3. Replies:
    13
    Views:
    6,634
    =?Utf-8?B?UGhpbGxpcCBXaWxsaWFtcw==?=
    Jan 26, 2006
  4. ASP.NET passing values

    , Mar 7, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    424
    =?Utf-8?B?Sm9l?=
    Mar 7, 2006
  5. deepak kumar
    Replies:
    0
    Views:
    133
    deepak kumar
    Oct 29, 2004
Loading...

Share This Page