Fill a javascript var with the content of a variable declared in a vb.net Sub

D

diac

Hello,

I create and fill a variable in the Page_Load sub and then i need it in
a javascript code. Of course the xxx variable is declared in the sub so
that it is not available out of the sub. How can i fill the javascript
variable with its content?

------------------------------------
Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
Dim xxx As String = "xxx"
End Sub

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>PR</title>
<script type="text/javascript" language="javascript">

var xxx = <%=xxx%>
 
K

Ken Cox [Microsoft MVP]

Hi Daniele,

There are methods built into ASP.NET for outputing script. Try the code
below?

Ken
Microsoft MVP [ASP.NET]

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
Protected Sub Page_Load _
(ByVal sender As Object, _
ByVal e As System.EventArgs)
Dim xxx As Integer
xxx = 666
ClientScript.RegisterClientScriptBlock _
(Me.GetType, _
"var", "var xxx=" & xxx.ToString & ";", _
True)
End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body onload="alert(xxx);">
<form id="form1" runat="server">
<div>

</div>
</form>
</body>
</html>
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top