当选中同一 GridView 中的 CheckBox 时,如何启用和禁用 GridView 中的 LinkBut​​ton?

发布于 2024-12-05 08:29:19 字数 5894 浏览 1 评论 0原文

我的 GridView 中有许多模板字段。其中我有一个名为“chkSelect”的复选框,在另一个模板字段中我有一个名为“lnkgvQCAttribute”的 LinkBut​​ton。在这里,我想在未选中复选框时禁用 LinkBut​​ton,并在选中复选框时启用 LinkBut​​ton。如何使用 JavaScript 来做到这一点?

这是我的 GridView 专栏:

<Columns>
    <asp:TemplateField HeaderText="S.No.">
        <ItemTemplate>
            <asp:Label ID="lblSNo" runat="server" Text='<%# Container.DataItemIndex + 1 %>'></asp:Label>
        </ItemTemplate>
        <ItemStyle Width="25px" />
        <ControlStyle Width="25px" />
    </asp:TemplateField>
    <asp:TemplateField>
        <ItemTemplate>
            <asp:CheckBox ID="chkSelect" runat="server" Checked='<%#Bind("Select") %>' onClick="CheckedTotal();" />
        </ItemTemplate>
        <HeaderTemplate>
            <asp:CheckBox ID="chkSelectAll" runat="server" onclick="CheckAll();" />
        </HeaderTemplate>
        <ControlStyle Width="20px" />
        <ItemStyle Width="20px" />
    </asp:TemplateField>
    <asp:BoundField DataField="PurchaseID" HeaderText="PurchaseID" Visible="false" />
    <asp:BoundField DataField="POID" HeaderText="POID" Visible="false" />
    <asp:BoundField DataField="PurchaseDetailID" HeaderText="PurchaseDetailID" Visible="false" />
    <asp:TemplateField HeaderText="ItemID" Visible="false">
        <ItemTemplate>
            <asp:Label ID="lblgvItemID" runat="server" Text='<%# Bind("ItemID") %>' />
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Item Description" SortExpression="ItemDescription">
        <ItemTemplate>
            <asp:Label ID="lblItemDesc" runat="server" Text='<%# Bind("ItemDescription") %>' Style="display: none"></asp:Label>
            <asp:TextBox ID="txtItemDesc" runat="server" Text='<%# Bind("ItemDescription") %>'   Font-Size="Smaller" Enabled="false"></asp:TextBox>
        </ItemTemplate>
        <ControlStyle Width="200px" />
        <ItemStyle Width="200px" />
    </asp:TemplateField>
    <asp:BoundField DataField="SpecificationName" HeaderText="Specification Name" SortExpression="SpecificationName">
        <ItemStyle HorizontalAlign="Left" />
    </asp:BoundField>
    <asp:TemplateField HeaderText="RD">
        <ItemTemplate>
            <asp:Label ID="lblgvRD" runat="server" Text='<%# Bind("RandomDimension") %>'></asp:Label></ItemTemplate>
        <ItemStyle Width="40px" />
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Planned Quantity" SortExpression="PlannedQuantity">
        <ItemTemplate>
            <asp:Label ID="lblPlannedQuantity" runat="server" Text='<%# Bind("PlannedQuantity") %>' />
            <asp:TextBox ID="txtPlannedQuantity" runat="server" Style="display: none" Text='<%# Bind("PlannedQuantity") %>' />
        </ItemTemplate>
        <ControlStyle Width="60px" />
        <ItemStyle Width="60px" />
    </asp:TemplateField>
    <asp:TemplateField HeaderText="OnOrder Quantity" SortExpression="OnOrderQuantity">
        <ItemTemplate>
            <asp:Label ID="lblOnOrderQuantity" runat="server" Text='<%# Bind("OnOrderQuantity") %>' />
            <asp:TextBox ID="txtOnOrderQuantity" runat="server" Style="display: none" Text='<%# Bind("OnOrderQuantity") %>' />
        </ItemTemplate>
        <ControlStyle Width="60px" />
        <ItemStyle Width="60px" />
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Order Quantity" SortExpression="OrderQuantity">
        <ItemTemplate>
            <asp:TextBox ID="txtOrderQuantity" runat="server" MaxLength="10" onkeypress="return DecimalValidate(event);" onkeyup="return calculateTotal();" Text='<%# Bind("OrderQuantity") %>' Enabled="false" />
        </ItemTemplate>
        <ControlStyle Width="60px" />
        <ItemStyle Width="60px" />
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Unit Rate" SortExpression="UnitRate">
        <ItemTemplate>
            <asp:Label ID="lblUnitRate" runat="server" Style="display: none" Text='<%# Bind("UnitRate") %>' />
             <asp:TextBox ID="txtUnitRate" runat="server" onkeypress="return DecimalValidate(event);" onkeyup="return calculateTotal();" Text='<%# Bind("UnitRate") %>' Enabled="false />
         </ItemTemplate>
         <ControlStyle Width="90%" />
         <ItemStyle Width="80px" />
     </asp:TemplateField>
     <asp:TemplateField HeaderText="Total Amount" SortExpression="TotalAmount">
         <ItemTemplate>
             <asp:TextBox ID="txtTotalAmt" runat="server" contentEditable="false" onclick="this.blur();" Style="background-color: Transparent; border: 0px; cursor: default" Text='<%# Bind("TotalAmount") %>' />
        </ItemTemplate>
        <ControlStyle Width="60px" />
        <ItemStyle Width="60px" />
    </asp:TemplateField>
    <asp:TemplateField HeaderText="ItemStatusID" Visible="false">
        <ItemTemplate>
            <asp:Label ID="lblgvItemStatusID" runat="server" Text='<%# Bind("ItemStatusID") %>' />
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="QC">
        <ItemTemplate>
            <asp:LinkButton ID="lnkgvQCAttribute" runat="server" CommandArgument='<%# DataBinder.Eval(Container, "RowIndex") %>' CommandName="QC" Text="QC" />
        </ItemTemplate>
    </asp:TemplateField>
