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

时间:2011-09-20 06:37:51

标签: javascript .net asp.net gridview itemtemplate

我的GridView中有很多模板字段。其中我有一个名为“chkSelect”的CheckBox,在另一个模板字段中我有一个名为“lnkgvQCAttribute”的LinkBut​​ton。这里我想在CheckBox未选中时禁用LinkBut​​ton,并在选中CheckBox时启用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>

我需要你的所有建议......

3 个答案:

答案 0 :(得分:2)

首先添加对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类的元素,并对其进行控制:

<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>

享受!

答案 1 :(得分:1)

在复选框

上拨打onclick="disableLinkButton(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;  
}

答案 2 :(得分:1)

在页面上添加以下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>