asp:checkbox hide/show text/fields when it's clicked without doing a postback?

U

UJ

Is there a way with a asp:checkbox to run a JavaScript to display/hide
text/input on the screen without doing a postback?

I also need to be able to access the stuff at the server so I need to have
run=server with it.

TIA - Jeff.
 
G

Greg Young [MVP]

Yes you can add javascript to the control.

in your code behind you can do this through ...

Checkbox.Attributes.Add("onclick", "yourjavascriptfunction(this)")
 
O

Onwuka Emeka

Yes there are ways to do that. the asp:checkbox ultimately renders a an
<input type='checkbox'> html tag to the web browser .
if it were a html control you can do this by adding an onclick attibute to
the tag i.e. <input type='checkbox' onclick='someJavaScript();'> right?
you can also do that with an asp:CheckBox control by adding the attribute
key, value pair to the Attributes collection of the control thus:
CheckBox.Attributes.Add("attributename","attributevalue") .

copy, save and host the code below. The trick is in the OnInit method where
the onClick attribute of the checkbox is added.

<%@ Page language="c#" AutoEventWireup="false" %>
<HTML>
<HEAD>
<title>Javascript Show Hide</title>

<script language=javascript>
function showHide(checkbox,textboxId)
{
var textbox = document.getElementById(textboxId);
if(checkbox.checked)
{
textbox.style.display = '';
}
else
{
textbox.style.display = 'none';
}
}
</script>
<script language=C# runat=server>
override protected void OnInit(EventArgs e)
{
chbShowHide.Attributes.Add("onClick",string.Format("javascript:showHide(this,'{0}');",txtShowHide.ClientID));
base.OnInit(e);
}
</script>
</HEAD>
<body >
<form id="frmShowHide" method="post" runat="server">
<asp:TextBox id="txtShowHide"
runat="server"></asp:TextBox>
<asp:CheckBox id="chbShowHide" Checked=True
runat="server"></asp:CheckBox>
</form>
</body>
</HTML>
 
Joined
May 20, 2008
Messages
1
Reaction score
0
Object Required!!!

<script type ="text/javascript">
function DispTextBox(checkbox,textboxId)
{
var textbox = document.getElementById(textboxId);
if(checkbox.checked)
{
textbox.style.display = '';
}
else
{
textbox.style.display = 'none';
}
}
</script>
<script language="C#" runat="server">
override protected void OnInit(EventArgs e)
{
cb_others.Attributes.Add("onClick", string.Format("javascript:DispTextBox('{0}','{1}');", cb_others.ClientID ,txt_others.ClientID));
base.OnInit(e);
}
</script>


I wrote the following script to hide or display the text box when the checkbox is checked or unchecked with little modifications.

But I still get an error 'Object Required' which I couldn't sort out. Please help!
 
Joined
Apr 24, 2009
Messages
1
Reaction score
0
I found your post and I am having the same problem with the "Object Required" message in a WSS Content page that I am customizing. It seemed to work properly in a standalone asp.net web page as written, but not in WSS.

Does anyone have any suggestions or advice about where to find any help.
Thanks,8)

Mel
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top