【问题标题】:Adding runat server to HTML control giving error将 runat 服务器添加到 HTML 控件给出错误
【发布时间】:2016-03-07 04:16:07
【问题描述】:

我想在服务器端访问 aHTML control,但是在添加 runat= server 时,它给了我错误

'chkA1' 不是有效标识符。

这是我的html

<input type="checkbox" runat="server" id="chkA1<%# (Container.RecordIndex)%>" name="chkAC1" style="width: 17px; 
                                                height: 17px;" value="<%# (Container.RecordIndex)%>" onclick="AppoveCheckA(this)" />

当我删除 runat=server 时,它可以正常工作。

为什么它不适用于runat=server。任何建议

更新

ID 会随着复选框的变化而变化。这是我写的代码

var checkBoxID;
    var status;
    var arrA = [];
    var arrB = [];
    var rowIndex
    function AppoveCheckA(chk) {
        status = true;
        checkBoxID = $(chk).attr("id");
        funClientSelectAfter(status, checkBoxID);
    }
    function AppoveCheckB(chk) {
        status = true;
        checkBoxID = $(chk).attr("id");
        funClientSelectAfter1(status, checkBoxID);
    }

    function funClientSelectAfter(status, checkBoxID) {
        if (status && checkBoxID != null) {
            var len = checkBoxID.length;

            if (len >= 7) {
                rowIndex = checkBoxID.substring(checkBoxID.length - 2, checkBoxID.length);
            }
            else {
                rowIndex = checkBoxID.substring(checkBoxID.length - 1, checkBoxID.length);

            }
            for (var col = 1; col <= 4; col++) {
                if (("chkA" + col + rowIndex) == checkBoxID) {
                    if (document.getElementById("chkA" + col + rowIndex).checked == true) {
                        $("#chkA" + col + rowIndex).attr("checked", true);
                        arrA[rowIndex] = col;
                        continue;
                    }
                    else
                        arrA[rowIndex] = 0
                }
                $("#chkA" + col + rowIndex).attr("checked", false);
            }
            document.getElementById("hidRateA").value = arrA.join();
            // alert(document.getElementById("hidRateA").value);
        }
    }

【问题讨论】:

    标签: html asp.net


    【解决方案1】:

    ID 将成为 C# 中的强类型标识符。因此 ID 不能更改,也不能包含无效字符。

    简单的解决方案是从您的 id 属性中删除它。

    <input type="checkbox" runat="server" id="chkA1" name="chkAC1" style="width: 17px; height: 17px;" value="<%# (Container.RecordIndex)%>" onclick="AppoveCheckA(this)" />
    

    【讨论】:

    • id's 正在发生变化,但是 gridview 中有很多行,并且 id 也会相应地发生变化
    • @coder 将访问此输入的代码添加到您的问题中。
    • @coder 服务器端代码呢?在客户端,您不必通过 ID 访问它。你可以使用 CSS 类甚至是其他属性的值(例如 value)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-27
    • 2012-11-14
    • 1970-01-01
    • 2011-07-25
    • 1970-01-01
    • 1970-01-01
    • 2013-01-03
    相关资源
    最近更新 更多