我有一个GridView
,我使用LinqDataSource
绑定。我想根据userid的值(存储在GridView
中)过滤Session
。我还有Session
来确定用户是否是管理员。
GridView
到user_id(列)==会话[“用户ID”] 我已经设法以多种方式为用户过滤它,但不是如何稍后为管理员显示所有内容(管理员有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;
}
}
}
}
答案 0 :(得分:1)
你可以做以下事情...
page_load()
{
if user is not admin put folllowing...
LinqDataSource2.Where = "user_id = @user_id"
else
do not filter
}
所以这样你可以做到这一点..因为Admin id为1,它的过滤记录为1。