在javascript中的Gridview(ASP.NET/ASCX)中启用/禁用文本框

时间:2019-12-02 08:48:22

标签: javascript c# jquery asp.net

我只需要一些帮助即可。我希望在未选中复选框时禁用文本框!你能帮我吗另外,如果用户取消选中我希望再次启用该文本​​框(非常感谢)。这是我的代码:

    <asp:GridView ID="gvModifOuvrageNonControles" runat="server" AutoGenerateColumns="false" SkinID="MarionGridView">
        <Columns>
            <asp:BoundField DataField="MirePrincipal" HeaderText="OUVRAGE PRINCIPAL" />
            <asp:BoundField DataField="LibelleMireSecondaire" HeaderText="OUVRAGE SECONDAIRE" />
            <asp:TemplateField HeaderText="NON CONTROLE">
                <ItemTemplate>
                    <asp:CheckBox ID="cbInspection" OnClick="grisé(this);" runat="server" />
                </ItemTemplate>
            </asp:TemplateField> 
            <asp:BoundField DataField="Libellel" HeaderText="LIBELLE DES MS,VI,PI,SU,CP,PL,PF" />
            <asp:TemplateField HeaderText="RAISON">
                <ItemTemplate>
                    <asp:TextBox ID="txtCause"  runat="server"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
</div> ``'

<script type="text/javascript">

                function grisé(obj) {
                    var rowData = obj.parentNode.parentNode;
                    //0-based 
                    // use as: (YourTemplateFieldColumnWhichContainsThatTextBox -1)
                    rowData.cells[4].firstElementChild.disabled= true;
                    rowData.cells[2].firstElementChild.checked= false;
                }

</script>



1 个答案:

答案 0 :(得分:0)

<html>
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:GridView ID="gvModifOuvrageNonControles" runat="server" AutoGenerateColumns="false"
        SkinID="MarionGridView">
        <Columns>
            <asp:BoundField DataField="MirePrincipal" HeaderText="OUVRAGE PRINCIPAL" />
            <asp:BoundField DataField="LibelleMireSecondaire" HeaderText="OUVRAGE SECONDAIRE" />
            <asp:TemplateField HeaderText="NON CONTROLE">
                <ItemTemplate>
                    <asp:CheckBox ID="cbInspection" runat="server" />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="Libellel" HeaderText="LIBELLE DES MS,VI,PI,SU,CP,PL,PF" />
            <asp:TemplateField HeaderText="RAISON">
                <ItemTemplate>
                    <asp:TextBox ID="txtCause" runat="server"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>

    <script type="text/javascript">
        $(function() {
            //Enable Disable all TextBoxes when Header Row CheckBox is checked.
            $("[id*=cbInspection]").bind("click", function() {
                var cbInspection = $(this);

                //Find and reference the GridView.
                var grid = $(this).closest("table");

                //Loop through the CheckBoxes in each Row.
                $("td", grid).find("input[type=checkbox]").each(function() {

                    //If Header CheckBox is checked.
                    //Then check all CheckBoxes and enable the TextBoxes.
                    if (cbInspection.is(":checked")) {
                        $(this).attr("checked", "checked");
                        var td = $("td", $(this).closest("tr"));
                        td.css({ "background-color": "#D8EBF2" });
                        $("input[type=text]", td).removeAttr("disabled");
                    } else {
                        $(this).removeAttr("checked");
                        var td = $("td", $(this).closest("tr"));
                        td.css({ "background-color": "#FFF" });
                        $("input[type=text]", td).attr("disabled", "disabled");
                    }
                });
            });
        });
    </script>

    <script type="text/javascript">
        $(function() {
            //Enable Disable TextBoxes in a Row when the Row CheckBox is checked.
            $("[id*=chkRow]").bind("click", function() {

                //Find and reference the GridView.
                var grid = $(this).closest("table");

                //Find and reference the Header CheckBox.
                var cbInspection = $("[id*=cbInspection]", grid);

                //If the CheckBox is Checked then enable the TextBoxes in thr Row.
                if (!$(this).is(":checked")) {
                    var td = $("td", $(this).closest("tr"));
                    td.css({ "background-color": "#FFF" });
                    $("input[type=text]", td).attr("disabled", "disabled");
                } else {
                    var td = $("td", $(this).closest("tr"));
                    td.css({ "background-color": "#D8EBF2" });
                    $("input[type=text]", td).removeAttr("disabled");
                }

                //Enable Header Row CheckBox if all the Row CheckBoxes are checked and vice versa.
                if ($("[id*=chkRow]", grid).length == $("[id*=chkRow]:checked", grid).length) {
                    cbInspection.attr("checked", "checked");
                } else {
                    cbInspection.removeAttr("checked");
                }
            });
        });
    </script>

    </form>
</body>
</html>

这里:-我是Jquery,用于禁用和启用该Gridview中的文本框...

您可以点击此链接获取信息...。它将帮助您很多.....

https://www.aspsnippets.com/Articles/Enable-disable-TextBox-in-GridView-when-checkbox-is-selected-checked-in-ASPNet.aspx