Multiple DataPagers on same page - c#

I'm having an issue where I have 2 DataPagers on the same page, linked to the same ListView. Everything works fine, except the "bottom" or 2nd pager doesn't seem to be working. The page numbers are generated, but clicking on them does nothing. If I copy the "bottom" pager above the "top" pager, then that pager will work, but the one below it doesn't. Seems a only the pager that comes first seems to work:
<asp:DataPager ID="dpPagerTop" runat="server" PagedControlID="lvOutput" QueryStringField="pageNumber">
<Fields>
<asp:NumericPagerField NextPageText="Next" PreviousPageText="Previous" />
</Fields>
</asp:DataPager>
<asp:DataPager ID="dpPagerBottom" runat="server" PagedControlID="lvOutput" QueryStringField="pageNumber">
<Fields>
<asp:NumericPagerField NextPageText="Next" PreviousPageText="Previous" />
</Fields>
</asp:DataPager>
<asp:ListView ID="lvOutput" runat="server" OnPagePropertiesChanged="lvOutput_PagePropertiesChanged">
<LayoutTemplate>
<asp:PlaceHolder id="itemPlaceholder" runat="server" />
</LayoutTemplate>
<ItemTemplate>
<%# Eval("Title") %>
</ItemTemplate>
</asp:ListView>
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
dpPagerTop.SetPageProperties(Request.QueryString["pageNumber"].ToString(), 25, false);
dpPagerBottom.SetPageProperties(Request.QueryString["pageNumber"].ToString(), 25, false);
lvOutput.DataSource = [datasource];
lvOutput.DataBind();
}
}
protected void lvOutput_PagePropertiesChanged(object sender, EventArgs e)
{
lvOutput.DataBind();
}
UPDATE:
After fooling around with this some more, I've determined that both pagers will work if SetPageProperties has the correct parameters. The first parameter should be the number to start the results and the second should be number of results to show. However, I am getting the wrong numbers to display. I have exactly 100 records and I want to display 25 results per page. If I hardcode:
dpPagerTop.SetPageProperties(25, 25, true);
dpPagerBottom.SetPageProperties(25, 25, true);
This should be the 2nd page of the results and the results show 26-50. However, the bottom pager doesn't work.
Now, if I hardcode:
dpPagerTop.SetPageProperties(26, 25, true);
dpPagerBottom.SetPageProperties(26, 25, true);
Both pagers work like the should, but the number of results go from 27-51.
Can anyone recreate this, it's driving me nuts?!?!?
UPDATE 2:
I think I got it to work by setting the page properties BEFORE binding to the ListView.

I had a similar problem with two datapagers on a page bound to one listview. the datapagers weren't synchronized with each - so changes to the top then bottom pagers would have the appearance of the pager not working. This method put them back on track:
protected void ListView1_PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
{
DataPager1.SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
DataPager2.SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
}

I think I have this figured out.
First from what I can tell you need to databind the listview before you set the page properties.
Secondly, I think you are misunderstanding the first parameter to the SetPageProperties method. It does not set the current page, it sets the first record on this page of data.
Here is the HTML I am using
<asp:DataPager ID="dpPagerTop" runat="server" PagedControlID="lvOutput" QueryStringField="pageNumber"
PageSize="2">
<Fields>
<asp:NumericPagerField NextPageText="Next" PreviousPageText="Previous" />
</Fields>
</asp:DataPager>
<asp:DataPager ID="dpPagerBottom" runat="server" PagedControlID="lvOutput" QueryStringField="pageNumber"
PageSize="2">
<Fields>
<asp:NumericPagerField NextPageText="Next" PreviousPageText="Previous" />
</Fields>
</asp:DataPager>
<asp:ListView ID="lvOutput" runat="server" OnPagePropertiesChanged="lvOutput_PagePropertiesChanged">
<LayoutTemplate>
<asp:PlaceHolder ID="itemPlaceholder" runat="server" />
</LayoutTemplate>
<ItemTemplate>
<a href="Donation.aspx" title="<%# Eval("Type") %>">
<%# Eval("id")%></a>
</ItemTemplate>
</asp:ListView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:LutheranAssistanceConnectionString %>"
SelectCommand="SELECT [Id], [RecipientId], [Type], [Reason] FROM [Donations]">
</asp:SqlDataSource>
Here is the code in the code behind
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//bind the list view first
lvOutput.DataSource = SqlDataSource1;
lvOutput.DataBind();
//the first parameter of SetPageProperties is not the page number
//it is index of the first record on the page
//So we need to calculate the index based on the passed in page number.
int pageNumber = Convert.ToInt32(Request["pageNumber"]);
int recordNumber = pageNumber * dpPagerTop.PageSize;
//now set first record
dpPagerTop.SetPageProperties(recordNumber , 25, false);
dpPagerBottom.SetPageProperties(recordNumber , 25, false);
}
}
protected void lvOutput_PagePropertiesChanged(object sender, EventArgs e)
{
lvOutput.DataBind();
}
Hope this helps

