如何使用JavaScript从客户端禁用GridViewclick中的LinkButton

本文关键字:GridViewclick 中的 LinkButton 客户端 何使用 JavaScript | 更新日期: 2023-09-27 17:50:56

我有一个GridView,在一列我有LinkButton控件。我想禁用点击从客户端对这列的某些条件。意味着对于某些行,它将不可能为用户调用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文本设置为应用,在绑定时未应用。对于已应用状态的行,用户不应该能够单击LinkButton Associate/Replace,否则他可以单击。

如何使用JavaScript从客户端禁用GridViewclick中的LinkButton

使用此代码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;
    }
    }

试试这个…

您可以通过Gridview循环并使特定控件禁用…

我正在写javascript页面加载函数的代码…

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

设置RowCount为Gridview的行数…