0

i have table populated with data then i add checkbox and textbox into it. below is my code:

int count1 = 0;
        TableCell tc;
        foreach (TableRow tr in Resource_TBL.Rows)
        {
            tr.Cells.Add(tc = new TableCell());
            CheckBox cbox = new CheckBox();
            cbox.ID = ""+count1;
            cbox.Attributes.Add("onclick", "document.getElementById('textbox_" + count1 + "').disabled=this.checked;");
            tc.Controls.Add(cbox);

            tr.Cells.Add(tc = new TableCell());
            TextBox tbox = new TextBox();
            tbox.ID = "textbox_" + count1;
            tbox.CssClass = "form-control";
            tbox.Enabled = false;
            tbox.Attributes.Add("placeholder", "Enter Detail Here");
            count1 += 1;
            tc.Controls.Add(tbox);
        }

i have tried :

cbox.Attributes.Add("onclick", "document.getElementById('textbox_" + count1 + "').Enabled=this.checked;");

but its not working. have an error saying(Unable to set property 'Enabled' of undefined or null reference)

is there any other way of doing it?

4
  • Can you try this cbox.Attributes.Add("onclick", "alert(document.getElementById('textbox_" + count1 + "'));"); Insted of cbox.Attributes.Add("onclick", "document.getElementById('textbox_" + count1 + "').Enabled=this.checked;");. and please comment the output. Commented Jun 10, 2015 at 5:11
  • @Dinesh Patra. nothing happen Commented Jun 10, 2015 at 6:11
  • is it throwing any error?? Commented Jun 10, 2015 at 6:33
  • @Dinesh Patra nope. nothing happen Commented Jun 10, 2015 at 6:44

3 Answers 3

1

I don't think there's an "Enabled" property for the textbox simply because its name is in Pascal-case. :)

cbox.Attributes.Add("onclick", "document.getElementById('textbox_" + count1 + "').disabled = !this.checked;");
Sign up to request clarification or add additional context in comments.

4 Comments

i got error JavaScript runtime error: Unable to set property 'disabled' of undefined or null reference
@HJK So it means the textbox by the ID does not exist. You may have to check the output HTML first to see the proper ID.
If I remembered correctly, the asp.net seems add some kind of suffix behind the control ID it generated.
thanks.. i saved me!! the code adding the id get MainContent_textbox_
0

i think your id is false I advice you using jquery it is syntaxicly shorter and easier to read.

Comments

0
int count1 = 0;
        TableCell tc;
        foreach (TableRow tr in Resource_TBL.Rows)
        {
            CheckBox cbox = new CheckBox();
            cbox.ID = ""+count1;
            TextBox tbox = new TextBox();
            tbox.ID = "textbox_" + count1;
            tbox.CssClass = "form-control";
            tbox.Enabled = false;
            tbox.Attributes.Add("placeholder", "Enter Detail Here");
            count1 += 1;
            cbox.Attributes.Add("onclick", "document.getElementById('" + tbox.ClientID + "').disabled=!this.checked;");
            tr.Cells.Add(tc = new TableCell());
            tc.Controls.Add(cbox);
            tr.Cells.Add(tc = new TableCell());
            tc.Controls.Add(tbox);
        }

Its all about the order. don't manually use the ID because when you use masterpage or other controls then the ID will change on the page itself. Use ClientID to know what the ID of the control is on the page.

Create controls first. then add them to your table hierachy later. Also you might want to put a ! before this.checked. else your textbox will be disabled as soon your checkbox gets selected.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.