【问题标题】:How to disable LinkButton within GridViewclick from Client Side using JavaScript?如何使用 JavaScript 从客户端禁用 GridViewclick 中的 LinkBut​​ton?
【发布时间】:2014-03-18 14:03:51
【问题描述】:

我有一个 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 Associate/Replace 否则他可以点击。

【问题讨论】:

    标签: c# javascript asp.net gridview


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      试试这个..

      您可以循环访问Gridview 并禁用特定控件...

      我正在用 javascript pageload 函数编写代码...

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

      设置RowCount为Gridview的行数...

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-27
        • 1970-01-01
        • 1970-01-01
        • 2011-02-17
        相关资源
        最近更新 更多