gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 11:34:33 24 4
gpt4 key购买 nike

我有一个包含三列的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com