Reputation: 100
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?
Upvotes: 0
Views: 343
Reputation: 41
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.
Upvotes: 0
Reputation: 238
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;");
Upvotes: 1
Reputation: 1379
i think your id is false I advice you using jquery it is syntaxicly shorter and easier to read.
Upvotes: 0