将链接图像添加到AFTER单元格文本列中的GridView单元格

时间:2012-10-19 02:50:17

标签: asp.net gridview

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

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

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" /> 

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

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

}

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