如何在使用JavaScript从客户端单击GridView中禁用链接按钮?

时间:2014-03-18 14:03:51

标签: c# javascript asp.net gridview

我有一个GridView,在一列中我有LinkBut​​ton控件。我想在此列上禁用从客户端点击某些条件的点击。某些行的含义用户无法调用onclick事件,而对于某些行则可能。 我想从客户端使用javascript实现这一点。 或者当用户单击链接时,它将通知用户此行无法完成此操作。

<asp:GridView Width="100%" ShowHeader="true" ViewStateMode="Enabled" GridLines="Both" CellPadding="10" CellSpacing="5" ID="GridViewMultiplePOSAssociationId" runat="server" AllowSorting="false" AutoGenerateColumns="false" OnRowCommand="RewardGridMultiD_RowCommand"
AllowPaging="true" PageSize="8" OnRowDataBound="grdViewCustomers_OnRowDataBound" PagerSettings-Position="Top" PagerSettings-Mode="NumericFirstLast" PagerSettings-FirstPageText="First" PagerSettings-LastPageText="Last" DataKeyNames="POS Id">
    <RowStyle CssClass="table_inner_text" BackColor="WhiteSmoke" BorderColor="CornflowerBlue" Wrap="true" ForeColor="Black" Height="30px" />
    <HeaderStyle CssClass="table_head_text" />
    <Columns>
        <asp:TemplateField ItemStyle-Width="80px">
            <ItemTemplate>
                <a href="JavaScript:divexpandcollapse('div<%# Eval(" POS Id ") %>');">
                    <img alt="Details" id="imgdiv<%# Eval("POS Id") %>" src="images/plus.png" />
                </a>
                <div id="div<%# Eval(" POS Id ") %>" style="display: none;">
                    <asp:GridView ID="grdViewOrdersOfCustomer" runat="server" AutoGenerateColumns="false" CssClass="ChildGrid">
                        <RowStyle CssClass="table_inner_text" BackColor="SkyBlue" BorderColor="Black" Wrap="true" ForeColor="White" Height="30px" />
                        <Columns>
                            <asp:BoundField ItemStyle-Width="150px" DataField="RULE FILE NAME" HeaderText="RULE FILE NAME" />
                            <asp:BoundField ItemStyle-Width="150px" DataField="RULE ID" HeaderText="RULE ID" />
                            <asp:BoundField ItemStyle-Width="150px" DataField="RULE TYPE ID" HeaderText="RULE TYPE ID" />
                            <asp:BoundField ItemStyle-Width="150px" DataField="START TIME" HeaderText="START TIME" />
                            <asp:BoundField ItemStyle-Width="150px" DataField="EXPIRY TIME" HeaderText="EXPIRY TIME" />
                        </Columns>
                    </asp:GridView>
                </div>
            </ItemTemplate>
        </asp:TemplateField>

        <asp:TemplateField HeaderStyle-Width="80px" ItemStyle-Width="80px" ItemStyle-HorizontalAlign="Center" HeaderText="Row Number">
            <ItemTemplate>
                <asp:Label ID="LabelRowNumberId" runat="server" Text='<%#Eval("Row Number") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderStyle-Width="80px" ItemStyle-Width="80px" ItemStyle-HorizontalAlign="Center" HeaderText="POS Id">
            <ItemTemplate>
                <asp:Label ID="LabelPOSID" runat="server" Text='<%#Eval("POS Id") %>'></asp:Label>
                <%-- <asp:LinkButton ID="LinkbtnPOSId" CommandArgument='<%#Eval("POS Id") %>' CommandName="ClickPOS" Text='<%#Eval("POS Id") %>' runat="server" CausesValidation="false"></asp:LinkButton>--%>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderStyle-Width="80px" ItemStyle-Width="250px" ItemStyle-HorizontalAlign="Center" HeaderText="Action">
            <ItemTemplate>
                <asp:LinkButton ID="HyperLinkAssociate" CommandArgument='<%#Eval("POS Id") %>' CommandName="Associate" Text="Associate" runat="server" OnClientClick="return OnClientClickAssociateRewardRuleFile(this);" CausesValidation="false"></asp:LinkButton>/
                <asp:LinkButton ID="HyperLinkReplace" CommandArgument='<%#Eval("POS Id") %>' CommandName="Replace" Text="Replace" runat="server" OnClientClick="return OnClientClickReplaceRewardRuleFile(this);" CausesValidation="false"></asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderStyle-Width="80px" ItemStyle-Width="250px" ItemStyle-HorizontalAlign="Center" HeaderText="Status">
            <ItemTemplate>
                <asp:Label runat="server" ID="LabelStatusPendingPOSId" Text='<%#Eval("Status") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

在GridViewMultiplePOSAssociationId中,有一列“Status”,其标签为LabelStatusPendingPOSId,LabelStatusPendingPOSId文本设置为“应用”,“绑定时未应用”。对于已应用状态的行,用户不能单击LinkBut​​ton关联/替换其他他可以单击。

2 个答案:

答案 0 :(得分:0)

为您的网格使用此代码OnRowDataBound

 protected void grdViewCustomers_OnRowDataBound(object sender, GridViewRowEventArgs e)
    { 
        if (e.Row.RowType==DataControlRowType.DataRow)
    {
    LinkButton LinkbtnPOSId=(LinkButton)e.Row.FindControl("LinkbtnPOSId");
    Label LabelStatusPendingPOSId = (Label)e.Row.FindControl("LabelStatusPendingPOSId");
    Boolean boolStatus = LabelStatusPendingPOSId.Text == "Applied" ? true : false;
    //LinkbtnPOSId.Attributes.Add("onclick", "function CheckStatus('" + boolStatus.ToString() + "')");   
    LinkbtnPOSId.Enabled = boolStatus;

    }

    }

答案 1 :(得分:0)

试试这个..

您可以遍历Gridview并使特定控件禁用...

我在javascript pageload函数中编写代码...

function PageLoad(sender, args) 
{
    for (var i = 0; i <= RowCount; i++) 
    {
        document.getElementById('Gridview1_HyperLinkAssociate_' + i.toString() + '').disabled = true;
    }
}

RowCount设置为Gridview的行数...