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
Girdview UPDATED:
<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"]
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:
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; hence the check for null
against theDeleteButton
variable.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.