Reputation: 47
I have a gridview table that has three columns..fileID, uploadedBy and delete. Only the owner of the file can delete the file. How can I validate that the person deleting the file is the owner of the file. I have the login credentials and I have the uploadedBy string. I can get the login credentials but I cannot get the uploadedBy column from the delete link that is clicked.
<asp:TemplateField HeaderText="View" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:HyperLink ID="lnkView" runat="server" NavigateUrl='<%# Eval("Id", "~/ViewFile.aspx?Id={0}") %>' Text="View"></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:HyperLinkField ItemStyle-HorizontalAlign="Center" DataNavigateUrlFields="Id" DataNavigateUrlFormatString="~/DeleteFile.aspx?Id={0}" HeaderText="Delete" Text="Delete" />
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
switch (e.Row.RowType)
{
case DataControlRowType.DataRow:
FileInfo myFileInfo = (FileInfo)e.Row.DataItem;
switch (myFileInfo.ContentType.ToLower())
{
case "image/pjpeg": // .jpg files
case "image/gif": // .gif files
case "application/msword": // .doc files
case "text/plain": // .txt files
case "application/vnd.ms-excel":
// Do nothing. When the row contains a viewable type,
// we want the View link to be enabled.
break;
default:
// Find the View link and disable it.
HyperLink myLink = (HyperLink)e.Row.FindControl("lnkView");
myLink.Enabled = false;
break;
}
break;
}
}
Upvotes: 2
Views: 1128
Reputation: 52241
You can use the RowDataBound
event and check UpdatedBy with the Current logined user. If it is not the same user, simply hide the delete button.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
System.Data.DataRow dr = ((System.Data.DataRowView)e.Row.DataItem).Row;
if (dr["uploadedBy"].ToString() != HttpContext.Current.User.Identity.Name)
{
((Button)e.Row.FindControl("btnDelete")).Visible = false;
}
}
}
Upvotes: 2