【问题标题】:Add link image to GridView cells in column AFTER cell text将链接图像添加到单元格文本后列中的 GridView 单元格
【发布时间】:2012-10-19 02:50:17
【问题描述】:

对于我的网格视图中的“开始日期”列之一,如果用户具有正确的权限,我想添加一个编辑图标来打开一个允许用户编辑日期的日历。

我有以下代码将图像添加到列中,但它是替换日期而不是在日期之后附加图像。

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{

    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        if (System.Web.Security.Roles.IsUserInRole(Security.GetUserName(true, true), "UpdateStartDate"))
        {
          HyperLink hl = new HyperLink();
          // hl.Text = e.Row.Cells[6].Text;
          hl.ImageUrl = "../images/pencil.gif";

          e.Row.Cells[6].Controls.Add(hl);
        }
    }
}

gridview 列

<asp:BoundField HeaderText="Start Date" DataField="start_dt" DataFormatString="{0:d}" SortExpression="start_dt" ReadOnly="true" /> 

【问题讨论】:

标签: asp.net gridview


【解决方案1】:

我认为最好使用相反的方法:如果用户具有适当的权限,则不要显示编辑链接,而如果用户没有,则隐藏链接。在带有日期值的文本框旁边添加 HyperLink 控件,并在 GridView1_RowDataBound 方法中有条件地隐藏它。

【讨论】:

  • 如果我这样做,我将如何将图像/链接添加到 BoundField?
【解决方案2】:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) {

if (e.Row.RowType == DataControlRowType.DataRow)
{
    if (System.Web.Security.Roles.IsUserInRole(Security.GetUserName(true, true), "UpdateStartDate"))
    {
      HyperLink hl = e.Row.find("h1")
      // hl.Text = e.Row.Cells[6].Text;
      hl.ImageUrl = "../images/pencil.gif";
      h1.visible=true;

    }
    else
    {
      HyperLink hl = e.Row.find("h1")
      h1.visible=false;
     }
}

}

我认为您在设计本身中添加超链接并控制行数据绑定中的可见性

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-22
    • 2016-03-30
    • 1970-01-01
    • 1970-01-01
    • 2017-04-04
    • 1970-01-01
    • 2021-01-24
    • 2011-07-09
    相关资源
    最近更新 更多