Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Passing values to javascript from asp.net

Reply
Thread Tools

Passing values to javascript from asp.net

 
 
Sobin Thomas
Guest
Posts: n/a
 
      08-08-2008
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....


 
Reply With Quote
 
 
 
 
Milosz Skalecki [MCAD]
Guest
Posts: n/a
 
      08-08-2008
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.GetTyp e(), 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....
>
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing paremeter values through url using javascript Peter Javascript 2 05-15-2006 02:46 PM
Passing Values By javascript Alper OZGUR ASP .Net 3 03-01-2006 02:40 PM
passing int values to asp:dropdownlist using javascript at page l deepak kumar ASP .Net Web Controls 0 10-29-2004 02:33 AM
Passing values from JavaScript into MySQL/PHP RootShell Javascript 2 07-26-2004 01:18 PM
[XSLT]Passing values from Javascript to a XSLT variable Benjamin Hillsley XML 3 09-25-2003 04:50 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57