【问题标题】:getting gridview row details from a link in the row从行中的链接获取 gridview 行详细信息
【发布时间】:2011-07-18 13:28:57
【问题描述】:

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

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

【问题讨论】:

    标签: c# .net asp.net gridview


    【解决方案1】:

    您可以使用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;
            }
         }
     }
    

    【讨论】:

    • 我收到错误:无法将“FileInfo”类型的对象转换为“System.Data.DataRowView”类型。
    • 你能看到/隐藏删除按钮吗?
    • 请发布您的完整代码。 Gridview 设计和绑定数据源。
    • 如果您的问题是我是否可以让链接不可见......那么是的,我可以做到。我在表中有一个 fileView 列,如果文件不是 txt 文件,那么我将链接设为 false 并且有效
    • 你是用数据表还是用 FileInfo 对象列表绑定网格?!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-22
    • 2012-02-29
    • 1970-01-01
    • 1970-01-01
    • 2022-08-19
    相关资源
    最近更新 更多