</Columns>

请我需要您的所有建议...

I have a number of Template Fields in my GridView. In which I have One CheckBox called "chkSelect" in it and in another Template Field I have one LinkButton called "lnkgvQCAttribute". Here I want to Disable the LinkButton when the CheckBox is Unchecked and Enable the LinkButton when the CheckBox is Checked. How to do this using JavaScript?

Here is my GridView's columns:

<Columns>
    <asp:TemplateField HeaderText="S.No.">
        <ItemTemplate>
            <asp:Label ID="lblSNo" runat="server" Text='<%# Container.DataItemIndex + 1 %>'></asp:Label>
        </ItemTemplate>
        <ItemStyle Width="25px" />
        <ControlStyle Width="25px" />
    </asp:TemplateField>
    <asp:TemplateField>
        <ItemTemplate>
            <asp:CheckBox ID="chkSelect" runat="server" Checked='<%#Bind("Select") %>' onClick="CheckedTotal();" />
        </ItemTemplate>
        <HeaderTemplate>
            <asp:CheckBox ID="chkSelectAll" runat="server" onclick="CheckAll();" />
        </HeaderTemplate>
        <ControlStyle Width="20px" />
        <ItemStyle Width="20px" />
    </asp:TemplateField>
    <asp:BoundField DataField="PurchaseID" HeaderText="PurchaseID" Visible="false" />
    <asp:BoundField DataField="POID" HeaderText="POID" Visible="false" />
    <asp:BoundField DataField="PurchaseDetailID" HeaderText="PurchaseDetailID" Visible="false" />
    <asp:TemplateField HeaderText="ItemID" Visible="false">
        <ItemTemplate>
            <asp:Label ID="lblgvItemID" runat="server" Text='<%# Bind("ItemID") %>' />
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Item Description" SortExpression="ItemDescription">
        <ItemTemplate>
            <asp:Label ID="lblItemDesc" runat="server" Text='<%# Bind("ItemDescription") %>' Style="display: none"></asp:Label>
            <asp:TextBox ID="txtItemDesc" runat="server" Text='<%# Bind("ItemDescription") %>'   Font-Size="Smaller" Enabled="false"></asp:TextBox>
        </ItemTemplate>
        <ControlStyle Width="200px" />
        <ItemStyle Width="200px" />
    </asp:TemplateField>
    <asp:BoundField DataField="SpecificationName" HeaderText="Specification Name" SortExpression="SpecificationName">
        <ItemStyle HorizontalAlign="Left" />
    </asp:BoundField>
    <asp:TemplateField HeaderText="RD">
        <ItemTemplate>
            <asp:Label ID="lblgvRD" runat="server" Text='<%# Bind("RandomDimension") %>'></asp:Label></ItemTemplate>
        <ItemStyle Width="40px" />
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Planned Quantity" SortExpression="PlannedQuantity">
        <ItemTemplate>
            <asp:Label ID="lblPlannedQuantity" runat="server" Text='<%# Bind("PlannedQuantity") %>' />
            <asp:TextBox ID="txtPlannedQuantity" runat="server" Style="display: none" Text='<%# Bind("PlannedQuantity") %>' />
        </ItemTemplate>
        <ControlStyle Width="60px" />
        <ItemStyle Width="60px" />
    </asp:TemplateField>
    <asp:TemplateField HeaderText="OnOrder Quantity" SortExpression="OnOrderQuantity">
        <ItemTemplate>
            <asp:Label ID="lblOnOrderQuantity" runat="server" Text='<%# Bind("OnOrderQuantity") %>' />
            <asp:TextBox ID="txtOnOrderQuantity" runat="server" Style="display: none" Text='<%# Bind("OnOrderQuantity") %>' />
        </ItemTemplate>
        <ControlStyle Width="60px" />
        <ItemStyle Width="60px" />
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Order Quantity" SortExpression="OrderQuantity">
        <ItemTemplate>
            <asp:TextBox ID="txtOrderQuantity" runat="server" MaxLength="10" onkeypress="return DecimalValidate(event);" onkeyup="return calculateTotal();" Text='<%# Bind("OrderQuantity") %>' Enabled="false" />
        </ItemTemplate>
        <ControlStyle Width="60px" />
        <ItemStyle Width="60px" />
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Unit Rate" SortExpression="UnitRate">
        <ItemTemplate>
            <asp:Label ID="lblUnitRate" runat="server" Style="display: none" Text='<%# Bind("UnitRate") %>' />
             <asp:TextBox ID="txtUnitRate" runat="server" onkeypress="return DecimalValidate(event);" onkeyup="return calculateTotal();" Text='<%# Bind("UnitRate") %>' Enabled="false />
         </ItemTemplate>
         <ControlStyle Width="90%" />
         <ItemStyle Width="80px" />
     </asp:TemplateField>
     <asp:TemplateField HeaderText="Total Amount" SortExpression="TotalAmount">
         <ItemTemplate>
             <asp:TextBox ID="txtTotalAmt" runat="server" contentEditable="false" onclick="this.blur();" Style="background-color: Transparent; border: 0px; cursor: default" Text='<%# Bind("TotalAmount") %>' />
        </ItemTemplate>
        <ControlStyle Width="60px" />
        <ItemStyle Width="60px" />
    </asp:TemplateField>
    <asp:TemplateField HeaderText="ItemStatusID" Visible="false">
        <ItemTemplate>
            <asp:Label ID="lblgvItemStatusID" runat="server" Text='<%# Bind("ItemStatusID") %>' />
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="QC">
        <ItemTemplate>
            <asp:LinkButton ID="lnkgvQCAttribute" runat="server" CommandArgument='<%# DataBinder.Eval(Container, "RowIndex") %>' CommandName="QC" Text="QC" />
        </ItemTemplate>
    </asp:TemplateField>
