ASP.net GridView: hide Edit|Delete links

老子叫甜甜 提交于 2019-12-25 00:14:09

问题


I have GridView with AutoGenerateDeleteButton=true && AutoGenerateEditButton=true. I want to allow only registered users to use these functions therefore I want to hide it from unregistered users. How can I hide it?

I tried hidding the whole column but on page_load gridView is not ready yet so I get null exception.


回答1:


On your pageLoad event store user Role inside Session

 protected void Page_Load(object sender, EventArgs e)
{
    Session["usrRole"] = "1";
}

On Row databound event of your gridview check for the session & if not equal to your administrator role, set visibility of your delete button column to false

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        if (Session["usrRole"] != "1")
        {
            e.Row.Cells[0].Visible = false;   //0 is autogenerate edit column index
            e.Row.Cells[1].Visible = false;  // 1  is autogenerate delete column index
        }
    }
}


来源:https://stackoverflow.com/questions/9655357/asp-net-gridview-hide-editdelete-links

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!