.Net varible into client side JavaScript


I'm need to get a variable from my web app and place its value in a JavaScript.
I tried doing this with a Label but I get the HTML included. I don't think I can use the <%# %> from classic ASP?
Anyone have any ideas on how they would approach this problem?
0
tbone_stl
2/10/2006 7:32:39 PM
📁 asp.net.client-side
📃 24353 articles.
⭐ 2 followers.

💬 2 Replies
👁️‍🗨️ 1680 Views



tbone_stl wrote:
Anyone have any ideas on how they would approach this problem?

Hi,
and welcome to the ASP.NET forums.
It's possible to pass a variable to a javascript function. I created a small coding snippet:
<%@ Page Language="C#" %>
 
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<script runat="server">
 
    private string _test;
 
    public string Test
    {
        get { return _test; }
        set { _test = value; }
    }
   
   
    protected void Page_Load(object sender, EventArgs e)
    {
        Test = "This is a test";
       
        // Databind the page.
        DataBind();
    }
   
script>
 
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Pagetitle>
    <script type="text/ecmascript">
    function Test()
    {
        alert('<%# Test %>');
    }
   
    window.onload = Test;
   
    script>
head>
<body>
    <form id="form1" runat="server">
    <div>
   
    div>
    form>
body>
html>
 

As you can see, I made a public property that I fill up in the Page_Load eventhandler and later use it to get that string passed into the javascript.
Grz, Kris.

Read my blog. Handy Firefox plugins for web developers.


Workaround for non working Mark as answer buttons.
0
XIII
2/10/2006 7:49:35 PM

I am not sure what you are trying..   but  here is one way of accessing variables in Java script
 function DefaultValue()
{
 var txt1 = document.getElementById("TextBox1");  // will get the TextBox1 ID
alert(txt1.value);   // Get the contents of TextBox1
}
 


My Blog


"Don't be afraid to be wrong; otherwise you'll never be right."
0
budugu
2/10/2006 7:55:07 PM
Reply: