GLP
GLP

Reputation: 3675

How to add onclientclick to a control that is dynamically created in the codebehind?

I have a checkbox that is dynamically created in the code-behind. I also need add a textbox, that is visible if the checkbox is checked. I know I can use

checkBox.Attributes.Add("onclick", "return func();").

But my question is how do I write the func() since my textbox is also created dynamically in the code-behind?

Update: Thanks for the answer. But if there any way not using JQuery? Also, my checkbox id is dynamically created in the code-behind.

Upvotes: 2

Views: 9080

Answers (2)

शेखर
शेखर

Reputation: 17614

You can do it by passing the id of the text box as follows

TextBox tx=new TextBox();
tx.Id="abc";
checkBox.Attributes.Add("onclick", "return func('"+checkBox.ClientId+"','"+tx.ClientId+"');");

and you can get the id in your java-script function.

function fun(checkboxId,textboxId)
{
    var remember= document.getElementById('checkboxId');
    if (remember.checked){
        document.getElementById(textboxId).disabled="disabled";
    }
    else{
         document.getElementById(textboxId).disabled="";
    } 
}

Upvotes: 3

Vaibs_Cool
Vaibs_Cool

Reputation: 6156

Look at this if helps you..I have used jquery

            $('.lettertyp').attr('checked', false);
            // Check current checkbox
            $(this).attr('checked', true);
            // Find the Sibling of the TextBox and SHow it
            $(this).parent().parent().find('input[type=text]').show();

JSFIDDLE

Upvotes: 0

Related Questions