我在gridview上打开了分页,但是当我尝试更改页面时,回发数据会变回空白。
第一次加载页面时,数据就在那里。
这是我第一次这样做,所以我可能做错了。
数据源是membershipuseruser集合列表。
这是我的代码......
protected void Page_Load(object sender, EventArgs e)
{
count = 0;
switch (Request.QueryString["status"])
{
case "active":
lblUserListTitle.Text = "Activated user accounts";
break;
case "inactive":
lblUserListTitle.Text = "Inactive user accounts";
break;
case "locked":
lblUserListTitle.Text = "Locked user accounts";
break;
case "online":
lblUserListTitle.Text = "Users online";
break;
case "notverified":
lblUserListTitle.Text = "Users accounts not yet verified";
break;
default:
lblUserListTitle.Text = "All user accounts";
break;
}
if (!IsPostBack)
BindGrid();
lblSearchResult.Text = "There are " + count.ToString() + " registered users found.";
}
protected void BindGrid()
{
MembershipUserCollection usersList = Membership.GetAllUsers();
MembershipUserCollection filteredUsers = new MembershipUserCollection();
foreach (MembershipUser user in usersList)
{
if (!Roles.IsUserInRole(user.UserName, "Admin") && !Roles.IsUserInRole(user.UserName, "Engineering"))
{
userProfile = Profile.GetProfile(user.UserName);
if (txtFilterCustomerNo.Text.Length > 0)
{
ProfileCommon PC = Profile.GetProfile(user.UserName);
if (PC.RaymarineAccountNo == txtFilterCustomerNo.Text.ToUpper())
{
filteredUsers.Add(user);
count++;
}
}
else
{
//filter on querystring
if (Request.QueryString["status"] == "active")
{
if (user.IsApproved && !user.IsLockedOut)
{
filteredUsers.Add(user);
count++;
}
}
else if (Request.QueryString["status"] == "inactive")
{
if (!user.IsApproved && !user.IsLockedOut)
{
filteredUsers.Add(user);
count++;
}
}
else if (Request.QueryString["status"] == "locked")
{
if (user.IsLockedOut)
{
filteredUsers.Add(user);
count++;
}
}
else if (Request.QueryString["status"] == "online")
{
if (user.IsOnline)
{
filteredUsers.Add(user);
count++;
}
}
else if (Request.QueryString["status"] == "notverified")
{
if (user.IsApproved && !userProfile.IsVerified)
{
filteredUsers.Add(user);
count++;
}
}
else
{
filteredUsers.Add(user);
count++;
}
}
}
}
GridView1.DataSource = filteredUsers;
GridView1.DataBind();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
BindGrid();
}
和.aspx页面......
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="1000px" onrowdatabound="GridView1_RowDataBound" ViewStateMode="Enabled" AllowPaging="True" onpageindexchanging="GridView1_PageIndexChanging" PageSize="25">
<Columns>
<asp:TemplateField ItemStyle-Width="70px">
<ItemTemplate>
<asp:HyperLink ID="lnkProfile" runat="server"><asp:Image ID="Image3" runat="server" ImageUrl="~/Content/admin_miniProfile.png" AlternateText="User Profile" /></asp:HyperLink>
<asp:HyperLink ID="lnkPermissions" runat="server"><asp:Image ID="Image2" runat="server" ImageUrl="~/Content/admin_miniPermissions.png" AlternateText="Permissions" /></asp:HyperLink>
<asp:HyperLink ID="lnkPassword" runat="server"><asp:Image ID="Image1" runat="server" ImageUrl="~/Content/admin_miniResetPassword.png" AlternateText="Reset Password" /></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="UserName" HeaderText="User Name" />
<asp:TemplateField ItemStyle-Width="100px" HeaderText="Account No">
<ItemTemplate>
<asp:Label ID="lblAccountNo" runat="server" Text=""></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="CreationDate" HeaderText="Created" ItemStyle-Width="160px" />
<asp:TemplateField ItemStyle-Width="20px">
<ItemTemplate>
<asp:HyperLink ID="lnkSalesman" runat="server"><asp:Image ID="imgFilter" runat="server" ImageUrl="~/Content/admin_miniFilter.png" /></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="200px" HeaderText="Salesman">
<ItemTemplate>
<asp:Label ID="lblSalesman" runat="server" Text=""></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="20px">
<ItemTemplate>
<asp:HyperLink ID="lnkDelete" runat="server"><asp:Image ID="Image4" runat="server" ImageUrl="~/Content/admin_miniDeleteUser.png" AlternateText="Delete User" /></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<PagerSettings Position="TopAndBottom" />
<RowStyle Height="20px" VerticalAlign="Middle" />
<AlternatingRowStyle BackColor="#EEEEEE" />
</asp:GridView>
除了分页之外,一切正常。
任何人都可以帮助我吗?如果我错过了任何重要信息,请告诉我,我会更新!!
答案 0 :(得分:2)
我已经对它进行了分类!
我已将集合放入页面加载的会话中,并将gridview数据源绑定到会话对象。仅在不回发时创建会话对象。
更新的代码有效......
protected void Page_Load(object sender, EventArgs e)
{
count = 0;
switch (Request.QueryString["status"])
{
case "active":
lblUserListTitle.Text = "Activated user accounts";
break;
case "inactive":
lblUserListTitle.Text = "Inactive user accounts";
break;
case "locked":
lblUserListTitle.Text = "Locked user accounts";
break;
case "online":
lblUserListTitle.Text = "Users online";
break;
case "notverified":
lblUserListTitle.Text = "Users accounts not yet verified";
break;
default:
lblUserListTitle.Text = "All user accounts";
break;
}
if (!IsPostBack)
{
MembershipUserCollection usersList = Membership.GetAllUsers();
MembershipUserCollection filteredUsers = new MembershipUserCollection();
foreach (MembershipUser user in usersList)
{
if (!Roles.IsUserInRole(user.UserName, "Admin") && !Roles.IsUserInRole(user.UserName, "Engineering"))
{
userProfile = Profile.GetProfile(user.UserName);
if (txtFilterCustomerNo.Text.Length > 0)
{
ProfileCommon PC = Profile.GetProfile(user.UserName);
if (PC.RaymarineAccountNo == txtFilterCustomerNo.Text.ToUpper())
{
filteredUsers.Add(user);
count++;
}
}
else
{
//filter on querystring
if (Request.QueryString["status"] == "active")
{
if (user.IsApproved && !user.IsLockedOut)
{
filteredUsers.Add(user);
count++;
}
}
else if (Request.QueryString["status"] == "inactive")
{
if (!user.IsApproved && !user.IsLockedOut)
{
filteredUsers.Add(user);
count++;
}
}
else if (Request.QueryString["status"] == "locked")
{
if (user.IsLockedOut)
{
filteredUsers.Add(user);
count++;
}
}
else if (Request.QueryString["status"] == "online")
{
if (user.IsOnline)
{
filteredUsers.Add(user);
count++;
}
}
else if (Request.QueryString["status"] == "notverified")
{
if (user.IsApproved && !userProfile.IsVerified)
{
filteredUsers.Add(user);
count++;
}
}
else
{
filteredUsers.Add(user);
count++;
}
}
}
}
if (Session["FilteredUsers"] == null)
Session.Add("FilteredUsers", new MembershipUserCollection());
Session["FilteredUsers"] = filteredUsers;
}
BindGrid();
lblSearchResult.Text = "There are " + count.ToString() + " registered users found.";
}
protected void BindGrid()
{
GridView1.DataSource = (MembershipUserCollection)Session["FilteredUsers"];
GridView1.DataBind();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
BindGrid();
}
如果有比这更好的方法,请说,我还在学习asp.net/c#!!
感谢