【问题标题】:how to visible false edit commandbutton inside Gridview如何在 Gridview 中显示错误的编辑命令按钮
【发布时间】:2014-08-06 11:55:23
【问题描述】:

我正在开发一个 asp.net 应用程序。我在其中添加了一个用于编辑行的命令按钮。我的 Gridview 是

<asp:GridView ID="gvCustomerPaymentDetails" runat="server" 
  AutoGenerateColumns="false" ShowFooter="true" OnRowEditing="EditPayment" 
  OnRowUpdating="UpdatePayment" OnRowCancelingEdit="CancelEdit"
  CssClass="table table-bordered table-hover" Style="margin-left: 5px; margin-right: 5px;">
  <Columns>
       <asp:TemplateField HeaderText="ID">
           <ItemTemplate>
             <asp:Label ID="lblID" runat="server" Text='<%# Eval("ID")%>'></asp:Label>
           </ItemTemplate>
           <ItemStyle Width="2%" />
       </asp:TemplateField>
       <asp:TemplateField HeaderText="Pay Amount">
           <ItemTemplate>
               <asp:Label ID="lblPayAmount" runat="server" Text='<%# Eval("Pay_Amount")%>'></asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox ID="txtPayAmount" runat="server" style="width:100%"  Text='<%# Eval("Pay_Amount")%>'></asp:TextBox>
             </EditItemTemplate>
             <FooterTemplate>
                <asp:TextBox ID="txtPayAmount" style="width:100%" runat="server"></asp:TextBox>                                                </FooterTemplate>                                                
        </asp:TemplateField>

 <asp:TemplateField HeaderText="Action">
     <ItemTemplate>
        <asp:LinkButton ID="linkPayNow" runat="server" Text="Pay Now" CommandArgument='<%#Eval("ID") %>' CommandName="Pay"></asp:LinkButton>
        <asp:Label ID="txtStatus" runat="server" Text="Paid" Style="margin-left: 20px;" Visible="false"></asp:Label>
        <asp:LinkButton ID="lnkRemove" runat="server" CommandArgument='<%# Eval("ID")%>' OnClientClick="return confirm('Do you want to delete?')"
        Text="Delete" OnClick="DeletePayment"></asp:LinkButton>
     </ItemTemplate>
     <FooterTemplate>
       <asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="AddNewPayment" CommandName="Footer" />
     </FooterTemplate>
  </asp:TemplateField>
     <asp:CommandField ShowEditButton="True" />
</Columns>
 <EmptyDataTemplate>
     <tr>
      <th scope="col">Pay Amount
       </th>
      <th scope="col"></th>
     </tr>
     <tr>
      <td>
        <asp:TextBox ID="txtPayAmount" runat="server" />
     </td>
     <td>
       <asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="AddNewPayment" CommandName="EmptyDataTemplate" />
     </td>
     </tr>
   </EmptyDataTemplate>
</asp:GridView>

我有一个标签“lblStatus”,它将是 0 或 1。如果它的值为 1,那么我希望看到 false 的更新和编辑该特定行的命令按钮。我搜索了很多,但没有找到任何完美的解决方案。请帮助我。

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    在这种情况下,我会将 CommandField 转换为 TemplateFields。为此,您将在设计视图的网格视图中单击智能标签。这将做的是将命令字段更改为链接按钮,您可以在后面的代码中将其设置为 false。您需要为新的链接按钮提供正确的 ID,以便您可以在后面的代码中找到它们。然后你将创建一个数据绑定方法......就像......

    protected void GridView1_OnRowDataBound(object sender, GridViewRowEventArgs e)
    {
    
       //Code Here to Disable button. I'd use a Foreach loop like this.
       foreach(GridViewRow gvr in GridView1.Rows)
       {
            Label label = ((Label)gvr.FindControl("label"));
            LinkButton edit = ((LinkButton)gvr.FindControl("edit"));
            if (label.Text == 1)
            {
               edit.Visible = false;
            }
       }       
    }
    

    希望这能让您朝着正确的方向前进。我认为这应该可以解决问题。

    【讨论】:

    • 但是当我在 TemplateField 中使用它时,不会出现更新命令按钮,单击编辑后应该会自动出现。请建议我应该怎么做。
    • 应该,我刚刚测试了将 CommandFields 转换为 TemplateFields 并且能够单击编辑,并且显示了我的更新和取消链接按钮。您现在可以更新您的 aspx 命令字段/模板字段的样子吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多