Monday, March 26, 2012

passing variable from code behind to function in javascript

Just wondering if someone could provide an example of passing a variable from
the code behind to javascript in vb. I want to have one control have focus
with the page loading with one condition and have another control with focus
after the page loads from a different condition. A gets set in the code
aspx.vb file and the script might look something like,
<script language="javascript">
function DoHighlight(a)
{
if a = 1 {
document.Form1.dr_lst_system.focus();
}
else
{
Form1.textbox1.focus();
}
}
</script
thanks.
--
Paul G
Software engineer.Hi Paul,

It is impossible to pass a variable value from the server to the client.
Server-side code executes on the server. Client-side code executes on the
client.

That said, it is certainly possible to WRITE client-side scripting code
(which will be executed on the client) dynamically from the server side.
That is what you need to do.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.

"Paul" <Paul@.discussions.microsoft.com> wrote in message
news:1BB64CF9-9547-4DF0-9556-E2F88CE79ECA@.microsoft.com...
> Just wondering if someone could provide an example of passing a variable
> from
> the code behind to javascript in vb. I want to have one control have
> focus
> with the page loading with one condition and have another control with
> focus
> after the page loads from a different condition. A gets set in the code
> aspx.vb file and the script might look something like,
> <script language="javascript">
> function DoHighlight(a)
> {
> if a = 1 {
> document.Form1.dr_lst_system.focus();
> }
> else
> {
> Form1.textbox1.focus();
> }
> }
> </script>
>
> thanks.
> --
> Paul G
> Software engineer.
Paul,

This is normally done with hidden input controls. Put a line

<input type=hidden runat=server id=inhA
in the .aspx file. If you switch from Html view to Design and back, you will
find in the code-behind a definition for inhA as an obyect of type
System.Web.UI.HtmlControls.HtmlInputHidden.

You can now set inhA.Value="1" on server side and check if (inhA.value ==
"1") on client side.

Eliyahu

"Paul" <Paul@.discussions.microsoft.com> wrote in message
news:1BB64CF9-9547-4DF0-9556-E2F88CE79ECA@.microsoft.com...
> Just wondering if someone could provide an example of passing a variable
from
> the code behind to javascript in vb. I want to have one control have
focus
> with the page loading with one condition and have another control with
focus
> after the page loads from a different condition. A gets set in the code
> aspx.vb file and the script might look something like,
> <script language="javascript">
> function DoHighlight(a)
> {
> if a = 1 {
> document.Form1.dr_lst_system.focus();
> }
> else
> {
> Form1.textbox1.focus();
> }
> }
> </script>
>
> thanks.
> --
> Paul G
> Software engineer.
Hi thanks for the response. Here is what I have but it does not seem to be
working. In the Java script I have
<form id="Form1" method="post" runat="server">
<input type="hidden" runat="server" id="inhA" NAME="inhA">
<script language="javascript" event="onload" for="window">
if (inhA.value == 0)
{Form1.dr_tx_names.focus();}
if (inhA.value == 1)
{Form1.dr_lst_system.focus();}
</script>and in the aspx.vb file I have
in page load event
If Not Page.IsPostBack Then 'only first time page loads
Me.inhA.Value = 0
and in a dropdown event select-causes the page to reload I have
Me.inhA.Value = 1
**************so I want one control two have focus if inhA = 0 and the other
to have focus if inhA = 1.
thanks for the information.
--
Paul G
Software engineer.

"Eliyahu Goldin" wrote:

> Paul,
> This is normally done with hidden input controls. Put a line
> <input type=hidden runat=server id=inhA>
> in the .aspx file. If you switch from Html view to Design and back, you will
> find in the code-behind a definition for inhA as an obyect of type
> System.Web.UI.HtmlControls.HtmlInputHidden.
> You can now set inhA.Value="1" on server side and check if (inhA.value ==
> "1") on client side.
> Eliyahu
> "Paul" <Paul@.discussions.microsoft.com> wrote in message
> news:1BB64CF9-9547-4DF0-9556-E2F88CE79ECA@.microsoft.com...
> > Just wondering if someone could provide an example of passing a variable
> from
> > the code behind to javascript in vb. I want to have one control have
> focus
> > with the page loading with one condition and have another control with
> focus
> > after the page loads from a different condition. A gets set in the code
> > aspx.vb file and the script might look something like,
> > <script language="javascript">
> > function DoHighlight(a)
> > {
> > if a = 1 {
> > document.Form1.dr_lst_system.focus();
> > }
> > else
> > {
> > Form1.textbox1.focus();
> > }
> > }
> > </script>
> > thanks.
> > --
> > Paul G
> > Software engineer.
>
On Wed, 29 Jun 2005 11:02:04 -0700, Paul wrote:

> Just wondering if someone could provide an example of passing a variable from
> the code behind to javascript in vb. I want to have one control have focus
> with the page loading with one condition and have another control with focus
> after the page loads from a different condition. A gets set in the code
> aspx.vb file and the script might look something like,
> <script language="javascript">
> function DoHighlight(a)
> {
> if a = 1 {
> document.Form1.dr_lst_system.focus();
> }
> else
> {
> Form1.textbox1.focus();
> }
> }
> </script>
>
> thanks.
Something like
if(<%=a%> = 1) will work (a is a server-side variable)
ok thanks for the information.
--
Paul G
Software engineer.

"intrader" wrote:

> On Wed, 29 Jun 2005 11:02:04 -0700, Paul wrote:
> > Just wondering if someone could provide an example of passing a variable from
> > the code behind to javascript in vb. I want to have one control have focus
> > with the page loading with one condition and have another control with focus
> > after the page loads from a different condition. A gets set in the code
> > aspx.vb file and the script might look something like,
> > <script language="javascript">
> > function DoHighlight(a)
> > {
> > if a = 1 {
> > document.Form1.dr_lst_system.focus();
> > }
> > else
> > {
> > Form1.textbox1.focus();
> > }
> > }
> > </script>
> > thanks.
> Something like
> if(<%=a%> = 1) will work (a is a server-side variable)

0 comments:

Post a Comment