Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > asp:checkbox hide/show text/fields when it's clicked without doing a postback?

Reply
Thread Tools

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

 
 
UJ
Guest
Posts: n/a
 
      04-26-2006
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.


 
Reply With Quote
 
 
 
 
Greg Young [MVP]
Guest
Posts: n/a
 
      04-26-2006
Yes you can add javascript to the control.

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

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

"UJ" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.
>
>



 
Reply With Quote
 
 
 
 
Onwuka Emeka
Guest
Posts: n/a
 
      04-26-2006
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","attribute value") .

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.Cl ientID));
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>



"UJ" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.
>
>



 
Reply With Quote
 
asharsyed asharsyed is offline
Junior Member
Join Date: May 2008
Posts: 1
 
      05-20-2008
<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("javascriptispTextBox('{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!
 
Reply With Quote
 
pamam pamam is offline
Junior Member
Join Date: Apr 2009
Posts: 1
 
      04-24-2009
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,

Mel
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to prevent a GridView's ButtonField from doing a postback when clicked? Rob Roberts ASP .Net 2 04-11-2006 06:44 PM
PythonWin: any way to delete all objects without exiting and without doing it with "del"? dananrg@yahoo.com Python 5 03-03-2006 05:12 PM
doing it without postback? Lasse Edsvik ASP .Net 3 09-22-2004 12:21 PM
Change url of supplied page without doing a redirect? Martin ASP .Net 4 06-25-2004 06:26 AM
HowTo clear all ViewState from a WebUserControl without doing a PostBack ? Andreas Klemt ASP .Net 3 08-28-2003 05:23 AM



Advertisments