Dynamic PagerTemplate


M

marty

Having trouble creating a dynamic pagertemplate. I want it like this
"Prev 1 2 3 4 Next". The numerically portion being the dynamic part
based on the record count.

I can generate it, but can't get any events to fire.

Is it possible to do? I've tried creating them int he RowCreated
event, DataBound and my own PagerTemplate, but nothing seems to work.


Here is one snippet that I've tried. It causes a postback, but it
isn't handled by the gridview.

protected void gvSearchResults_RowCreated(object sender,
GridViewRowEventArgs e)
{
if( e.Row.RowType == DataControlRowType.Pager)
{
// Get the pager row.
GridViewRow pagerRow = e.Row;

// Get the pager row.
Panel pagesPanel =
(Panel)pagerRow.FindControl("PagesPanel");

//Panel pagesPanel = pagesPanel;
LinkButton myButton = new LinkButton();
myButton.Text = "Prev";
myButton.CommandName = "Page";
myButton.CommandArgument = "Prev";
myButton.ID = "PrevPage";
pagesPanel.Controls.Add(myButton);

for (int i = 1; i < ((GridView)sender).PageCount + 1; i++)
{
myButton = new LinkButton();
myButton.Text = String.Format("{0}&nbsp;", i);
myButton.CommandName = "Page";
myButton.CommandArgument = i.ToString();
myButton.ID = "Page" + i;
pagesPanel.Controls.Add(myButton);
}

myButton = new LinkButton();
myButton.Text = "Next";
myButton.CommandName = "Page";
myButton.CommandArgument = "Next";
myButton.ID = "NextPage";
pagesPanel.Controls.Add(myButton);
}
}
 
Ad

Advertisements

M

marty

I have. He's using a dropdown list. Not the same thing as adding
control dynamically.
 
A

Andy

I have. He's using a dropdown list. Not the same thing as adding
control dynamically.

Did you get this working? I ran into the exact same problem,
dynamically adding a LinkButton to the Pager row. I worked around it
with this snippet in the Page_Load method:

if ( IsPostBack )
{
// Workaround for an ASP.NET bug.
// We dynamically add an "All" LinkButton to the Pager row of the
GridView.
// When this button is clicked then the GridView is to redisplay
with all
// rows shown. The bug is that this LinkButton doesn't get wired up
correctly
// so that while a postback does, in fact, occur - no event handlers
are ever
// called. By checking the __EVENTTARGET form value here we can
look for our
// LinkButton ID (uxShowAll). If we see it then treat it as if the
event were raised.
String eventTarget = Request.Form["__EVENTTARGET"];
if ( eventTarget.IndexOf("uxShowAll") > 0 )
ShowAllRows();
return;
}
 
Ad

Advertisements

C

Curtis

I have. He's using a dropdown list. Not the same thing as adding
control dynamically.

Did you get this working? I ran into the exact same problem,
dynamically adding a LinkButton to the Pager row. I worked around it
with this snippet in the Page_Load method:

if ( IsPostBack )
{
// Workaround for an ASP.NET bug.
// We dynamically add an "All" LinkButton to the Pager row of the
GridView.
// When this button is clicked then the GridView is to redisplay
with all
// rows shown. The bug is that this LinkButton doesn't get wired up
correctly
// so that while a postback does, in fact, occur - no event handlers
are ever
// called. By checking the __EVENTTARGET form value here we can
look for our
// LinkButton ID (uxShowAll). If we see it then treat it as if the
event were raised.
String eventTarget = Request.Form["__EVENTTARGET"];
if ( eventTarget.IndexOf("uxShowAll") > 0 )
ShowAllRows();
return;
}

I solved the numeric pager with prev/next a different way. I let the
pagersettings generate a numeric mode. I then added the link buttons
in on the rowcreate method. I just inserted into the pagertable. Here
is the rowcreate method (grid is called ArchiveGridView):

if (e.Row.RowType == DataControlRowType.Pager)
{
if (ArchiveGridView.PageIndex != 0)
{
LinkButton myButton = new LinkButton();
myButton.Text = ArchiveGridView.PagerSettings.PreviousPageText;
myButton.CommandName = "Page";
myButton.CommandArgument = "Prev";
myButton.ID = "PrevPage";

TableCell cell = new TableCell();
cell.Controls.Add(myButton);

e.Row.Cells[0].Controls[0].Controls[0].Controls.AddAt(0, cell);
}

if (ArchiveGridView.PageIndex != ArchiveGridView.PageCount - 1)
{
LinkButton myButton = new LinkButton();
myButton.Text = ArchiveGridView.PagerSettings.NextPageText;
myButton.CommandName = "Page";
myButton.CommandArgument = "Next";
myButton.ID = "NextPage";

TableCell cell = new TableCell();
cell.Controls.Add(myButton);

// remove the line wrap
e.Row.Cells[0].Controls[0].Controls[0].Controls.AddAt(e.Row.Cells[0].Controls[0].Controls[0].Controls.Count,
cell);
}
}

Curtis
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top