andieje
asked on
reusing a gridview pager template
Hi
I have a gridview that has a custom pager template. I also have some code in the page that creates some numeric links dynamically and adds them to the pager template and handles their events.
I would like to encapsulate this into a control so that I can reuse the paging functionality but change the columns the grid displays on different pages. I haven't got a clue how to do this. Please could you advise where I would begin
Here is the template. I know I could make this into a template class if that helps
<asp:GridView ID="GridView1" runat="server">
<PagerTemplate>
<tr>
<td style="padding-left:10px;" >
<asp:ImageButton ID="imgFirst" runat="server" CommandName="Page" CommandArgument="First" ImageUrl="~/Images/first_g rey.gif" Height="14px" Width="16px" /></td>
<td>
<asp:ImageButton ID="imgPrev" runat="server" CommandName="Page" CommandArgument="Prev" ImageUrl="~/Images/prev_gr ey.gif" Height="14px" Width="11px" /></td>
<td style="padding-right: 3px; padding-left: 3px;"><span id="pnlNumberLinks" runat="server"></span></td >
<td>
<asp:LinkButton ID="lnkNext" runat="server" CommandName="page" CommandArgument="Next" CausesValidation="false" Text="Next" />
<asp:ImageButton ID="imgNext" runat="server" CommandName="Page" CommandArgument="Next" ImageUrl="~/Images/next_gr ey.gif" Height="14px" Width="11px" /></td>
<td> <asp:ImageButton ID="imgLast" runat="server" CommandName="Page" CommandArgument="Last" ImageUrl="~/Images/last_gr ey.gif" Height="14px" Width="16px" /></td>
</tr>
</PagerTemplate>
</asp:GridView>
Then in the page behind I have code for creating the links and responding to link clicks. For example here is a function for handling the click of a link button
Sub NumericNav_OnCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls. CommandEve ntArgs)
CurrentPageNumber = e.CommandName
BindGrid()
End Sub
If I put this grid into a user control, how would i be able to change the columns that the grid displays on different pages? Do I have to do something more complicated than a user control?
thanks a lot
andrea
I have a gridview that has a custom pager template. I also have some code in the page that creates some numeric links dynamically and adds them to the pager template and handles their events.
I would like to encapsulate this into a control so that I can reuse the paging functionality but change the columns the grid displays on different pages. I haven't got a clue how to do this. Please could you advise where I would begin
Here is the template. I know I could make this into a template class if that helps
<asp:GridView ID="GridView1" runat="server">
<PagerTemplate>
<tr>
<td style="padding-left:10px;"
<asp:ImageButton ID="imgFirst" runat="server" CommandName="Page" CommandArgument="First" ImageUrl="~/Images/first_g
<td>
<asp:ImageButton ID="imgPrev" runat="server" CommandName="Page" CommandArgument="Prev" ImageUrl="~/Images/prev_gr
<td style="padding-right: 3px; padding-left: 3px;"><span id="pnlNumberLinks" runat="server"></span></td
<td>
<asp:LinkButton ID="lnkNext" runat="server" CommandName="page" CommandArgument="Next" CausesValidation="false" Text="Next" />
<asp:ImageButton ID="imgNext" runat="server" CommandName="Page" CommandArgument="Next" ImageUrl="~/Images/next_gr
<td> <asp:ImageButton ID="imgLast" runat="server" CommandName="Page" CommandArgument="Last" ImageUrl="~/Images/last_gr
</tr>
</PagerTemplate>
</asp:GridView>
Then in the page behind I have code for creating the links and responding to link clicks. For example here is a function for handling the click of a link button
Sub NumericNav_OnCommand(ByVal
CurrentPageNumber = e.CommandName
BindGrid()
End Sub
If I put this grid into a user control, how would i be able to change the columns that the grid displays on different pages? Do I have to do something more complicated than a user control?
thanks a lot
andrea
The easiest way to make all of your pages look alike is by doing a skin file, but I'm not 100% sure how to do it in your case.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You could always write a usercontrol that contained the pager template. Then you just drop that in when you need it.
it would look something like this, you'd just drop this code anytime you need the template.
<PagerTemplate>
<my:pagertemplate id="myPagerTemplate" runat="server" />
</PagerTemplate>
it would look something like this, you'd just drop this code anytime you need the template.
<PagerTemplate>
<my:pagertemplate id="myPagerTemplate" runat="server" />
</PagerTemplate>
ASKER
thanks everyone for your replies
hi raterus- i considered writing a user control that contained the template. However, to work, the template uses many of the events of the gridview. I probably need to extend the gridview as suggested by samtran but i was trying to avoid doing that.
I'm familiar with you and the answers you give and you have always given me very good advice. If you have anything else to add I would appreciate it.
Here is an example of the itemcreated event of the grid so you can see how the pager functionality is dependent on the grid events (and you wil see i'm not actually using a gridview - i'm using a 3rd party grid but it has the same lifecycle etc)
Protected Sub RadGrid1_ItemCreated(ByVal sender As Object, ByVal e As Telerik.WebControls.GridIt emEventArg s) Handles RadGrid1.ItemCreated
If TypeOf e.Item Is GridPagerItem Then
Response.Write("Page index: " & RadGrid1.CurrentPageIndex & "<br>")
Dim pagerItem As GridPagerItem = CType(e.Item, GridPagerItem)
'this creates a hyperlink in the pager, one for each page in the grid
CreateNumericLinksPanel(pa gerItem)
'enable and disable nav buttons
Dim imgPrev, imgNext, imgFirst, imgLast As ImageButton
imgPrev = pagerItem.FindControl("img Prev")
imgNext = pagerItem.FindControl("img Next")
imgFirst = pagerItem.FindControl("img First")
imgLast = pagerItem.FindControl("img Last")
Select Case RadGrid1.CurrentPageIndex
Case 0
imgPrev.Enabled = False
imgFirst.Enabled = False
imgNext.Enabled = True
imgLast.Enabled = True
Case RadGrid1.VirtualItemCount - 1
imgPrev.Enabled = True
imgFirst.Enabled = True
imgNext.Enabled = False
imgLast.Enabled = False
Case Else
imgPrev.Enabled = True
imgFirst.Enabled = True
imgNext.Enabled = True
imgLast.Enabled = True
End Select
SetNavigationButtonImage(i mgPrev)
SetNavigationButtonImage(i mgFirst)
SetNavigationButtonImage(i mgLast)
SetNavigationButtonImage(i mgNext)
End If
End Sub
hi raterus- i considered writing a user control that contained the template. However, to work, the template uses many of the events of the gridview. I probably need to extend the gridview as suggested by samtran but i was trying to avoid doing that.
I'm familiar with you and the answers you give and you have always given me very good advice. If you have anything else to add I would appreciate it.
Here is an example of the itemcreated event of the grid so you can see how the pager functionality is dependent on the grid events (and you wil see i'm not actually using a gridview - i'm using a 3rd party grid but it has the same lifecycle etc)
Protected Sub RadGrid1_ItemCreated(ByVal
If TypeOf e.Item Is GridPagerItem Then
Response.Write("Page index: " & RadGrid1.CurrentPageIndex & "<br>")
Dim pagerItem As GridPagerItem = CType(e.Item, GridPagerItem)
'this creates a hyperlink in the pager, one for each page in the grid
CreateNumericLinksPanel(pa
'enable and disable nav buttons
Dim imgPrev, imgNext, imgFirst, imgLast As ImageButton
imgPrev = pagerItem.FindControl("img
imgNext = pagerItem.FindControl("img
imgFirst = pagerItem.FindControl("img
imgLast = pagerItem.FindControl("img
Select Case RadGrid1.CurrentPageIndex
Case 0
imgPrev.Enabled = False
imgFirst.Enabled = False
imgNext.Enabled = True
imgLast.Enabled = True
Case RadGrid1.VirtualItemCount - 1
imgPrev.Enabled = True
imgFirst.Enabled = True
imgNext.Enabled = False
imgLast.Enabled = False
Case Else
imgPrev.Enabled = True
imgFirst.Enabled = True
imgNext.Enabled = True
imgLast.Enabled = True
End Select
SetNavigationButtonImage(i
SetNavigationButtonImage(i
SetNavigationButtonImage(i
SetNavigationButtonImage(i
End If
End Sub
Ahh, I wasn't really even thinking of what would go in that pager template...
After seeing what you are doing, I'm with Sam, in that I think you should extend this Grid. I did this with the DataGrid, and had very good results with it. And since it's extended, it's very easy to hook up to the events to do what you need to do. Once you get over the hurdle of figuring out how to extend the grid, you should be ok.
The UserControl idea would require a lot of manual "hookup" you'd have to remember every time you implemented this pager.
After seeing what you are doing, I'm with Sam, in that I think you should extend this Grid. I did this with the DataGrid, and had very good results with it. And since it's extended, it's very easy to hook up to the events to do what you need to do. Once you get over the hurdle of figuring out how to extend the grid, you should be ok.
The UserControl idea would require a lot of manual "hookup" you'd have to remember every time you implemented this pager.
the link i posted on extending the gridview is really nice...but can be a bit daunting....
if it looks like a bit much...google "extending asp.net controls" and there are tons of smaller examples like adding functionality to textboxes and calendars that will make it easier to grasp...
extend label:
http://www.codeproject.com/aspnet/textcontrolext.asp
extend textbox:
http://www.eggheadcafe.com/articles/pfc/extendservercontrol.asp
extend calendar:
http://aspnet.4guysfromrolla.com/articles/100103-1.aspx
then go to extend the gridview:
http://aspalliance.com/946
g'luck!
if it looks like a bit much...google "extending asp.net controls" and there are tons of smaller examples like adding functionality to textboxes and calendars that will make it easier to grasp...
extend label:
http://www.codeproject.com/aspnet/textcontrolext.asp
extend textbox:
http://www.eggheadcafe.com/articles/pfc/extendservercontrol.asp
extend calendar:
http://aspnet.4guysfromrolla.com/articles/100103-1.aspx
then go to extend the gridview:
http://aspalliance.com/946
g'luck!
ASKER
thanks samtran - i'll have a look at those links!
ASKER
Hello
I've been looking at how to extend a gridview and it seems fairly straightforward. However, the code that I would need to add to my extended gridview is dependent on the gridview using a specific pager template. How could i force the gridview to use a specific pager template and incorporate that template into the gridview?
thanks
I've been looking at how to extend a gridview and it seems fairly straightforward. However, the code that I would need to add to my extended gridview is dependent on the gridview using a specific pager template. How could i force the gridview to use a specific pager template and incorporate that template into the gridview?
thanks