 |
BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0  | This is the forum to discuss the Wrox book ASP.NET 2.0 Website Programming: Problem - Design - Solution by Marco Bellinaso; ISBN: 9780764584640 |
|
Welcome to the p2p.wrox.com Forums.
You are currently viewing the BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|
|

June 19th, 2009, 06:10 PM
|
|
Authorized User
|
|
Join Date: Mar 2007
Posts: 52
Thanks: 6
Thanked 2 Times in 2 Posts
|
|
Repeater ItemCommand not fired...
In my search module, based on TheBeerHouse, Iâm trying to fix together some paging using a repeater control. I have a DAL method that returns a list of objects. In my first attempt, I filled the repeater with numbers according to the total count of objects, divided by a pagesize, chosen by the user from a dropdownlist.
This is the code in the codebehind that loads the repeater:
Code:
private void LoadPager()
{
int LinkNumber = TotalRows / int.Parse(ddlPageSize.SelectedValue);
List<int> Pages = new List<int>();
if (TotalRows > 1)
{
rptPages.Visible = true;
for (int i = 0; i <= LinkNumber; i++)
Pages.Add(i + 1);
rptPages.DataSource = Pages;
rptPages.DataBind();
}
}
LoadPager() is called from the OnItemDataBound event of the ListView that shows the results
This writes out all the pagenumbers, and then I use this code to show the results on each page:
Code:
protected void rptPages_ItemCommand(object source, RepeaterCommandEventArgs e)
{
PageNumber = Convert.ToInt32(e.CommandArgument) - 1;
DoSearch(PageNumber, int.Parse(ddlPageSize.SelectedValue));
}
The Dosearch that is called in this event, is the method that makes the actual call to the database, and it returns a paged resultset based on the parameters.
The code for the repeater look like this:
HTML Code:
<asp:Repeater ID="rptPages" runat="server" OnItemCommand="rptPages_ItemCommand">
<HeaderTemplate>
<table cellpadding="0" cellspacing="0" border="0">
<tr>
<td>
<b>Page:</b>
<asp:LinkButton ID="LinkBtnPrev" runat="server" OnClick="LinkBtnPrev_Click"><< Previous</asp:LinkButton>
</td>
<td>
</HeaderTemplate>
<ItemTemplate>
<asp:LinkButton ID="btnPage" CommandName="Page" CommandArgument="<%#
Container.DataItem %>" runat="server"><%# Container.DataItem %>
</asp:LinkButton>
</ItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="LinkBtnNext" runat="server" OnClick="LinkBtnNext_Click">Next >></asp:LinkButton>
</td> </tr> </table>
</FooterTemplate>
</asp:Repeater>
Itâs not pretty, I know, but itâs for testing sofarâ¦
So, letâs say I get 420 objects in my list and I have a pagesize of 10, the repeater then writes 42 linkbuttons under my ListView, and each button works fine and I can page my results.
BUTâ¦.
I then decided that it was stupid to write out all the buttons at once, and I wanted to make it so that I show 10 buttons at a time, and then have a next/previous button, kind of like the Google pager⦠So I changed my LoadPager to this:
Code:
private void LoadPager(int _pageindex)
{
int _pagesize = 10;
List<int> Pages = new List<int>();
if (TotalRows > 1)
{
rptPages.Visible = true;
for (int i = (_pageindex * _pagesize + 1); i <= ((_pageindex + 1) * _pagesize); i++)
Pages.Add(i);
rptPages.DataSource = Pages;
rptPages.DataBind();
}
}
The rest of the code is the same, but instead of calling LoadPager from the ListView event, I call it at the end of the DoSearch method that makes the actual search call, and I make it pass 0 as pageindex, because I want it to show the first 10 pages when the user hits search. The 10 buttons 1-10 are written out perfectly, and if I change pageindex to 1 for example, the buttons change accordingly to 11-20. The idea is to set the pageindex with the next/previous buttons, but I havenât gotten to that yet.
But this time the buttons do nothing, and the event
Code:
protected void rptPages_ItemCommand(object source, RepeaterCommandEventArgs e)
{
PageNumber = Convert.ToInt32(e.CommandArgument) - 1;
DoSearch(PageNumber, int.Parse(ddlPageSize.SelectedValue));
}
Is not calledâ¦
So, my question is, why?? Is it because I call LoadPager from the DoSearch method, which again is called from the repeater event when a linkbutton is hit? This seems to be the only real change I have madeâ¦
Hope Itâs ok I ask here, even though itâs not exactly the TheBeerHouse book project, but I have found that this is the place to get the best answers. But, it is a little BeerHouse related, and I thought someone else might benefit from this discussion 
|
|

