0
我有一個GridView
,我使用LinqDataSource
來綁定它。我想根據用戶標識的值(存儲在Session
中)過濾GridView
。我還有一個Session
用於確定用戶是否是管理員。根據用戶名過濾GridView
- 如果只有用戶 - 過濾
GridView
到user_id說明(列)==會議[ 「用戶id」] - 如果管理員 - 顯示所有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;
}
}
}
}
謝謝1把它過濾rocord你...我解決了它: if(Session [「admin」] == null) LinqDataSource2.Where =「user_id =」+ userId(); else LinqDataSource2.Where =「user_id = @user_id」; – Mix
whoila it worked ...剛果。 –