</Columns>

Please I need all your suggestions...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

待"谢繁草 2024-12-12 08:29:20

首先添加对 jQuery 的引用(下载它并将其放入站点文件夹之一,例如 Scripts):

<script src="Scripts/jquery-1.2.6.pack.js" type="text/javascript"></script>

然后您应该为元素创建一个 CssClass 名称,例如:

<asp:TemplateField>
    <ItemTemplate>
        <asp:CheckBox ID="chkSelect" runat="server" Checked='<%#Bind("Select") %>' CssClass="chkSelect" />
    </ItemTemplate>
    <HeaderTemplate>
        <asp:CheckBox ID="chkSelectAll" runat="server" onclick="CheckAll();" />
    </HeaderTemplate>
    <ControlStyle Width="20px" />
    <ItemStyle Width="20px" />
</asp:TemplateField>

现在

<asp:TemplateField HeaderText="QC">
    <ItemTemplate>
        <asp:LinkButton ID="lnkgvQCAttribute" runat="server" CommandArgument='<%# DataBinder.Eval(Container, "RowIndex") %>'
                        CommandName="QC" Text="QC" CssClass="lnkgvQCAttribute" />
    </ItemTemplate>
</asp:TemplateField>

,您可以使用 JS 找到您的元素并控制它们的行为。在本例中,复选框位于 span 中,然后位于 td 中,最后位于 tr 中。您应该导航到 tr,在 tr 中找到具有 lnkgvQCAttribute 类的 a 元素,并控制它:

