Asp.net 将链接图像添加到单元格文本后的列中的GridView单元格
对于我的gridview中的一列“startdate”,如果用户具有正确的权限,我想添加一个编辑图标以打开一个日历,允许用户编辑日期Asp.net 将链接图像添加到单元格文本后的列中的GridView单元格,asp.net,gridview,Asp.net,Gridview,对于我的gridview中的一列“startdate”,如果用户具有正确的权限,我想添加一个编辑图标以打开一个日历,允许用户编辑日期 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { if (System.Web.Security.Roles.IsUser
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);
}
}
}
我有以下代码将图像添加到列中,但它将替换日期,而不是在日期后追加图像
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\u RowDataBound
方法中。受保护的void GridView1\u RowDataBound(对象发送方,GridViewRowEventArgs e)
{
}
我认为您可以在设计本身中添加超链接,并控制行数据绑定中的可见性如果这样做,我将如何将图像/链接添加到边界字段?处理行数据绑定事件。。。。如果用户没有编辑权限,则不要显示链接或禁用该链接或您希望在该位置执行的任何其他操作,或者您也可以检查该链接,该链接在网格视图中使用条件语句,谢谢@rahularyansharma第一个链接帮了大忙!!