i have created my own pagination class and i want to apply it on my gridview...here is my code....all is working but when we change page mean NEXT/PRV etv then data do not change.only paging changes.data remains the same....here is my code
i have created a class and ascx control for data to display
Superguppie.
Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members reading the thread.
When all you've got is a Hammer, Every Problem looks like a Nail. Michael Swain.
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
_controlToPage = Parent.FindControl(_controlToPageName);
if (_controlToPage == null)
throw new Exception(string.Format("'{0}' is not a valid value for ControlToPage (The control does not exist).", _controlToPageName));
if (
! (
(_controlToPage.GetType().IsSubclassOf(typeof(BaseDataBoundControl)))
||
(_controlToPage.GetType().IsSubclassOf(typeof(BaseDataList)))
)
)
throw new Exception("Works only for Controls deriving from BaseDataBoundControl or BaseDataList.");
if (_controlToPage is BaseDataBoundControl)
((BaseDataBoundControl)_controlToPage).DataBinding += new EventHandler(ControlToPage_DataBinding);
else if (_controlToPage is BaseDataList)
((BaseDataList)_controlToPage).DataBinding += new EventHandler(ControlToPage_DataBinding);
}
private void ControlToPage_DataBinding(object sender, EventArgs e)
{
ChildControlsCreated = false;
object o = null;
if (_controlToPage is BaseDataBoundControl)
o = ((BaseDataBoundControl)_controlToPage).DataSource;
else if (_controlToPage is BaseDataList)
o = ((BaseDataList)_controlToPage).DataSource;
IEnumerable enumerable;
if (o.GetType().Name == typeof(DataTable).Name)
enumerable = ((DataTable)o).DefaultView;
else
enumerable = (IEnumerable)o;
_pagedDataSource = new PagedDataSource();
_pagedDataSource.AllowPaging = true;
_pagedDataSource.DataSource = enumerable;
_pagedDataSource.PageSize = PageSize;
_pagedDataSource.CurrentPageIndex = CurrentPageIndex;
if (_controlToPage is BaseDataBoundControl)
((BaseDataBoundControl)_controlToPage).DataSource = _pagedDataSource;
else if (_controlToPage is BaseDataList)
((BaseDataList)_controlToPage).DataSource = _pagedDataSource;
TotalPages = _pagedDataSource.PageCount;
Visible = (TotalPages > 0);
}
Post the code for the NEXT/PRV events. I guess you have some linkbuttons or similar for each page with records. When you click a linkbutton, you need to change the CurrentPageIndex for the PagedDataSource that your control is bound to.
You need to databind the control in the btn_Command event handler:
Using your code enumerable is not define in your code..so i made it again like i have in my first code so finally i made it like this
void btn_Command(object sender, CommandEventArgs e)
{
CurrentPageIndex = int.Parse(e.CommandArgument.ToString());
if (_controlToPage is BaseDataBoundControl)
((BaseDataBoundControl)_controlToPage).DataBinding += new EventHandler(ControlToPage_DataBinding);
else if (_controlToPage is BaseDataList)
((BaseDataList)_controlToPage).DataBinding += new EventHandler(ControlToPage_DataBinding);
object o = null;
if (_controlToPage is BaseDataBoundControl)
o = ((BaseDataBoundControl)_controlToPage).DataSource;
else if (_controlToPage is BaseDataList)
o = ((BaseDataList)_controlToPage).DataSource;
IEnumerable enumerable;
enumerable = (IEnumerable)o;
_pagedDataSource = new PagedDataSource();
_pagedDataSource.AllowPaging = true;
_pagedDataSource.DataSource = enumerable;
_pagedDataSource.PageSize = PageSize;
_pagedDataSource.CurrentPageIndex = CurrentPageIndex;
if (_controlToPage is BaseDataBoundControl)
((BaseDataBoundControl)_controlToPage).DataSource = _pagedDataSource;
else if (_controlToPage is BaseDataList)
((BaseDataList)_controlToPage).DataSource = _pagedDataSource;
((BaseDataList)_controlToPage).DataBind();
}
Now on debugging Datasource(Bold one) is NULL after click next page.so O si null..so not working
On PostBack the DataSource is not remembered. You need to get it again, just like a regular DataBind.
Superguppie.
Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members reading the thread.
When all you've got is a Hammer, Every Problem looks like a Nail. Michael Swain.
shafiqkr
Member
454 Points
372 Posts
Asp.Net Control Issue During Pagination
Mar 29, 2012 07:51 AM|LINK
Hi.
i have created my own pagination class and i want to apply it on my gridview...here is my code....all is working but when we change page mean NEXT/PRV etv then data do not change.only paging changes.data remains the same....here is my code
i have created a class and ascx control for data to display
code on display.ascx
this is at the end of gridview
<cc1:Pager ID="resultsPager" runat="server" ControlToPage="gvResults" CurrentPageIndex="0" FirstImageUrl="" LastImageUrl="" Mode="FirstPreviousNextLast" NextImageUrl="" PageSize="5" PreviousImageUrl="" ShowPageNumbers="True"> </cc1:Pager>and this code is on display.ascx.cs
protected void Page_PreRender(object sender, EventArgs e) { if (!IsPostBack) { gvResults.DataSource = ObjectDataSource1.Select(); gvResults.DataBind(); } }where should i put this code?what to change in this code so that data also get changed when some one change the pages
mm10
Contributor
6445 Points
1187 Posts
Re: Asp.Net Control Issue During Pagination
Mar 29, 2012 11:07 AM|LINK
You probably need to change the CurrentPageIndex property on your UserControl when a page link is clicked.
superguppie
All-Star
48225 Points
8679 Posts
Re: Asp.Net Control Issue During Pagination
Mar 29, 2012 11:45 AM|LINK
How does the pager interact with the GridView?
Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members reading the thread.
When all you've got is a Hammer, Every Problem looks like a Nail. Michael Swain.
shafiqkr
Member
454 Points
372 Posts
Re: Asp.Net Control Issue During Pagination
Apr 01, 2012 07:57 AM|LINK
here is the code
protected override void OnLoad(EventArgs e) { base.OnLoad(e); _controlToPage = Parent.FindControl(_controlToPageName); if (_controlToPage == null) throw new Exception(string.Format("'{0}' is not a valid value for ControlToPage (The control does not exist).", _controlToPageName)); if ( ! ( (_controlToPage.GetType().IsSubclassOf(typeof(BaseDataBoundControl))) || (_controlToPage.GetType().IsSubclassOf(typeof(BaseDataList))) ) ) throw new Exception("Works only for Controls deriving from BaseDataBoundControl or BaseDataList."); if (_controlToPage is BaseDataBoundControl) ((BaseDataBoundControl)_controlToPage).DataBinding += new EventHandler(ControlToPage_DataBinding); else if (_controlToPage is BaseDataList) ((BaseDataList)_controlToPage).DataBinding += new EventHandler(ControlToPage_DataBinding); } private void ControlToPage_DataBinding(object sender, EventArgs e) { ChildControlsCreated = false; object o = null; if (_controlToPage is BaseDataBoundControl) o = ((BaseDataBoundControl)_controlToPage).DataSource; else if (_controlToPage is BaseDataList) o = ((BaseDataList)_controlToPage).DataSource; IEnumerable enumerable; if (o.GetType().Name == typeof(DataTable).Name) enumerable = ((DataTable)o).DefaultView; else enumerable = (IEnumerable)o; _pagedDataSource = new PagedDataSource(); _pagedDataSource.AllowPaging = true; _pagedDataSource.DataSource = enumerable; _pagedDataSource.PageSize = PageSize; _pagedDataSource.CurrentPageIndex = CurrentPageIndex; if (_controlToPage is BaseDataBoundControl) ((BaseDataBoundControl)_controlToPage).DataSource = _pagedDataSource; else if (_controlToPage is BaseDataList) ((BaseDataList)_controlToPage).DataSource = _pagedDataSource; TotalPages = _pagedDataSource.PageCount; Visible = (TotalPages > 0); }The above code is from pager class.
mm10
Contributor
6445 Points
1187 Posts
Re: Asp.Net Control Issue During Pagination
Apr 01, 2012 01:41 PM|LINK
Post the code for the NEXT/PRV events. I guess you have some linkbuttons or similar for each page with records. When you click a linkbutton, you need to change the CurrentPageIndex for the PagedDataSource that your control is bound to.
shafiqkr
Member
454 Points
372 Posts
Re: Asp.Net Control Issue During Pagination
Apr 02, 2012 05:54 AM|LINK
This is the remaining code....
protected override void CreateChildControls() { _table.Rows.Clear(); Controls.Clear(); ClearChildViewState(); //for Valid XHTML 1.0 Transitional the table-tag cannot be placed directly within span-tag //span-tag is created automatically by the .net control //HtmlGenericControl obj = new HtmlGenericControl("object"); //Controls.Add(obj); Controls.Add(_table); //obj.Controls.Add(_table); _table.EnableViewState = false; _table.Width = Unit.Percentage(100); _table.Rows.Add(new TableRow()); TableCell cell = null; cell = new TableCell(); _table.Rows[0].Cells.Add(cell); cell.Width = Unit.Percentage(20); cell.HorizontalAlign = HorizontalAlign.Left; // first button if ((CurrentPageIndex > 0) && ((Mode == PagerMode.FirstLast) || (Mode == PagerMode.FirstPreviousNextLast))) { LinkButton btn = new LinkButton(); cell.Controls.Add(btn); if (FirstImageUrl != string.Empty) btn.Text = string.Format("<img src=\"{0}\" border=\"0\" alt=\"\" />", FirstImageUrl); else btn.Text = "|<<"; btn.CommandArgument = "0"; btn.CommandName = "Page"; btn.Command += new CommandEventHandler(btn_Command); btn.CausesValidation = false; } // previous button if ((CurrentPageIndex > 0) && ((Mode == PagerMode.PreviousNext) || (Mode == PagerMode.FirstPreviousNextLast))) { if (cell.Controls.Count > 0) { Literal lit = new Literal(); lit.Text = " "; cell.Controls.Add(lit); } LinkButton btn = new LinkButton(); cell.Controls.Add(btn); if (PreviousImageUrl != string.Empty) btn.Text = string.Format("<img src=\"{0}\" border=\"0\" alt=\"\" />", PreviousImageUrl); else btn.Text = "<"; btn.CommandArgument = (CurrentPageIndex - 1).ToString(); btn.CommandName = "Page"; btn.Command += new CommandEventHandler(btn_Command); btn.CausesValidation = false; } // page numbers cell = new TableCell(); _table.Rows[0].Cells.Add(cell); cell.HorizontalAlign = HorizontalAlign.Center; cell.Width = Unit.Percentage(40); if (_showPageNumbers) { for (int i = 0; i < TotalPages; i++) { if (cell.Controls.Count > 0) { Literal lit = new Literal(); lit.Text = " "; cell.Controls.Add(lit); } if (i != CurrentPageIndex) { LinkButton btn = new LinkButton(); cell.Controls.Add(btn); //this is shafiq coding for .... // int cT = CurrentPageIndex + 3; // int cW = CurrentPageIndex + 7; // if (cW > i) // { // if (cT < i) // { // btn.Text = ".."; //} // else // { // btn.Text = (i + 1).ToString(); //} btn.Text = (i + 1).ToString(); btn.CommandName = "Page"; btn.CommandArgument = i.ToString(); btn.Command += new CommandEventHandler(btn_Command); btn.CausesValidation = false; } else { Literal lit = new Literal(); cell.Controls.Add(lit); lit.Text = (i + 1).ToString(); } } } cell = new TableCell(); _table.Rows[0].Cells.Add(cell); cell.Width = Unit.Percentage(20); cell.HorizontalAlign = HorizontalAlign.Right; // next button if ((CurrentPageIndex < TotalPages - 1) && ((Mode == PagerMode.PreviousNext) || (Mode == PagerMode.FirstPreviousNextLast))) { LinkButton btn = new LinkButton(); cell.Controls.Add(btn); if (NextImageUrl != string.Empty) btn.Text = string.Format("<img src=\"{0}\" border=\"0\" alt=\"\" />", NextImageUrl); else btn.Text = ">"; btn.CommandArgument = (CurrentPageIndex + 1).ToString(); btn.CommandName = "Page"; btn.Command += new CommandEventHandler(btn_Command); btn.CausesValidation = false; } // last button if ((CurrentPageIndex < TotalPages - 1) && ((Mode == PagerMode.FirstLast) || (Mode == PagerMode.FirstPreviousNextLast))) { if (cell.Controls.Count > 0) { Literal lit = new Literal(); lit.Text = " "; cell.Controls.Add(lit); } LinkButton btn = new LinkButton(); cell.Controls.Add(btn); if (LastImageUrl != string.Empty) btn.Text = string.Format("<img src=\"{0}\" border=\"0\" alt=\"\" />", LastImageUrl); else btn.Text = ">>|"; btn.CommandArgument = (TotalPages - 1).ToString(); btn.CommandName = "Page"; btn.Command += new CommandEventHandler(btn_Command); btn.CausesValidation = false; } } void btn_Command(object sender, CommandEventArgs e) { CurrentPageIndex = int.Parse(e.CommandArgument.ToString()); } } public enum PagerMode { FirstLast, PreviousNext, FirstPreviousNextLast, None }mm10
Contributor
6445 Points
1187 Posts
Re: Asp.Net Control Issue During Pagination
Apr 02, 2012 07:10 AM|LINK
You need to databind the control in the btn_Command event handler:
void btn_Command(object sender, CommandEventArgs e) { CurrentPageIndex = int.Parse(e.CommandArgument.ToString());YourControl.DataSource = _pagedDataSource; YourControl.DataBind(); }shafiqkr
Member
454 Points
372 Posts
Re: Asp.Net Control Issue During Pagination
Apr 02, 2012 09:05 AM|LINK
Using your code enumerable is not define in your code..so i made it again like i have in my first code so finally i made it like this
void btn_Command(object sender, CommandEventArgs e) { CurrentPageIndex = int.Parse(e.CommandArgument.ToString()); if (_controlToPage is BaseDataBoundControl) ((BaseDataBoundControl)_controlToPage).DataBinding += new EventHandler(ControlToPage_DataBinding); else if (_controlToPage is BaseDataList) ((BaseDataList)_controlToPage).DataBinding += new EventHandler(ControlToPage_DataBinding); object o = null; if (_controlToPage is BaseDataBoundControl) o = ((BaseDataBoundControl)_controlToPage).DataSource; else if (_controlToPage is BaseDataList) o = ((BaseDataList)_controlToPage).DataSource; IEnumerable enumerable; enumerable = (IEnumerable)o; _pagedDataSource = new PagedDataSource(); _pagedDataSource.AllowPaging = true; _pagedDataSource.DataSource = enumerable; _pagedDataSource.PageSize = PageSize; _pagedDataSource.CurrentPageIndex = CurrentPageIndex; if (_controlToPage is BaseDataBoundControl) ((BaseDataBoundControl)_controlToPage).DataSource = _pagedDataSource; else if (_controlToPage is BaseDataList) ((BaseDataList)_controlToPage).DataSource = _pagedDataSource; ((BaseDataList)_controlToPage).DataBind(); } Now on debugging Datasource(Bold one) is NULL after click next page.so O si null..so not workingmm10
Contributor
6445 Points
1187 Posts
Re: Asp.Net Control Issue During Pagination
Apr 02, 2012 10:50 AM|LINK
On which line is the datasource NULL?
superguppie
All-Star
48225 Points
8679 Posts
Re: Asp.Net Control Issue During Pagination
Apr 02, 2012 02:33 PM|LINK
On PostBack the DataSource is not remembered. You need to get it again, just like a regular DataBind.
Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members reading the thread.
When all you've got is a Hammer, Every Problem looks like a Nail. Michael Swain.