<script type="text/javascript">
    $(document).ready(function () {
        $("span.chkSelect input").click(function () {
            var enabled = this.checked;
            var link = $(this).parent().parent().parent().find("a.lnkgvQCAttribute");
            if (enabled) {
                var href = $(link).attr("data-href");
                $(link).attr("href", href);
                $(link).removeAttr("data-href");
            } else {
                var href = $(link).attr("href");
                $(link).attr("data-href", href);
                $(link).removeAttr("href");
            }
        });
    });
</script>

享受吧!

First add a reference to jQuery (download it and put it in one of site-folders, for example Scripts):

<script src="Scripts/jquery-1.2.6.pack.js" type="text/javascript"></script>

Then you should create a CssClass name for your elements, like:

<asp:TemplateField>
    <ItemTemplate>
        <asp:CheckBox ID="chkSelect" runat="server" Checked='<%#Bind("Select") %>' CssClass="chkSelect" />
    </ItemTemplate>
    <HeaderTemplate>
        <asp:CheckBox ID="chkSelectAll" runat="server" onclick="CheckAll();" />
    </HeaderTemplate>
    <ControlStyle Width="20px" />
    <ItemStyle Width="20px" />
</asp:TemplateField>

and

<asp:TemplateField HeaderText="QC">
    <ItemTemplate>
        <asp:LinkButton ID="lnkgvQCAttribute" runat="server" CommandArgument='<%# DataBinder.Eval(Container, "RowIndex") %>'
                        CommandName="QC" Text="QC" CssClass="lnkgvQCAttribute" />
    </ItemTemplate>
</asp:TemplateField>

Now, you can find your elements with JS and control their behavior. In this case, the checkbox is in a span, then in a td, and finally a tr. You should navigate to the tr, in the tr find the a element that have lnkgvQCAttribute class, and control it:

<script type="text/javascript">
    $(document).ready(function () {
        $("span.chkSelect input").click(function () {
            var enabled = this.checked;
            var link = $(this).parent().parent().parent().find("a.lnkgvQCAttribute");
            if (enabled) {
                var href = $(link).attr("data-href");
                $(link).attr("href", href);
                $(link).removeAttr("data-href");
            } else {
                var href = $(link).attr("href");
                $(link).attr("data-href", href);
                $(link).removeAttr("href");
            }
        });
    });
