Visual Studio .NET - Using C# variable in javascript function?

Asked By james b on 08-Mar-06 05:21 AM
Hi all - I need to use a c# integer in a javascript function, e.g
[CODE]
<script language="JavaScript">
function TargetDate()
{
	targetNumber=getQueryString("tnum=","xxxyyy");	
}
</script>
[/CODE] 
where "xxxyyy" is a c# integer that I get from a database and pass into the javascript somehow - is this possible?

Using C# variable in javascript function

Asked By Pankaj Sharma on 08-Mar-06 05:40 AM
Hi,
How can a server side variable be accessed on client side?
I don't think it is possible to directly access C# variable in javascript code. If you get anything about this please share with me.
Instead you can save value of C# variable in hidden field on the form and then access that field value in javascript.
[CODE]
<INPUT id="hd" style="Z-INDEX: 101; LEFT: 16px; POSITION: absolute; TOP: 144px" type="hidden"  value="<%=strvariable %>">
<script language="javascript">
function Button_Click()
{
	alert(document.getElementById('hd').value); 
}
</script>
[/CODE]

To add Client-Side Script from the Code-Behind

Asked By mv ark on 08-Mar-06 07:27 AM
explore the RegisterStartupScript and RegisterClientScriptBlock methods:
 http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/aspnet-usingjavascript.asp
This snippet demonstrates use of RegisterStartupScript method
Page.RegisterStartupScript("MyScript",
   "<script language=javascript>" +
   "function AlertHello() { alert('Hello ASP.NET'); }</script>");
Button1.Attributes["onclick"] = "AlertHello()";
Button2.Attributes["onclick"] = "AlertHello()";
 This snippet shows how to access a C# variable through javascript, after the page loads.
private void Page_Load(object sender, System.EventArgs e)
{
	String myString = "Hello C# World";
	Button1.Attributes.Add("onclick","javascript:alert('" + myString + "')");

thanks, but

Asked By james b on 08-Mar-06 07:34 AM
not really what I'm after. I have a very large block of javascript code that executes when the page loads. I need to "inject" a c# integer (grabbed from a database) into that javascript code.
Hopefully the technique that was mentioned in the first reply will work...will give it a try this afternoon
Thanks again,
James
Using C# variable in javascript function
Asked By Paul Flies on 08-Mar-06 09:33 AM
Another method:
code behind:
public int c_integer=0;
assign c_integer form the db
HTML:
<script language="javascript" type="text/javascript">
var c_int=<%=c_integer%>;
</script>
Then use the var as needed.
Hope this helps, it works for me.
Thanks - this works great!
Asked By james b on 10-Mar-06 04:58 AM
Thanks Paul - this is exactly what I was after
James
Using C# variable in javascript function
Asked By Aj .. on 03-Apr-06 07:51 PM
Paul,
That was very helpful, thanks a bunch. Was wondering if you knew how to access C# array variables from Javascript. I tried but in vain...
Thanks,
Aj
Accessing C# array variables from Javascript
Asked By Paul Flies on 04-Apr-06 10:35 AM
AJ, without knowing exactly what you need, here's what I do:
	Declare a public string variable.
	Fill the string variable with a known character seperating values, i.e., variable+=value+",";
	On client-side, assign a javascript variable to the C# variable.
	Use the javascript method split to create the javascript array.
	Iterate thru the array to retrieve the results.
	Here's an example:
		[CODE]
		var c_var="<%=variable%>"; // variable being the C# public string variable
		var js_array=c_var.split(",");
		for(i=0;i<js_array.length;i++){
			alert(js_array[i]);
		}
		[/CODE]
The seperator used in the C# between results is the same seperator used in the javascript split method.
I hope this helps and good luck,
Accessing C# array variables from Javascript
Asked By Aj .. on 04-Apr-06 05:39 PM
Paul,
Thanks a ton for the prompt reply. Your solution works fine. Needed to store the array in a string as you said instead of an typed array.
Thanks,
Aj
ashutosh replied to Paul Flies on 02-Mar-10 01:36 AM
Thanks a lot.
Really its a good code.
m.m, m replied to james b on 20-Feb-11 06:14 AM
m mmmmm,kmmk/k
karthik reddy replied to james b on 23-Feb-11 07:33 AM
Thanks for Helping me in accessing variables from c# in javascript
Christophe replied to james b on 27-May-11 09:34 AM
It's good for the first time, but how can i refresh the variable without refreshing the page?

 I ve got a timer thats reads a static class variable thats empty on initialize. It gets filled after triggering a method, but the timer class keeps reading an empty value. Only when i refresh the page, the filled value is displayed.