client side script with server side controls

C

Cathryn Lindner

I have a web form that has a checkbox on it and a textbox associated
with it. I want the textbox to be displayed when the checkbox is checked
and I want this to happen on the client-side instead of posting back to
the server. The checkbox is a server control (asp:checkbox...). I can
write some codebehind that will display an alert message upon checking
the box that looks something like this...

Page_Load...

Dim s As String
Dim scriptString As New System.Text.StringBuilder

chkExpDate.Attributes.Add("onclick", "alertmsg()")
scriptString.Append("<script language=JavaScript> function alertmsg()
{")
scriptString.Append("alert('hi'); }<")
scriptstring.Append("/" + "script>"
s = scriptstring.ToString()

Page.RegisterStartupScript("startup", s)

End Sub

I used this as a test to make sure the server control was running the
script. But, when I change the script to assess the value of the server
control checkbox such as checked = true, I get errors and if I just try
to display the asp:textbox, it doesn't work either.

Any suggestions?
 
T

Thomas 'PointedEars' Lahn

Cathryn said:
I have a web form that has a checkbox on it and a textbox associated
with it. I want the textbox to be displayed when the checkbox is checked
and I want this to happen on the client-side instead of posting back to
the server.

<script type="text/javascript">
<!--
function showHide(
/** @argument object */ o,
/** @argument boolean */ b)
/**
* @author (C) 2003 Thomas Lahn &lt;[email protected]&gt;
* @argdescr o Object to show/hide.
* @argdescr b <code>true</code> shows the object,
* <code>false</code> or this argument
* left out hides it. CSS scripting
* support is required for this to work.
*/
{
if (o
&& typeof o.style != "undefined"
&& typeof o.style.visibility != "undefined")
o.style.visibility = b ? "visible" : "hidden";
}
//-->
</script>
<form ...>
...
<input
type="checkbox"
checked
...
onclick="showHide(this.form.elements['foobar'], this.checked);"
...
<input
name="foobar"
...>
The checkbox is a server control (asp:checkbox...).

You probably do not need it.
I can write some codebehind that will display an alert message upon
checking the box that looks something like this...

Page_Load...

Dim s As String
Dim scriptString As New System.Text.StringBuilder

chkExpDate.Attributes.Add("onclick", "alertmsg()")
scriptString.Append("<script language=JavaScript> function alertmsg()
{")
scriptString.Append("alert('hi'); }<")
scriptstring.Append("/" + "script>"
s = scriptstring.ToString()

Page.RegisterStartupScript("startup", s)

End Sub

That is VBScript, supported only by IE when client-side, and even not
required here.
I used this as a test to make sure the server control was running the
script.

Not the server control is running the JavaScript, the client's
JavaScript engine is. What you (are trying to) do here is merely
telling the server to include that script into the code sent to
the client.


PointedEars
 

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,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top