基于角色的GridView中显示的编辑按钮 [英] Displaying Edit button in GridView based on Role
问题描述
我有一个网格视图。我加了 AutoGenerateEditButton属性=真
。
我想显示按钮到属于一定角色的用户。如果没有呈现不是按钮。
你有什么建议?哪个事件我必须处理来实现这一目的?
我必须与模板驱动的控制工作,比如ListView的?
解决方案
<列>
< ASP:CommandField中
ShowEditButton =真/>
< ASP:绑定列
数据字段=ID
只读=真
可见=真/>
< ASP:绑定列
数据字段=标题
HEADERTEXT =标题/>
< /列>
和GridView的Load事件:
如果(!User.IsInRole(经理人))
{
的for(int i = 0; I< grdMovies.Columns.Count;我++)
{
如果(grdMovies.Columns [i]为CommandField中)
{
grdMovies.Columns [I]。可见=虚假的;
}
}
}
使用 ButtonField字段
与的CommandName =编辑
。
您可以隐藏基于用户角色的的Page_Load
函数列:
const int的_editColumnIndex = 0;
无效的Page_Load(对象发件人,EventArgs的)
{
如果(!User.IsInRole(经理人))
grdMovies.Columns [_editColumnIndex]。可见=虚假的;
}
I have a Grid View. I added AutoGenerateEditButton=True
.
I want to display that button to users that belong to a certain role. If not the button is not rendered.
What do you recommend? Which event do I have to handle to accomplish that purpose?
Do I have to work with template-driven control such as ListView?
Solution:
<Columns>
<asp:CommandField
ShowEditButton="True" />
<asp:BoundField
DataField="Id"
ReadOnly="true"
Visible="true" />
<asp:BoundField
DataField="Title"
HeaderText="Title" />
</Columns>
And the GridView's Load event:
if(!User.IsInRole("Manager"))
{
for (int i = 0; i < grdMovies.Columns.Count; i++)
{
if (grdMovies.Columns[i] is CommandField)
{
grdMovies.Columns[i].Visible = false;
}
}
}
Use ButtonField
with CommandName = Edit
.
You can hide the column in Page_Load
function based on the user's role:
const int _editColumnIndex = 0;
void Page_Load(object sender, EventArgs e)
{
if(!User.IsInRole("Manager"))
grdMovies.Columns[_editColumnIndex].Visible = false;
}
这篇关于基于角色的GridView中显示的编辑按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!