我正在开发一个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>
我有一个标签&#34; lblStatus&#34;它将是0或1.如果它的值为1,那么我想看到false该特定行的更新和编辑命令按钮。我搜索了很多,但没有找到任何完美的解决方案。请帮助我。
答案 0 :(得分:2)
在这种情况下,我会将CommandField转换为TemplateFields。为此,您将在设计视图中单击gridview中的智能标记。这样做是将命令字段更改为链接按钮,您可以在后面的代码中将其设置为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;
}
}
}
希望这会让你朝着正确的方向前进。我认为这应该可以解决问题。