Related

ListView ItemCommand not being reached on button click

I have an aspx page using listview. The delete button I have needs to execute a sql delete using an ID found in the list view.
ASPX:
<asp:ListView runat="server" ID="ListView2" OnItemDataBound="ListView2_ItemDataBound" OnItemCommand="ListView2_ItemCommand">
<LayoutTemplate>
...
<asp:DataPager runat="server" ID="DataPager" PageSize="10" OnPreRender="DataPager_PreRender">
<Fields>
...
</Fields>
</asp:DataPager>
</LayoutTemplate>
<ItemTemplate>
...
<td runat="server" align="right" colspan="4"><asp:Button ID="deleteRButton" CssClass="Button" runat="server" Text="Delete" CommandName="deleteRButton" OnClientClick="return confirm('You are about to delete this forum response. Are you sure you want to proceed?');" /></td>
...
</ItemTemplate>
<ItemSeparatorTemplate>
</ItemSeparatorTemplate>
</asp:ListView>
Code Behind:
protected void ListView2_ItemCommand(object sender, ListViewCommandEventArgs e)
{
if (e.CommandName == "deleteRButton") //Never makes it here with breakpoints
{
...
//Find label value and execute SQL
}
}
How do I get the button click to execute the Item Command code? I have tried using an OnClick method as well (which fires) but I don't have access to the label ID value that way.
EDIT:
Sharing page load event:
protected void Page_Load(object sender, EventArgs e)
{
GetQuestions(); //builds data table and binds to listview 1
GetAnswers(); //builds data table and binds to listview 2
questionIDBreadCrumb.Text = grabID(); //grabs id from url
//loads the current userID
getUserData();
}
SOLUTION:
Putting the page load events inside if (!ispostback) solved the issue.
if (!IsPostBack)
{
GetQuestions();
GetAnswers();
questionIDBreadCrumb.Text = grabID();
//loads the current userID
getUserData();
//validates questionOwner
}

list view not showing when last record deletes using asp.net

