[英]Edit visibility of a cell in GridView based on a value in the row
I'm trying to set a my delete button visibility to true when the value of the last cell in row is equal to the session value which is the userID 当行中最后一个单元格的值等于会话值(即用户ID)时,我试图将删除按钮的可见性设置为true
Girdview UPDATED: Girdview更新:
<Columns>
<asp:TemplateField>
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="attachID" HeaderText="Attachment ID" SortExpression="atID" HeaderStyle-CssClass="hidcol" ItemStyle-CssClass="hidcol"/>
<asp:BoundField DataField="attachmentTitle" HeaderText="Attachment Title" SortExpression="attachmentTitle" />
<asp:BoundField DataField="attachmentType" HeaderText="Attachment Type" SortExpression="attachmentType" />
<asp:BoundField DataField="attachmentDescription" HeaderText="Attachment Description" SortExpression="attachmentDescription" />
<asp:BoundField DataField="attachmentDate" HeaderText="Attachment Date" SortExpression="attachmentDate" />
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="download" CommandArgument='<%# Eval("attachmentFile") %>'><img src="CSS/images/download.png" alt="Download File" /></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:ImageButton ID="ImageButton1" runat="server" CausesValidation="False" CommandArgument='<%# Eval("userID") %>' CommandName="Delete" ImageUrl="~/CSS/images/delete_page.png" Text="Delete" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
I'm trying to set the Delete button property visibile if the userID
machates the session["username"]
如果userID
ID匹配session["username"]
我试图设置“删除”按钮属性
I'm not sure this is the right way or not. 我不确定这是否正确。
Code-Behind UPDATED: 后台代码更新:
String searchValue = "" + Session["username"];
foreach (GridViewRow row in GridView1.Rows)
{
// Only look in data rows
if (row.RowType == DataControlRowType.DataRow ||
row.RowType == DataControlRowType.EmptyDataRow)
{
// Find the delete button by ID
ImageButton theDeleteButton = row.FindControl("ImageButton1") as ImageButton;
// Verify the button was found before we try to use it
if (theDeleteButton != null)
{
if (theDeleteButton.CommandArgument != searchValue)
{
// Make the button invisible
theDeleteButton.Visible = false;
}
}
}
}
How can i make it happen ? 我该如何实现?
thanks in advance! 提前致谢!
The Code has been Updated 守则已更新
I recommend using a TemplateField
for the delete button with a CommandName
attribute (similar to what you are doing with the download link button) rather than a CommandField
, because it will make your code for hiding the delete button cleaner, like this: 我建议使用带有CommandName
属性(类似于您使用下载链接按钮的操作)的Delete按钮而不是CommandField
的TemplateField
,因为它会使您隐藏删除按钮的代码更加简洁,如下所示:
Markup: 标记:
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="ButtonDelete" runat="server" CommandName="delete"
CommandArgument='<%# Eval("id") %>' Text="Delete" />
</ItemTemplate>
</asp:TemplateField>
Code-behind: 代码隐藏:
foreach (GridViewRow row in GridView1.Rows)
{
// Only look in data rows
if (row.RowType == DataControlRowType.DataRow ||
row.RowType == DataControlRowType.EmptyDataRow)
{
// Find the delete button by ID
Button theDeleteButton = row.FindControl("ButtonDelete") as Button;
// Verify the button was found before we try to use it
if(theDeleteButton != null)
{
// Make the button invisible
theDeleteButton.Visible = false;
}
}
}
Note: The as
operator will return null
if the attempted cast cannot be completed successfully; 注意:如果尝试的强制转换无法成功完成,则as
运算符将返回null
否则,返回0。 hence the check for null
against theDeleteButton
variable. 因此,针对theDeleteButton
变量检查是否为null
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.