views:

867

answers:

5

How do I handle the click of a checkbox to show another control preferrably a user conrtrol (ASP.NET ) dynamically.

A: 

I don't know anything about the system you're using but the low down dirty way I'd do this is...

Stick the user control on a blank page of it's own.

When the checkbox is clicked, have JQuery go get the HTML content of the page the user control is on and stick it in the div.

This will result in not-so-neat html in the calling page though.

And is this an asp:CheckBox or an html input?

Logicalunatic
I am using ASP.NET user control.
Greens
A: 

Are you looking for something like this:

$(function(){
 $('#Control').hide(); //initially hide the control
 $('#checkbox').click(function(){  // bind the checkbox click event
 if ($('#checkbox').attr('checked')) {
  $('#Control').show(); 
 }
 });
});


<mycontrol:UC id="Control" runat="server" />
TStamper
since you're using asp.net I would recommend using class names selectors instead of id selectors, because ASP.NET tends to change the id name when rendered
TStamper
Depending on the size or the page and/or the user's connection speed, you may get a flash of the control you hide. Hide it with CSS instead.
Adrian Lynch
true it does depends on the speed of your the users connection, so if you want to keep the suprise suspense then hide it with css as style:hidden
TStamper
A: 

If you want to create a new object, you can do this:

var checkbox = $("<input type='checkbox' ... />");
$('div#someID').append(checkbox);

Though it sounds like you perhaps want to get the data to append from an AJAX call. I can't quite tell from the question.

Kit
A: 

Assuming you hide the control with CSS by default you could shorten TStamper's code to something like:

$(function() {
    $('#checkbox').click(function() {
     $('#control').toggle();
    });
});

<mycontrol:UC id="control" runat="server" />
Adrian Lynch
How do I implement this with a checkbox list?.
Greens
A: 

I'm partial to using jquery's taconite plugin. It enables you to return multiple controls from a single ajax call. For a simple show/hide control scenario rendering a hidden control on a page is good enough. If your control is big or changes due to user actions then your best bet is rendering control on the server and using js to update DOM. If you're using jquery for your DOM updates and wish to find control by id use:

$("[id$=controlId]")

This will locate your control even with asp.net prefixes to the id. I'm working on a simple c# wrapper for the taconite plugin which should enable you to use the plugin more easily (sample web site coming soon).

Goran