禁用和启用文本框,复选框单击表中
本文关键字:复选框 单击 启用 文本 | 更新日期: 2023-09-27 17:55:08
我用数据填充了表格,然后我在其中添加了复选框和文本框。 下面是我的代码:
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);
}
我试过:
cbox.Attributes.Add("onclick", "document.getElementById('textbox_" + count1 + "').Enabled=this.checked;");
但它不起作用。 有一个错误说(无法设置未定义或空引用的属性"已启用")
还有其他方法吗?
我不认为文本框有"启用"属性,因为它的名称在 Pascal-case 中。 :)
cbox.Attributes.Add("onclick", "document.getElementById('textbox_" + count1 + "').disabled = !this.checked;");
我认为你的ID是假的我建议你使用jquery,它在语法上更短,更容易阅读。
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);
}
一切都与订单有关。 不要手动使用 ID,因为当您使用母版页或其他控件时,ID 将在页面本身上更改。使用 ClientID 了解控件在页面上的 ID 是什么。
首先创建控件。然后将它们添加到表层次结构中。此外,您可能还想放一个 !在此之前。已检查。否则,一旦选中复选框,您的文本框将被禁用。