Click here to Skip to main content
15,890,946 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I use linq to sql,and use a drop down for showing records,

I use this code for paging in .aspx file
XML
<asp:DropDownList ID="drpPageSize" class="form-control" runat="server" AutoPostBack="True" OnSelectedIndexChanged="drpPageSize_SelectedIndexChanged" meta:resourcekey="drpPageSizeResource1">

   <asp:ListItem Selected="True" meta:resourcekey="ListItemResource1">5</asp:ListItem>
   <asp:ListItem meta:resourcekey="ListItemResource2">10</asp:ListItem>
   <asp:ListItem meta:resourcekey="ListItemResource10">50</asp:ListItem>
   <asp:ListItem meta:resourcekey="ListItemResource12">100</asp:ListItem>
   <asp:ListItem  Text="All" meta:resourcekey="ListItemResource11"></asp:ListItem>
</asp:DropDownList>


and use this code for .cs file,here is both code for gridbind and drpselect index changed event
C#
protected void grdBind()
{
   try
   {
      EventManagerDataContext db = new EventManagerDataContext();

      var q = from a in db.EMR_EVENTs
      join b in db.EMR_CLIENTs on a.ClientID equals b.ClientID
      // join c in db.EMR_INVITATIONs on a.EventID equals c.EventID

      select new
      {
         ClientName = b.Name,
         ClientID = a.ClientID,
         Name = a.Name,
         No_Of_Invitees = a.No_Of_Invitees,
         Extra_Invitees = a.Extra_Invitees,
         Total_Invitees = a.Extra_Invitees + a.No_Of_Invitees,
         EventID = a.EventID,
         // InvitationId=c.InvitationID
      };

      grid.DataSource = q.ToList();
      if (Convert.ToInt32(Session["k"])== 1)
      {
         grid.PageSize = Convert.ToInt32(Session["total"]); //here total is the no of rows in gridview
      }
      else
      {
         grid.PageSize = int.Parse(drpPageSize.SelectedValue);
      }
      grid.DataBind();
   }
   catch
   {
      throw;
   }
}

protected void drpPageSize_SelectedIndexChanged(object sender, EventArgs e)
{
   if (drpPageSize.SelectedIndex == 4)
   {
      grid.AllowPaging = false;
      Session["k"] = 1;
      // grid.PageSize = Convert.ToInt32(Session["total"]);
      grdBind();
   }
   else
   {
      Session["k"] = 2;
      grid.PageSize = int.Parse(drpPageSize.SelectedItem.Text);
      grdBind();
   }
}


my all code is working. but i found that when i select all from gridview i found all records,but after select all from drop down i select 5 then i not get the 5 records ,insted of i get all the records,same as in 10,50,100

any solution please.?

i also appreciate that a better code for my requirement,my main purpose is show all records from gridview while i select all from dropdown.
Posted
Updated 1-Jan-14 0:43am
v3
Comments
Have you debugged and seen that it is going inside else part on selection 5 in DropDownList?
JoCodes 1-Jan-14 2:16am    
Use selected value uniformly instead of using value or text of the dropdown.Debug and check the code.

Try to check the session contains value before setting it as page size. Also, suggest to use the dropdownlist selected value instead of text.Carefully debug with these things in mind.

Adding a reference on how to deal with the custom pagination

http://devtoolbox.wordpress.com/tag/add-dropdown-list-to-pager/[^]

Hope this helps...
 
Share this answer
 
To allow Paging in gridview in asp.net i used follow...

set property of gridview is

C#
Allowpaging=True
pageSize=2 //(depend on you)

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
   {
       GridView1.PageIndex = e.NewPageIndex;
       GridView1.DataBind();
   }
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900