将链接图像添加到单元文本后列中的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" /> 

在我看来最好使用相反的方法:如果用户具有适当的权限,请不要显示编辑链接,而是在用户不使用的情况下隐藏链接。添加带有日期值的文本框旁边的超链接控件,并在GridView1_RowDataBound方法中有条件隐藏。

受保护的void gridview1_rowdatabound(对象发送者,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;
     }
}

}

我认为您在设计本身中添加了超链接,并控制ROW Databound中的可见性

最新更新