c# - 从行中的链接获取 gridview 行详细信息

标签 c# .net asp.net gridview

我有一个包含三列的 gridview 表..fileID、uploadedBy 和 delete。只有文件的所有者才能删除文件。我如何验证删除文件的人是文件的所有者。我有登录凭据,我有 uploadedBy 字符串。我可以获取登录凭据,但无法从单击的删除链接中获取 uploadedBy 列。

<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;
    }
  }

最佳答案

您可以使用 RowDataBound 事件并使用当前登录用户检查 UpdatedBy。如果不是同一用户,只需隐藏删除按钮即可。

 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;
        }
     }
 }

关于c# - 从行中的链接获取 gridview 行详细信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6733644/

相关文章:

c# - 在 C# 中抛出或不抛出异常

.net - Entity Framework 中的通用持久方法 - 插入或更新(如果存在)实体

c# - 嵌套类难题

javascript - 如果找到 Razor 处理程序,则调用 JS 函数

c# - 如何使用DirectSound从Stream播放MP3

c# - Unity3d未显示编译器错误

c# - 如何在.net core项目中添加View路由?

python - 更正 pb 文件以将 Tensorflow 模型移动到 ML.NET

c# - 3 层架构引用

c# - Asp.Net Core 重定向到 Action 但不允许直接调用 Action ?