Click here to Skip to main content
15,915,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am having problems with datapager. I created a listview then placed a button which filters a datasource using SelectCommand. When I click the button, first time, everything works perfectly but once I click any of the datapager buttons it resets the filter.

I am fairly new the asp .net and I don't know where to start digging.

A bit lost.

This is what my datapager layout looks like:

XML
<asp:DataPager ID="DataPager1" runat="server" >
    <Fields>
        <asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True"
            ShowLastPageButton="True" />
    </Fields>
</asp:DataPager>



Thanks.
Posted
Updated 5-Apr-12 1:14am
v2

1 solution

Hi ,
Try this example
C#
protected void ListView1_PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
  {
             SqlDataSource1.FilterExpression = "Item_name like '%trr%'";      
  }



ASP.NET
<div>
        <asp:listview id="ListView1" runat="server" datakeynames="item_code" xmlns:asp="#unknown">
            DataSourceID="SqlDataSource1">
    
            <itemtemplate>
                <li style="background-color: #DCDCDC;color: #000000;">item_code:
                    <asp:label id="item_codeLabel" runat="server" text="<%# Eval("item_code") %>" />
                    <br />
                    Item_name:
                    <asp:label id="Item_nameLabel" runat="server" text="<%# Eval("Item_name") %>" />
                    <br />
                    brand:
                    <asp:label id="brandLabel" runat="server" text="<%# Eval("brand") %>" />                   
                    <br />
                    price:
                    <asp:label id="priceLabel" runat="server" text="<%# Eval("price") %>" />
                    <br />
                    Material:
                    <asp:label id="MaterialLabel" runat="server" text="<%# Eval("Material") %>" />
                    <br />
                    tax:
                    <asp:label id="taxLabel" runat="server" text="<%# Eval("tax") %>" />
                    <br />
                </li>
            </itemtemplate>
            <layouttemplate>
                <ul id="itemPlaceholderContainer"  runat="server">
                    style="font-family: Verdana, Arial, Helvetica, sans-serif;">
                    <li  runat="server" id="itemPlaceholder" />
                </ul>
                <div style="text-align: center;background-color: #CCCCCC;font-family: Verdana, Arial, Helvetica, sans-serif;color: #000000;">
                </div>
            </layouttemplate>
            
        </asp:listview>
        <asp:sqldatasource id="SqlDataSource1" runat="server" xmlns:asp="#unknown">
            ConnectionString="<%$ ConnectionStrings:testConnectionString %>" 
            SelectCommand="SELECT * FROM [Items]"></asp:sqldatasource>
        <asp:datapager id="DataPager1" runat="server" pagedcontrolid="ListView1" pagesize="4" xmlns:asp="#unknown">
            <fields>
           <asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True" 
                    ShowNextPageButton="False" ShowPreviousPageButton="False" />
                <asp:NumericPagerField />
                <asp:NextPreviousPagerField ButtonType="Button" ShowLastPageButton="True" 
                    ShowNextPageButton="False" ShowPreviousPageButton="False" />     
            </fields>
                
        </asp:datapager>
    </div>

Best Regards
M.Mitwalli
 
Share this answer
 
v4
Comments
JimAce 5-Apr-12 8:13am    
Thank you for the response. The situation is once I click on Next/Previous or any of the pagers, it is resetting the filter back to the original which displays all the records in my table.

Below is my code for the filter.
<pre lang="sql">Button btnSelect = (Button)sender;

switch (btnSelect.CommandName)
{
case "newSelect":
SqlDataSource1.SelectCommand = "SELECT * FROM BasicInfo WHERE sSchool='" + btnSelect.CommandArgument.ToString() + "'";
ListView1.DataBind();
break;
}</pre>
Mohamed Mitwalli 5-Apr-12 22:14pm    
your welcome i believe this happen because of your passing wrong data source
i Noticed in your code that you are not passing the new data source to list view it has to be like this
switch (btnSelect.CommandName)
{
case "newSelect": SqlDataSource1.SelectCommand = "SELECT * FROM BasicInfo WHERE sSchool='" + btnSelect.CommandArgument.ToString() + "'";
//add here datatable
DataTable dt = new Datatable();
SqlDataAdapter adpt = new SqlDataAdapter();
adpt.Fill(dt);
ListView1.DataSource = dt;
ListView1.DataBind();
break;
}
I hope it work with you
JimAce 5-Apr-12 22:57pm    
Hi,

How should I pass the value from SelectCommand to the adapter? Currently I am getting "System.InvalidOperationException: The SelectCommand property has not been initialized before calling 'Fill'."

Thank you so much :)
JimAce 5-Apr-12 23:03pm    
Here is my SqlDataSource in aspx. I think this is what the filter does after clicking the datapager.

<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:TableConnectionString %>"
SelectCommand="SELECT [SeqSchool], [SchoolURL], [sSchoolName], [sRegion]FROM [BasicInfo]">
<SelectParameters></SelectParameters>


Thank you. :)
Mohamed Mitwalli 8-Apr-12 2:31am    
Hi ,
just write your filter on ListView1_PagePropertiesChanging or your select command and it will work fine
i did improve the solution have look

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900