我有一个GridView
我正在使用LinqDataSource
. 我想GridView
根据用户标识的值(存储在 a 中Session
)过滤 。我还有一个Session
用于确定用户是否是管理员的方法。
- 如果只有用户 - 过滤
GridView
到 user_id(column) == Session["userid"] - 如果 admin - 显示所有 user_ids。
我已经设法以多种方式为用户过滤它,但不是以后如何为管理员显示所有内容(管理员的 user_id = 1)。
有人知道吗?
这是我的aspx:
<asp:GridView ID="GridView1" runat="server"
CausesValidation="False"
GridLines="None"
AllowPaging="True"
CssClass="mGrid"
PagerStyle-CssClass="pgr"
AlternatingRowStyle-CssClass="alt" AutoGenerateColumns="False"
DataSourceID="LinqDataSource2" DataKeyNames="event_id">
<AlternatingRowStyle CssClass="alt"></AlternatingRowStyle>
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
<asp:BoundField DataField="event_id" HeaderText="event_id" ReadOnly="True"
SortExpression="event_id" InsertVisible="False" />
<asp:BoundField DataField="title" HeaderText="title" SortExpression="title" />
<asp:BoundField DataField="description" HeaderText="description"
SortExpression="description" />
<asp:BoundField DataField="event_start" HeaderText="event_start"
SortExpression="event_start" />
<asp:BoundField DataField="event_end" HeaderText="event_end"
SortExpression="event_end" />
<asp:BoundField DataField="user_id" HeaderText="user_id"
SortExpression="user_id" />
</Columns>
<PagerStyle CssClass="pgr"></PagerStyle>
</asp:GridView>
<asp:LinqDataSource ID="LinqDataSource2" runat="server"
ContextTypeName="DataClassesDataContext" EntityTypeName=""
TableName="calevents" EnableDelete="True"
EnableInsert="True" EnableUpdate="True" Where="user_id = @user_id">
</asp:LinqDataSource>
我尝试过的一些事情(手动数据绑定):
// Registered users may only edit their own events
DataClassesDataContext dc = new DataClassesDataContext();
var events = from a in dc.calevents
where a.user_id == userId()
select a;
GridView1.DataSource = events;
GridView1.DataBind();
或跟随,这使我不得不重写编辑、取消和更新方法。
// Registered users may only edit their own events
protected void GridView1_DataBound(object sender, EventArgs e)
{
if (Session["admin"] != "admin")
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
for (int j = 0; j < GridView1.Columns.Count; j++)
{
if (GridView1.Rows[i].Cells[j].Text != userId().ToString())
{
GridView1.Rows[i].Visible = false;
Label1.Text = GridView1.Rows.Count.ToString();
}
else
GridView1.Rows[i].Visible = true;
}
}
}
}