Reputation: 1574
I've turned on paging on my gridview but when I try to change the page, the postback data come back empty.
When the page loads for the first time, the data is there.
This is the first time i've done this so i'm probably doing something wrong.
The datasource is a membershipusercollection list.
Here's my code...
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();
}
and the .aspx page...
<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>
Everything is working fine except for the paging.
Can anyone help me? Please let me know if i've missed any important info and i'll update!!
Upvotes: 4
Views: 351
Reputation: 1574
I've sorted it!
I've put the collection into the session on the page load and bound the gridview datasource to the session object. Only creating the session object when not a postback.
updated code works...
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();
}
if there is a better way than this, please do say, im still learning asp.net/c#!!
thanks
Upvotes: 2