Saturday, March 24, 2012
Server Variable in Client Script
I am trying to pass a variable from my VB asp.net script (from for example
Sub Page_Load in mypage.aspx.vb) to my Client side script.
I have found and looked at a very good example "Client and Server Scripting
in Web Pages" but it only shows server side scripting that is written in
HTML in mypage.aspx and not script from mypage.aspx.vb. How can I pass a
variable value from mypage.aspx.vp to my client side script?
Sample from "Client and Server Scripting in Web Pages"
<%@dotnet.itags.org. LANGUAGE="VBSCRIPT" %
<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual InterDev 1.0">
<META HTTP-EQUIV="Content-Type" content="text/html; charset=iso-8859-1">
<TITLE>Document Title</TITLE>
</HEAD>
<BODY
<% Dim strMyServerVar
strMyServerVar = 42 %>
.......
The following line of HTML was generated on the client to print the value of
a client variable into the page after the page was sent to the browser. The
value of the client variable has been set equal to the value of the server
variable printed above.
<BR>
<BR>
<SCRIPT LANGUAGE = "JavaScript">
<!--
var x ;
x = <%= strMyServerVar %> ;
if (x == <%= strMyServerVar%>) {
document.write ("<STRONG>The value of the client variable is " + x +
".</STRONG>")
}
//
-->
</SCRIPTFew ways... build your clientside script dynamically from within your server
side script, passing in the values.
Also, could always put the values in a hidden text box and have the
clientside pull from that.
--
Curt Christianson
Owner/Lead Developer, DF-Software
www.Darkfalz.com
"Bob T" <twick10@.hotmail.com> wrote in message
news:e9G7ajs1DHA.2604@.TK2MSFTNGP09.phx.gbl...
> Hi All,
> I am trying to pass a variable from my VB asp.net script (from for example
> Sub Page_Load in mypage.aspx.vb) to my Client side script.
> I have found and looked at a very good example "Client and Server
Scripting
> in Web Pages" but it only shows server side scripting that is written in
> HTML in mypage.aspx and not script from mypage.aspx.vb. How can I pass a
> variable value from mypage.aspx.vp to my client side script?
>
> Sample from "Client and Server Scripting in Web Pages"
> <%@. LANGUAGE="VBSCRIPT" %>
> <HTML>
> <HEAD>
> <META NAME="GENERATOR" Content="Microsoft Visual InterDev 1.0">
> <META HTTP-EQUIV="Content-Type" content="text/html; charset=iso-8859-1">
> <TITLE>Document Title</TITLE>
> </HEAD>
> <BODY>
> <% Dim strMyServerVar
> strMyServerVar = 42 %>
> ......
> The following line of HTML was generated on the client to print the value
of
> a client variable into the page after the page was sent to the browser.
The
> value of the client variable has been set equal to the value of the server
> variable printed above.
> <BR>
> <BR>
> <SCRIPT LANGUAGE = "JavaScript">
> <!--
> var x ;
> x = <%= strMyServerVar %> ;
> if (x == <%= strMyServerVar%>) {
> document.write ("<STRONG>The value of the client variable is " + x +
> ".</STRONG>")
> }
> //
> -->
> </SCRIPT>
Bob,
Why would you do this?
The whole point of using a code behind page is so that the programming logic
can be separated from the display. Placing the programming logic in both
places defeats one of the best things about .NET.
Just Dim your variable in the code behind and you're done.
Move this line to the code behind page like this:
Public Class WebForm1
Inherits System.Web.UI.Page
Public strMyServerVar As String = "42"
'--Rest of page code below
And remove the variable declaration from the .aspx page altogether:
Delete This: <% Dim strMyServerVar strMyServerVar = 42 %
Your code will still work the same way. The .aspx page will have access to
the variable because it's declared as Public (you could also declare it as
Protected or Protected Friend)
--
Sincerely,
S. Justin Gengo, MCP
Web Developer / Programmer
Free code library at:
www.aboutfortunate.com
"Out of chaos comes order."
Nietzche
"Bob T" <twick10@.hotmail.com> wrote in message
news:e9G7ajs1DHA.2604@.TK2MSFTNGP09.phx.gbl...
> Hi All,
> I am trying to pass a variable from my VB asp.net script (from for example
> Sub Page_Load in mypage.aspx.vb) to my Client side script.
> I have found and looked at a very good example "Client and Server
Scripting
> in Web Pages" but it only shows server side scripting that is written in
> HTML in mypage.aspx and not script from mypage.aspx.vb. How can I pass a
> variable value from mypage.aspx.vp to my client side script?
>
> Sample from "Client and Server Scripting in Web Pages"
> <%@. LANGUAGE="VBSCRIPT" %>
> <HTML>
> <HEAD>
> <META NAME="GENERATOR" Content="Microsoft Visual InterDev 1.0">
> <META HTTP-EQUIV="Content-Type" content="text/html; charset=iso-8859-1">
> <TITLE>Document Title</TITLE>
> </HEAD>
> <BODY>
> <% Dim strMyServerVar
> strMyServerVar = 42 %>
> ......
> The following line of HTML was generated on the client to print the value
of
> a client variable into the page after the page was sent to the browser.
The
> value of the client variable has been set equal to the value of the server
> variable printed above.
> <BR>
> <BR>
> <SCRIPT LANGUAGE = "JavaScript">
> <!--
> var x ;
> x = <%= strMyServerVar %> ;
> if (x == <%= strMyServerVar%>) {
> document.write ("<STRONG>The value of the client variable is " + x +
> ".</STRONG>")
> }
> //
> -->
> </SCRIPT>
Bob,
Just a quick note. My previous message shows how to do what you want, but I
shouldn't have included the "Why would you do this?" at the top of the
message.
I misunderstood part of your question.
Still, the code I provided does what you need.
Happy programming!
--
Sincerely,
S. Justin Gengo, MCP
Web Developer / Programmer
Free code library at:
www.aboutfortunate.com
"Out of chaos comes order."
Nietzche
"Bob T" <twick10@.hotmail.com> wrote in message
news:e9G7ajs1DHA.2604@.TK2MSFTNGP09.phx.gbl...
> Hi All,
> I am trying to pass a variable from my VB asp.net script (from for example
> Sub Page_Load in mypage.aspx.vb) to my Client side script.
> I have found and looked at a very good example "Client and Server
Scripting
> in Web Pages" but it only shows server side scripting that is written in
> HTML in mypage.aspx and not script from mypage.aspx.vb. How can I pass a
> variable value from mypage.aspx.vp to my client side script?
>
> Sample from "Client and Server Scripting in Web Pages"
> <%@. LANGUAGE="VBSCRIPT" %>
> <HTML>
> <HEAD>
> <META NAME="GENERATOR" Content="Microsoft Visual InterDev 1.0">
> <META HTTP-EQUIV="Content-Type" content="text/html; charset=iso-8859-1">
> <TITLE>Document Title</TITLE>
> </HEAD>
> <BODY>
> <% Dim strMyServerVar
> strMyServerVar = 42 %>
> ......
> The following line of HTML was generated on the client to print the value
of
> a client variable into the page after the page was sent to the browser.
The
> value of the client variable has been set equal to the value of the server
> variable printed above.
> <BR>
> <BR>
> <SCRIPT LANGUAGE = "JavaScript">
> <!--
> var x ;
> x = <%= strMyServerVar %> ;
> if (x == <%= strMyServerVar%>) {
> document.write ("<STRONG>The value of the client variable is " + x +
> ".</STRONG>")
> }
> //
> -->
> </SCRIPT>
Thanks all
The rec from S. Justin Gengo works fine. I felt there was a simple way.
Bob T
"Bob T" <twick10@.hotmail.com> wrote in message
news:e9G7ajs1DHA.2604@.TK2MSFTNGP09.phx.gbl...
> Hi All,
> I am trying to pass a variable from my VB asp.net script (from for example
> Sub Page_Load in mypage.aspx.vb) to my Client side script.
> I have found and looked at a very good example "Client and Server
Scripting
> in Web Pages" but it only shows server side scripting that is written in
> HTML in mypage.aspx and not script from mypage.aspx.vb. How can I pass a
> variable value from mypage.aspx.vp to my client side script?
>
> Sample from "Client and Server Scripting in Web Pages"
> <%@. LANGUAGE="VBSCRIPT" %>
> <HTML>
> <HEAD>
> <META NAME="GENERATOR" Content="Microsoft Visual InterDev 1.0">
> <META HTTP-EQUIV="Content-Type" content="text/html; charset=iso-8859-1">
> <TITLE>Document Title</TITLE>
> </HEAD>
> <BODY>
> <% Dim strMyServerVar
> strMyServerVar = 42 %>
> ......
> The following line of HTML was generated on the client to print the value
of
> a client variable into the page after the page was sent to the browser.
The
> value of the client variable has been set equal to the value of the server
> variable printed above.
> <BR>
> <BR>
> <SCRIPT LANGUAGE = "JavaScript">
> <!--
> var x ;
> x = <%= strMyServerVar %> ;
> if (x == <%= strMyServerVar%>) {
> document.write ("<STRONG>The value of the client variable is " + x +
> ".</STRONG>")
> }
> //
> -->
> </SCRIPT>
Thursday, March 22, 2012
Server.GetLastError
I am trying to get the last occured error via the
following code:
Sub Page_Load(...)
Dim zero As Integer = 0
Try
Dim err As Integer = 10 / zero
Catch ex As Exception
End Try
Response.write(Server.GetLastError.Message)
End Sub
However the above code always throws an error at the
response.write line with:
Object reference not set to an instance of an object.
Anybody knows why this is happening?Well, first off 10/0 will not throw an error. I also assume you must have
Option Strict Off or your code would not even compile: err would need to be
dimmed as a double or you would need ctype(10/zero, double) for code to
compile.
I think GetLastError only returns the last unhandled error, during the
current context, and I think the page's Page_Error event or global.asax
Application_Error event has to be raised to access it. Once one of those
events is raised Server.GetLastError is available anywhere in your
code...for the remainder of the current context / request
Example:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim zero As Integer = 0
Dim err As Double = 10 / CType("x", Integer)
End Sub
Private Sub Page_Error(ByVal sender As Object, ByVal e As System.EventArgs)
Handles MyBase.Error
Response.Write(Server.GetLastError.Message)
End Sub
Or since it's now available anywhere, you could also do something like this:
Private Sub Page_Error(ByVal sender As Object, ByVal e As System.EventArgs)
Handles MyBase.Error
Server.Transfer("error.aspx")
End Sub
And then in error.aspx
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Response.Write(Server.GetLastError.Message)
End Sub
"VK" <anonymous@.discussions.microsoft.com> wrote in message
news:0dfb01c54132$353bf820$a401280a@.phx.gbl...
> Hello,
> I am trying to get the last occured error via the
> following code:
> Sub Page_Load(...)
> Dim zero As Integer = 0
> Try
> Dim err As Integer = 10 / zero
> Catch ex As Exception
> End Try
> Response.write(Server.GetLastError.Message)
> End Sub
> However the above code always throws an error at the
> response.write line with:
> Object reference not set to an instance of an object.
> Anybody knows why this is happening?
Since you are catching the exception, it is no longer an error.
bill
"VK" <anonymous@.discussions.microsoft.com> wrote in message
news:0dfb01c54132$353bf820$a401280a@.phx.gbl...
> Hello,
> I am trying to get the last occured error via the
> following code:
> Sub Page_Load(...)
> Dim zero As Integer = 0
> Try
> Dim err As Integer = 10 / zero
> Catch ex As Exception
> End Try
> Response.write(Server.GetLastError.Message)
> End Sub
> However the above code always throws an error at the
> response.write line with:
> Object reference not set to an instance of an object.
> Anybody knows why this is happening?
Hello VK,
If this code would generate an error (which it wont as detailed by Brad),
you're swallowing it anyways. You need to remove the try/catch.
Matt Berther
http://www.mattberther.com
> Hello,
> I am trying to get the last occured error via the following code:
> Sub Page_Load(...)
> Dim zero As Integer = 0
> Try
> Dim err As Integer = 10 / zero
> Catch ex As Exception
> End Try
> Response.write(Server.GetLastError.Message)
> End Sub
> However the above code always throws an error at the response.write
> line with:
> Object reference not set to an instance of an object.
> Anybody knows why this is happening?
>
Server.GetLastError
I am trying to get the last occured error via the
following code:
Sub Page_Load(...)
Dim zero As Integer = 0
Try
Dim err As Integer = 10 / zero
Catch ex As Exception
End Try
Response.write(Server.GetLastError.Message)
End Sub
However the above code always throws an error at the
response.write line with:
Object reference not set to an instance of an object.
Anybody knows why this is happening?Well, first off 10/0 will not throw an error. I also assume you must have
Option Strict Off or your code would not even compile: err would need to be
dimmed as a double or you would need ctype(10/zero, double) for code to
compile.
I think GetLastError only returns the last unhandled error, during the
current context, and I think the page's Page_Error event or global.asax
Application_Error event has to be raised to access it. Once one of those
events is raised Server.GetLastError is available anywhere in your
code...for the remainder of the current context / request
Example:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim zero As Integer = 0
Dim err As Double = 10 / CType("x", Integer)
End Sub
Private Sub Page_Error(ByVal sender As Object, ByVal e As System.EventArgs)
Handles MyBase.Error
Response.Write(Server.GetLastError.Message)
End Sub
Or since it's now available anywhere, you could also do something like this:
Private Sub Page_Error(ByVal sender As Object, ByVal e As System.EventArgs)
Handles MyBase.Error
Server.Transfer("error.aspx")
End Sub
And then in error.aspx
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Response.Write(Server.GetLastError.Message)
End Sub
"VK" <anonymous@.discussions.microsoft.com> wrote in message
news:0dfb01c54132$353bf820$a401280a@.phx.gbl...
> Hello,
> I am trying to get the last occured error via the
> following code:
> Sub Page_Load(...)
> Dim zero As Integer = 0
> Try
> Dim err As Integer = 10 / zero
> Catch ex As Exception
> End Try
> Response.write(Server.GetLastError.Message)
> End Sub
> However the above code always throws an error at the
> response.write line with:
> Object reference not set to an instance of an object.
> Anybody knows why this is happening?
Well, first off 10/0 will not throw an error. I also assume you must have
Option Strict Off or your code would not even compile: err would need to be
dimmed as a double or you would need ctype(10/zero, double) for code to
compile.
I think GetLastError only returns the last unhandled error, during the
current context, and I think the page's Page_Error event or global.asax
Application_Error event has to be raised to access it. Once one of those
events is raised Server.GetLastError is available anywhere in your
code...for the remainder of the current context / request
Example:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim zero As Integer = 0
Dim err As Double = 10 / CType("x", Integer)
End Sub
Private Sub Page_Error(ByVal sender As Object, ByVal e As System.EventArgs)
Handles MyBase.Error
Response.Write(Server.GetLastError.Message)
End Sub
Or since it's now available anywhere, you could also do something like this:
Private Sub Page_Error(ByVal sender As Object, ByVal e As System.EventArgs)
Handles MyBase.Error
Server.Transfer("error.aspx")
End Sub
And then in error.aspx
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Response.Write(Server.GetLastError.Message)
End Sub
"VK" <anonymous@.discussions.microsoft.com> wrote in message
news:0dfb01c54132$353bf820$a401280a@.phx.gbl...
> Hello,
> I am trying to get the last occured error via the
> following code:
> Sub Page_Load(...)
> Dim zero As Integer = 0
> Try
> Dim err As Integer = 10 / zero
> Catch ex As Exception
> End Try
> Response.write(Server.GetLastError.Message)
> End Sub
> However the above code always throws an error at the
> response.write line with:
> Object reference not set to an instance of an object.
> Anybody knows why this is happening?
Since you are catching the exception, it is no longer an error.
bill
"VK" <anonymous@.discussions.microsoft.com> wrote in message
news:0dfb01c54132$353bf820$a401280a@.phx.gbl...
> Hello,
> I am trying to get the last occured error via the
> following code:
> Sub Page_Load(...)
> Dim zero As Integer = 0
> Try
> Dim err As Integer = 10 / zero
> Catch ex As Exception
> End Try
> Response.write(Server.GetLastError.Message)
> End Sub
> However the above code always throws an error at the
> response.write line with:
> Object reference not set to an instance of an object.
> Anybody knows why this is happening?
Hello VK,
If this code would generate an error (which it wont as detailed by Brad),
you're swallowing it anyways. You need to remove the try/catch.
--
Matt Berther
http://www.mattberther.com
> Hello,
> I am trying to get the last occured error via the following code:
> Sub Page_Load(...)
> Dim zero As Integer = 0
> Try
> Dim err As Integer = 10 / zero
> Catch ex As Exception
> End Try
> Response.write(Server.GetLastError.Message)
> End Sub
> However the above code always throws an error at the response.write
> line with:
> Object reference not set to an instance of an object.
> Anybody knows why this is happening?