禁用并启用复选框单击表格中的文本框

时间:2015-06-10 04:43:03

标签: javascript c# asp.net

我有填充数据的表格,然后我将复选框和文本框添加到其中。下面是我的代码:

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;");

但它不起作用。有错误说(无法设置属性'启用'未定义或空引用)

还有其他办法吗?

3 个答案:

答案 0 :(得分:1)

我认为没有"启用"文本框的属性只是因为它的名称是Pascal-case。 :)

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

答案 1 :(得分:0)

我认为你的身份是假的 我建议你使用jquery,它在语法上更短,更容易阅读。

答案 2 :(得分: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);
        }

关于订单的一切。不要手动使用ID,因为当您使用母版页或其他控件时,ID将在页面本身上更改。使用ClientID知道页面上控件的ID。

首先创建控件。然后将它们添加到您的表层次结构中。 你也许想放一个!在此之前。检查。否则,只要您的复选框被选中,您的文本框就会被禁用。

相关问题