I have a list view grid with delete say I have a pagination links on the page (previous 1 2 Next). when I am trying to delete last record in the second page the listview doesn't show up.I have done binding in the server side and calling when a record delete. If the second page is having more than two records then grid shows. Any one have idea why this issue coming ? But when I reload page the grid showing.
I have a listview grid as
<asp:ListView ID="lvSurvey" runat="server" GroupPlaceholderID="groupPlaceHolder2"
ItemPlaceholderID="itemPlaceHolder2" OnPagePropertiesChanging="OnPagePropertiesChangingSurvey" OnItemCommand="lvSurvey_ItemCommand" >
---
--
</asp:ListView>
I also have pagination as
<asp:PlaceHolder runat="server" ID="groupPlaceHolder2"></asp:PlaceHolder>
<tr>
<td colspan = "3">
<asp:DataPager ID="DataPager2" runat="server" PagedControlID="lvSurvey" PageSize="1">
<Fields>
<asp:NextPreviousPagerField ButtonType="Link" ShowFirstPageButton="false" ShowPreviousPageButton="true"
ShowNextPageButton="false" />
<asp:NumericPagerField ButtonType="Link" />
<asp:NextPreviousPagerField ButtonType="Link" ShowNextPageButton="true" ShowLastPageButton="false" ShowPreviousPageButton = "false" />
</Fields>
</asp:DataPager>
</td>
</tr>
I am able to display record from sql server and also pagination shows in the page. I have a delete button in grid as
<asp:LinkButton ID="lnkDelete" runat="server" CssClass="btn btn-danger" OnClientClick="return getConfirmation(this, 'Please confirm','Are you sure you want to delete?');" CommandArgument='<%# Eval("Order_Survey_ID") + "|" + Eval("Status") %>'
CommandName="DeleteSurveyObject"><i class="glyphicon glyphicon-trash"></i> </asp:LinkButton> <br />
</td>
</tr>
</ItemTemplate>
</asp:ListView>
I am also able to delete and have done server coding for deletion.But when only one record in second link, not able to show listview at the time of deletion.
protected void OnPagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
{
(lvOrderInstall.FindControl("DataPager1") as DataPager).SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
this.GetAllOrderInstall();
TabName.Value = "installation";
}
protected void lvSurvey_ItemCommand(object source, ListViewCommandEventArgs e)
{
if(e.CommandName == "DeleteObject")
{
string[] param = e.CommandArgument.ToString().Split('|');
hfInstallID.Value = param[0].ToString();
DELETEINSTALL(int.Parse(hfInstallID.Value), clsCommon.gConstDTOMode_Delete, clsCommon.gConstActive_Status);
GetAllOrderInstall();
TabName.Value = "installation";
litMsg.Text = "Record deleted successfully";
}
}
Try adding GetAllOrderInstall(); at the end of lvSurvey_ItemCommand function to rebind the list view after updation.
protected void lvSurvey_ItemCommand(object source, ListViewCommandEventArgs e)
{
if(e.CommandName == "DeleteObject")
{
string[] param = e.CommandArgument.ToString().Split('|');
hfInstallID.Value = param[0].ToString();
DELETEINSTALL(int.Parse(hfInstallID.Value), clsCommon.gConstDTOMode_Delete, clsCommon.gConstActive_Status);
GetAllOrderInstall();
TabName.Value = "installation";
litMsg.Text = "Record deleted successfully";
}
GetAllOrderInstall();
}

call listview OnPagePropertiesChanging function from another function in c# asp.net

I have an aspx ListView control, a data pager for paging, paging is working fine. Now I have a dropdown to change page size and show the relevant records in listview. But dropdown not working properly.
I have total 14 records and when i go to 2nd page which has 4 records after that i change dropdown page size to 30 then it does not reflect listview and still showing same 4 records. It changes page number from 2nd to 1st only.
HTML:-
<asp:DropDownList ID="ddlCount" AutoPostBack="true" runat="server" CssClass="form-control"
SelectedIndexChanged="ddlCount_SelectedIndexChanged"> </asp:DropDownList>
<asp:ListView ID="lvParkingLots" runat="server" GroupPlaceholderID="grp" ItemPlaceholderID="item"
GroupItemCount="2" OnPagePropertiesChanging="lvPL_PagePropertiesChanging">
<LayoutTemplate>
<table class="table table-striped tblForm" id="gs">
<tr id="grp" runat="server">
</tr>
<tr class="pagination-listview">
<td></td>
<td style="float: right;">
<asp:DataPager ID="dp" runat="server" PagedControlID="lvParkingLots" PageSize="30">
<Fields>
<asp:NumericPagerField NextPageText="Next" PreviousPageText="Prev" />
</Fields>
</asp:DataPager>
</td>
</tr>
</table>
</LayoutTemplate>
C# function:-
protected void lvPL_PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
{
DataPager dp = (lvParkingLots.FindControl("dp") as DataPager);
dp.SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
Binddata();
}
I have tried below code but not working..
protected void ddlCount_SelectedIndexChanged(object sender, EventArgs e)
{
Binddata();
}
The first argument to the event is of type object, here it should be the ListView, and the second argument will be PagePropertiesChangingEventArgs which is expecting StartRowIndex and MaximumRow, both are of type integer, so you can call that method like this:
int startRowIndex =2,MaximumRow=3;
lvPL_PagePropertiesChanging(lvSample, new PagePropertiesChangingEventArgs(startRowIndex , MaximumRow));
// where lvSample is the listview
In the case of the second example, you have to call the method like this:
ddlCount_SelectedIndexChanged(ddlCount, EventArgs.Empty);