June 19th, 2009, 07:46 PM
|
 |
Wrox Author
|
|
Join Date: Jan 2008
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
|
|
My question is this:
Since you are using a ListView to display the results, why not just use a DataPager for the paging? That's what it's there for.
Code:
<asp:DataPager ID="dpgResults" runat="server" PagedControlID="lvResults"
PageSize="10">
<Fields>
<asp:NumericPagerField ButtonCount="10" NextPageText="Next" PreviousPageText="Previous" />
</Fields>
</asp:DataPager>
This accomplishes exactly what you are trying to do.
|
|

June 20th, 2009, 05:45 AM
|
|
Authorized User
|
|
Join Date: Mar 2007
Posts: 52
Thanks: 6
Thanked 2 Times in 2 Posts
|
|
One of the reasons I skipped around the DataPager option, is because I'm not binding the ListView to a datasource declaratively.
I'm building the query dynamically in the codebehind, based on the user input and there are a lot of different choises, so it can be very different from query to query. The query is built using paging with RowNum, so I only return the amount of rows the user chooses.
I'm not using a stored procedure, I send the query as a string to my DAL method. It then returns a list of objects, and I bind this list to my ListView programmatically. The way the database is built (I didn't do that), I have to join many tables, so it's a fairly complex SQL query, that changes everytime the user changes a single parameter.
So, to be honest, I don't know how to create paging with a DataPager without a stored procedure and an ObjectDataSource, like we did it in TheBeerHouse.
So, I chose to build a custom paging thingy with a repeater instead, and it was working ok until, all of a sudden, I couldn't fire the OnItemCommand in the repeater. The event worked when I wrote out all the LinkButtons, but when I wrote them out 10 buttons at a time, the event stopped working.
|
|

June 20th, 2009, 03:22 PM
|
 |
Wrox Author
|
|
Join Date: Jan 2008
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
|
|
The DataPager is built off whatever is being displayed in the ListView. It does not matter how the ListView is being bound -- programatically or declaratively, with an ObjectDataSource, sproc or text query, whatever.
Quite frankly though, if this were my project, I probably would create a SearchResult object and return that via an ODS, and avoid the programmatic binding. But, that's just me.
To get to your main question, there is just no way I can see that it could be answered on a forum without having access to the code and DB itself. If you want, you can zip it and email it to me and let me have a look.
|
|

June 23rd, 2009, 08:02 AM
|
|
Authorized User
|
|
Join Date: Mar 2007
Posts: 52
Thanks: 6
Thanked 2 Times in 2 Posts
|
|
Going back to the topic of this thread, I managed to figure out why the ItemCommand event on the Repeater didn't fire. I had the Repeater wrapped in an UpdatePanel, and as soon as I removed the Panel, it worked.
I found a couple of forum posts that mentioned the same problem with Repeaters nested in Repeaters. So it seems that when you put the Repeater inside another web control, the Repeater's ItemCommand isn't fired.
I should think there is a way around this, since I think it should be possible to put Repeaters into other web controls, and still use the events of the Repeater...
Anyway, I solved it by removing the UpdatePanel...
|
|

November 4th, 2009, 01:52 AM
|
|
Registered User
|
|
Join Date: Oct 2009
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
asp .net repeater control pass commandargument OnItemCommand
Exception: asp .net repeater control pass commandargument OnItemCommand
Here is the solution...
http://muruganad.com/ASP.NET/asp-.ne...emCommand.html
Thanks!
Murugan Andezuthu Dharmaratnam
http://muruganad.com
|
|
 |