I have a ListView (with search bar) that I am binding data to from the SQL server and am using the DataPager control to break up all the information. The problem that I am having is that when the first page loads after a search I have to click twice on any page link or next/last button to show a new page. Can anyone please help me?
DataPager Code:
<div id="pagging" runat="server">
<asp:DataPager ID="DataPager1" runat="server" PagedControlID="ListView1" PageSize="9" >
<Fields>
<asp:NextPreviousPagerField NextPageText=" Next <i class='fa fa-chevron-left'></i> " ShowNextPageButton="true"
ShowPreviousPageButton="false" ShowFirstPageButton="false"
ButtonCssClass="btn btn-default" RenderNonBreakingSpacesBetweenControls="false" RenderDisabledButtonsAsLabels="false" />
<asp:NumericPagerField ButtonType="Link" CurrentPageLabelCssClass="btn btn-primary disabled" RenderNonBreakingSpacesBetweenControls="false"
NumericButtonCssClass="btn btn-default" ButtonCount="10" NextPageText="..." NextPreviousButtonCssClass="btn btn-default" />
<asp:NextPreviousPagerField PreviousPageText=" <i class='fa fa-chevron-right'></i> Prev" ShowPreviousPageButton="true"
ShowNextPageButton="false" ShowLastPageButton="false"
ButtonCssClass="btn btn-default" RenderNonBreakingSpacesBetweenControls="false" RenderDisabledButtonsAsLabels="false" />
</Fields>
</asp:DataPager>
</div>
C#
protected void Page_Load(object sender, EventArgs e){
if (!IsPostBack)
BindListView();
}
private void SearchCustomers()
{
"select * from aspnet_table where *** ", conString);
DataTable dtTable = new DataTable();
dad.Fill(dtTable);
using (DataTable dt1 = new DataTable())
{
ListView1.DataSource = dtTable;
ListView1.DataBind();
}
}
protected void Search(object sender, EventArgs e)
{
SearchCustomers();
}
private void BindListView()
{
ListView1.DataSource = db.aspnet_table;
ListView1.DataBind();
}
protected void list_PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
{
//set current page startindex, max rows and rebind to false
//DataPager1.SetPageProperties( e.MaximumRows, e.StartRowIndex, false); when i remove comment here the second page need one click but data without applay search
//rebind List View
//BindListView();
}
Well, you have removed/commented out the data page chaning event.
If you do NOT wire up the event, then you have that double click event issue/problem.
(you click on the pager - it will need double clicks - and not even work.
So, YOU MUST RE-BIND the lv EACH time for the data pager event.
So, I suggest you build/make a SINGLE listview data loader. And you persist the filter in ViewState (or hidden control).
So, I would suggest this:
Now MyTableWithFitler - maybe it returns a "data table" table that starts without a filter - but you need that re-bind event in Page changing event. If you don't wire up that event correctly, and RE-BIND this pager event, then you get the double click issue/effect that you see.
So that routine will have to return the filtered data if a filter is active, or the whole table if no filter. But a re-bind NEEDS to occur for each pager event (regardless if you have some filter active or not).
Edit: so the question asked is how to filter?
That should be easy.
Say we have this markup - I'll use your pager:
So I just added a text box and a search button.
So, now the code looks like this:
It looks like this:
but say, I look for any hotel name with lodge, I get this:
I am somewhat "confused" as to why the filter is a probelm or issue. I mean, once you get the pager working, then since the dawn of time, applying some critera to the sql is standard fair - and has little much to do with this over-all problem (using the data pager).