Listview DataPager with ObjectDataSource problem

I was added the DataPager Control inside Listview. There is no problem while displaying the data. But When I click the Next page button I m getting error.
Error: The Select operation is not supported by ObjectDataSource 'ObjectDataSource2' unless the SelectMethod is specified.
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
FillGrid();
}
private void FillGrid()
{
User user = new User();
user = (User)HttpContext.Current.Session["login"];
ObjectDataSource2.SelectMethod = "GetDetails";
ObjectDataSource2.SelectParameters.Add("Customer_ID", DbType.Int32, Convert.ToString(user.Customer_ID));
ObjectDataSource2.SelectParameters.Add("Selected_Period", DbType.String, Convert.ToString(Request.QueryString["period"]));
ObjectDataSource2.TypeName = "Online.Lib.Invoice";
}
CodeBeside:
<asp:ListView ID="ListView1" runat="server" DataSourceID="ObjectDataSource2">
<LayoutTemplate>
<asp:DataPager ID="DataPager1" PagedControlID="ListView1" runat="server">
<Fields>
<asp:NumericPagerField ButtonCount="10" />
<asp:NextPreviousPagerField FirstPageText="İlk" LastPageText="Son" NextPageText="İleri" PreviousPageText="Geri" />
</Fields>
</asp:DataPager>
</LayoutTemplate>
</asp:ListView>
Ok. Your FillGrid() works well and you can load it's data by the Page_Load routine. When you click "Next page" of the ListView, you're doing a PostBack.
if(!IsPostBack)
FillGrid();
}
..Which means FillGrid() aren't loaded (which is the place ObjectDataSource have it's Select instruction). That's out of what I can see in the code snippets above. Quite common to make such mistakes in IsPostBack handling.

Strange Pager behaviour in ListView

I have a standart Page within my ListView control on the page, And the Pager is work, however in order to move to next list of items i required to click on pager link twice before it actually moves to next set of items.
The code for the pager is:
<asp:ListView ID="lv_LostCard" runat="server" DataKeyNames="request_id" EnableViewState="false">
<LayoutTemplate>
<table width="550" border="1" class="table">
<asp:PlaceHolder ID="itemPlaceholder" runat="server" />
</table>
<asp:DataPager ID="lv_Books_Pager" runat="server" PageSize="10">
<Fields>
<asp:NextPreviousPagerField ShowFirstPageButton="false" ShowPreviousPageButton="true" ShowNextPageButton="false" />
<asp:NumericPagerField />
<asp:NextPreviousPagerField ShowFirstPageButton="false" ShowPreviousPageButton="false" ShowNextPageButton="true" ShowLastPageButton="false" />
</Fields>
</asp:DataPager>
</LayoutTemplate>
<ItemTemplate>
</ItemTemplate>
</asp:ListView>
and the Code behind is:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
getLostCardsList();
}
}
protected void getLostCardsList()
{
using(LostCardsManagementDataContext LostCard = new LostCardsManagementDataContext())
{
var getLostCardsList = from lc in LostCard.lostcard_request_cards
select lc;
lv_LostCard.DataSource = getLostCardsList;
lv_LostCard.DataBind();
}
Can somebody tell me what happening and how to fix it ?
Thanks in advance
I have problems with listview sincerely.
I found a solution related to your question which seems there is no other way to fix that.You need to call OnPreRender method to rebind your source to listview.
protected void listview_PreRender(object sender, EventArgs e)
{
getLostCardsList();//your method for binding
}
Be adviced, PreRender events called before your page is rendered.More clearly,if your page has a postback event will render again.That means you need to store your data into a server collection (i.e. Session).
DataBind in the PagePropertiesChanged event.
private void listview_PagePropertiesChanged(object sender, System.EventArgs e)
{
listview.DataBind();
}
Are you binding your listview in code? Make sure you are only doing that on non-postbacks.
You have turned off the viewstate on your ListView. Try it with the viewstate back on.

Categories

Resources