passing variable from code behind to function in javascript

G

Guest

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.
 
K

Kevin Spencer

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.
 
E

Eliyahu Goldin

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
 
G

Guest

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 said:
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
 
I

intrader

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)
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top