</script>

Enjoy!

柠檬色的秋千 2024-12-12 08:29:20

在复选框上调用 onclick="disableLinkBut​​ton(this)"

function disableLinkButton(obj) {
    var rowObject = getParentRow(obj);
    if(obj.checked) {             
        rowObject.childNodes[3].disabled = true // debug and check, row Object will contain that link button as its childnodes        
    }
    else {
        rowObject.childNodes[3].disabled = false;
    }
}

function getParentRow(obj) {
    obj = obj.parentElement;
    while(obj.tagName != "TR")
    return obj;  
}

Call onclick="disableLinkButton(this)" on checkbox

function disableLinkButton(obj) {
    var rowObject = getParentRow(obj);
    if(obj.checked) {             
        rowObject.childNodes[3].disabled = true // debug and check, row Object will contain that link button as its childnodes        
    }
    else {
        rowObject.childNodes[3].disabled = false;
    }
}

function getParentRow(obj) {
    obj = obj.parentElement;
    while(obj.tagName != "TR")
    return obj;  
}
ˉ厌 2024-12-12 08:29:20

将以下 javascript 和样式添加到页面上:

<script type="text/javascript">
     $(function () {
          $("input[id*='chkSelect_']", "#<%= GridView1.ClientID %>")
          .click(function () {
               var linkButton = $(this).closest("tr").find("a[id*='lnkgvQCAttribute_']");

               if (this.checked) {
                    EnableLinkButton(linkButton);
               }
               else {
                    DisableLinkButton(linkButton);
               }
          }) //disable turned off linkbuttons on page load
          .not(":checked").each(function () {
               DisableLinkButton($(this).closest("tr").find("a[id*='lnkgvQCAttribute_']"));
          });
     });

     function EnableLinkButton(lb) {
          $(lb)
          .removeClass("disabled")
          .attr("href", function () {
               return !$(this).is("[href^='javascript:__doPostBack']") ? this.href.replace('retun false;', '__doPostBack') : this.href;
          });
     }

     function DisableLinkButton(lb) {
          $(lb)
          .addClass("disabled")
          .attr("href", function () { return $(this).is("[href^='javascript:__doPostBack']") ? this.href.replace('__doPostBack', 'retun false;') : this.href; });
     }
</script>
<style type="text/css">
     a.disabled
     {
          color: #e3e3e3;
          text-decoration: none;
          cursor: default;
     }
</style>

Add following javascript and style onto the page:

<script type="text/javascript">
     $(function () {
          $("input[id*='chkSelect_']", "#<%= GridView1.ClientID %>")
          .click(function () {
               var linkButton = $(this).closest("tr").find("a[id*='lnkgvQCAttribute_']");

               if (this.checked) {
                    EnableLinkButton(linkButton);
               }
               else {
                    DisableLinkButton(linkButton);
               }
          }) //disable turned off linkbuttons on page load
          .not(":checked").each(function () {
               DisableLinkButton($(this).closest("tr").find("a[id*='lnkgvQCAttribute_']"));
          });
     });

     function EnableLinkButton(lb) {
          $(lb)
          .removeClass("disabled")
          .attr("href", function () {
               return !$(this).is("[href^='javascript:__doPostBack']") ? this.href.replace('retun false;', '__doPostBack') : this.href;
          });
     }

     function DisableLinkButton(lb) {
          $(lb)
          .addClass("disabled")
          .attr("href", function () { return $(this).is("[href^='javascript:__doPostBack']") ? this.href.replace('__doPostBack', 'retun false;') : this.href; });
     }
</script>
<style type="text/css">
     a.disabled
     {
          color: #e3e3e3;
          text-decoration: none;
          cursor: default;
     }
</style>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文