0

For one of the columns "startdate" in my gridview, if the user has correct permissions, I want to add an edit icon to open a calendar which allows the user to edit the date.

I have the following code to add the image to the column, but it's replacing the date rather than appending the image after the date.

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

The gridview column

<asp:BoundField HeaderText="Start Date" DataField="start_dt" DataFormatString="{0:d}" SortExpression="start_dt" ReadOnly="true" /> 
Filip
  • 3,257
  • 2
  • 22
  • 38
RememberME
  • 2,092
  • 4
  • 37
  • 62

2 Answers2

2

In my opinion better to use opposite approach: don't show edit link if user have appropriate permissions, but instead to hide link if user doesn't. Add HyperLink control next to textbox with date value and hide it conditionally in GridView1_RowDataBound method.

Yuriy Rozhovetskiy
  • 22,270
  • 4
  • 37
  • 68
1

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

}

I think you add the hyperlink in design itself and controls the visibility in row databound

Sreerejith S S
  • 258
  • 5
  • 18