Hi, I have a listview with a paging control. All works fine but I added a dropdowncontrol to be used as filter.
The dropdown has a distinct query to show type user profile (male/female/kid/unisex). I add a listitem "select" using prerender.
prerender:
Protected Sub DropDownList1_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.PreRender
If (DropDownList1.Items.Count > 0) Then
If (DropDownList1.Items(0).Text <> "All") Then
DropDownList1.Items.Insert(0, New ListItem("All", ""))
End If
End If
End Sub
Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
ListView1.DataSource = SqlDataSource3
ListView1.DataBind()
End Sub
and sqldatasource3:
<asp:SqlDataSource ID="SqlDataSource3" runat="server"
ConnectionString="<%$ ConnectionStrings:MyShopConnString %>"
SelectCommand="SELECT * FROM [vw_Product] WHERE (([Cat] = @Cat) AND ([gebruiker] = @gebruiker) and visible=1)">
<SelectParameters>
<asp:QueryStringParameter Name="Cat" QueryStringField="Category" Type="String" />
<asp:ControlParameter ControlID="DropDownList1" Name="gebruiker"
PropertyName="SelectedValue" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
binding logic: on load it binds All entries (sql1). If postback: bind sql3, except if value = "" (=select all) .. then it should show all entries (= same as on pageload)
If Page.IsPostBack Then
If Not DropDownList1.SelectedValue = "" Then
ListView1.DataSource = SqlDataSource3
ListView1.DataBind()
Else
ListView1.DataSource = SqlDataSource1
ListView1.DataBind()
End If
Else
ListView1.DataSource = SqlDataSource1
ListView1.DataBind()
End If
Issue 1: When I select dropdownfilter, it shows the filtered list .. but the paging is not working properly anymore.
Issue 2: When I reselect all, it shows me 'no data' .. result returns no data.
Bottomline or maybe easier question: what is easy way to filter a listview?
Create a procedure to return records for Listview. Add two parameters one for Category and one for gebruiker. You can use IF ELSE condition to check if gebruiker is blank or not and then write select statement accordingly. One should return all records and
second one returns records for a particualr gebruiker value.
You can set the DropDownList autopostback to true and add control parameter. That should filter records once the selected value changes. you do not need to add databinding logic or paging logic. Try that and see if that approach works.
I took another apporach: I use a small repeater which creates hyperlinks if available. Works correct now, BUT paging NOT!
On pageload I load product data in dataset, filtered by querystring:
Dim gebruiker As String = Request.QueryString("Gebruiker")
If gebruiker = "" Then
gebruiker = "%"
Else
End If
Dim da2 As New SqlDataAdapter("Select * FROM vw_Product WHERE cat=@cat and gebruiker like @gebruiker and visible=1", conn)
da2.SelectCommand.Parameters.AddWithValue("@Cat", Request.QueryString("category"))
da2.SelectCommand.Parameters.AddWithValue("@Gebruiker", gebruiker)
Dim ds2 As New DataSet()
da2.Fill(ds2, "producten")
ListView1.DataSource = ds2.Tables("producten")
ListView1.DataBind()
I added a simple pager control, and works, but with 1 click delay: I click on page 2 > nothing happens. I click on page 2 again > goes to page 2.
EnableViwestate is not defined, but does not change anything. something to do with Page.Ispostback? Tried to put the databinding in 'Not Postback' but no luck ..
When I use modified datapager as the one below: I get the Invalid argument error, telling me I need to set the enableeventvalidation. When I set this validation to true > invalid argument error stays. When I set this validation to false > paging does not work .. instead it keeps navigating to 1st page (you see the pagelink when hovering over the link)
From your first issue quesion I get to know that you're now wanting to filter ListView with Dropdownlist. That's easy:
1) Bind the SqlDataSource to the Dropdownlist, set its DataTextField, DataValueField, property of AutoPostBack = True.
2) Then bind the SqlDataSource to the GridView, make sure that your Select method should have a parameter left for the Dropdownlist to filter with the help of the ControlParameter.
Please refer to this similar sample here:
<%@ Page Language="VB" %>
<html>
<head>
<title>Introducing the ASP.NET 2.0 GridView - Filtering</title>
</head>
<body>
<form runat="server">
<asp:SqlDataSource ID="myFilteringDataSource" runat="server"
ConnectionString="Server=(local);Database=pubs;User Id=sa;Password=password;"
SelectCommand="SELECT DISTINCT state FROM authors ORDER BY state"
/>
Only Show Authors From:
<asp:DropDownList ID="myDropDownList" runat="server"
DataSourceID = "myFilteringDataSource"
DataValueField = "state"
AutoPostBack = "True"
/>
<asp:SqlDataSource ID="mySqlDataSource" runat="server"
ConnectionString = "Server=(local);Database=pubs;User Id=sa;Password=password;"
SelectCommand = "SELECT * FROM authors WHERE state = @state"
>
<SelectParameters>
<asp:ControlParameter Name="state" ControlID="myDropDownList" PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
Member
33 Points
181 Posts
Filter a Listview
Sep 13, 2010 01:24 PM|Dompi|LINK
Hi, I have a listview with a paging control. All works fine but I added a dropdowncontrol to be used as filter.
The dropdown has a distinct query to show type user profile (male/female/kid/unisex). I add a listitem "select" using prerender.
prerender:
and sqldatasource3:
binding logic: on load it binds All entries (sql1). If postback: bind sql3, except if value = "" (=select all) .. then it should show all entries (= same as on pageload)
Issue 1: When I select dropdownfilter, it shows the filtered list .. but the paging is not working properly anymore.
Issue 2: When I reselect all, it shows me 'no data' .. result returns no data.
Bottomline or maybe easier question: what is easy way to filter a listview?
Thanks!
D.
Member
46 Points
57 Posts
Re: Filter a Listview
Sep 13, 2010 02:14 PM|patinit|LINK
What does your SELECT statement look like for SqlDataSource1?
All-Star
37505 Points
8109 Posts
Re: Filter a Listview
Sep 13, 2010 02:19 PM|sansan|LINK
You can do this way
Create a procedure to return records for Listview. Add two parameters one for Category and one for gebruiker. You can use IF ELSE condition to check if gebruiker is blank or not and then write select statement accordingly. One should return all records and second one returns records for a particualr gebruiker value.
You can set the DropDownList autopostback to true and add control parameter. That should filter records once the selected value changes. you do not need to add databinding logic or paging logic. Try that and see if that approach works.
Member
33 Points
181 Posts
Re: Filter a Listview
Sep 14, 2010 04:05 AM|Dompi|LINK
I took another apporach: I use a small repeater which creates hyperlinks if available. Works correct now, BUT paging NOT!
On pageload I load product data in dataset, filtered by querystring:
I added a simple pager control, and works, but with 1 click delay: I click on page 2 > nothing happens. I click on page 2 again > goes to page 2.
EnableViwestate is not defined, but does not change anything. something to do with Page.Ispostback? Tried to put the databinding in 'Not Postback' but no luck ..
Thanks for any help please - frustrating.
D.
Member
33 Points
181 Posts
Re: Filter a Listview
Sep 14, 2010 04:14 AM|Dompi|LINK
what I described happens with this standard datapaging setting:
When I use modified datapager as the one below: I get the Invalid argument error, telling me I need to set the enableeventvalidation. When I set this validation to true > invalid argument error stays. When I set this validation to false > paging does not work .. instead it keeps navigating to 1st page (you see the pagelink when hovering over the link)
This is modified pager:
Member
33 Points
181 Posts
Re: Filter a Listview
Sep 14, 2010 07:50 AM|Dompi|LINK
I fixed the "second" click issue with the standard paging control by adding a listview.databind in a page prerendering event.
However it still did not fix my image based version ..
Any thoughts welcome ?!
All-Star
94130 Points
18109 Posts
Re: Filter a Listview
Sep 15, 2010 09:32 PM|Decker Dong - MSFT|LINK
From your first issue quesion I get to know that you're now wanting to filter ListView with Dropdownlist. That's easy:
1) Bind the SqlDataSource to the Dropdownlist, set its DataTextField, DataValueField, property of AutoPostBack = True.
2) Then bind the SqlDataSource to the GridView, make sure that your Select method should have a parameter left for the Dropdownlist to filter with the help of the ControlParameter.
Please refer to this similar sample here:
<%@ Page Language="VB" %>
<html>
<head>
<title>Introducing the ASP.NET 2.0 GridView - Filtering</title>
</head>
<body>
<form runat="server">
<asp:SqlDataSource ID="myFilteringDataSource" runat="server"
ConnectionString="Server=(local);Database=pubs;User Id=sa;Password=password;"
SelectCommand="SELECT DISTINCT state FROM authors ORDER BY state"
/>
Only Show Authors From:
<asp:DropDownList ID="myDropDownList" runat="server"
DataSourceID = "myFilteringDataSource"
DataValueField = "state"
AutoPostBack = "True"
/>
<asp:SqlDataSource ID="mySqlDataSource" runat="server"
ConnectionString = "Server=(local);Database=pubs;User Id=sa;Password=password;"
SelectCommand = "SELECT * FROM authors WHERE state = @state"
>
<SelectParameters>
<asp:ControlParameter Name="state" ControlID="myDropDownList" PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
<asp:GridView ID="myGridView" DataSourceID="mySqlDataSource" runat="server"
AllowPaging = "True"
AllowSorting = "True"
/>
</form>
</body>
</html>
Download the running codes from here:
http://www.15seconds.com/files/060323.zip