I have a page (category-list.apsx) that uses the Repeater Control method to display the xml details on the page. I used the example shown here:
http://www.w3schools.com/aspnet/aspnet_repeater.asp
This works fine but I would want the user to be able to filter the results using a dropdown for CategoryName.
The results repeater look like this:
<form runat="server">
<asp:Repeater id="categories" runat="server">
<ItemTemplate>
<tr>
<td><%#Container.DataItem("CategoryName")%> </td>
<td> </td>
<td><%#Container.DataItem("CategoryMonth")%> </td>
<td> </td>
<td><%#Container.DataItem("CategoryMonthSpend")%> </td>
<td> </td>
<td><%#Container.DataItem("Amount")%> </td>
</tr>
</ItemTemplate>
</asp:Repeater>
</form>
The XML look like this:
<catalog>
<categories>
<CategoryName>Category Name1</CategoryName>
<CategoryMonth>April 2012</CategoryMonth>
<CategoryMonthSpend>£1</CategoryMonthSpend> <Amount>1</Amount>
</categories>
</catalog>
The script that activates the repeater can be seen below:
<script runat="server">
Public Sub Page_Load()
If Not Page.IsPostBack Then
Dim cat As String = Request.QueryString("cat")
Dim mycategories As DataSet = New DataSet()
mycategories.ReadXml(MapPath("XML/" + cat + ".xml"))
categories.DataSource = mycategories
categories.DataBind()
End If
End Sub
</script>
OK, quite a bit to cover here so I won’t go into overwhelming detail for each section. Hopefully this should give you a good starting point to understanding Databinding in ASP.NET a little more.
I prefer actually writing my code in the code-behind, not a
<script runat="server">inside of my .aspx page, so that’s where my code is in this example. Functionally, however, there is no difference here and you can choose to put this code in that .aspx-side script if you’d prefer.First, let’s fix your
Repeatertemplate. You appear to be using a table layout, but nowhere in your template is the actual<table></table>tag. You need to add a<HeaderTemplate>and a<FooterTemplate>Second, let’s declare a
DropDownListon your aspx page that you would like to use for filtering:The
AutoPostBackproperty here means that yourDropDownListwill automatically post back to the server and fire off aSelectedIndexChangedevent on your server that you can handle in your code. Alternatively you can use aButtonto click when you want to trigger the filter.Third, let’s separate your databinding code into nice, neat little methods that can more easily be reused.
Fourth, let’s update your
Page_Loadcode so that we can take advantage of these methods:Last and certainly not least, let’s create the
SelectedIndexChangedevent handler in order to trigger filtering of this dataset:So what did we do here? By separating out these databinding methods I made it a little cleaner and allowed the two separate controls to more easily share the same
DataSetin your XML file. UsingIEnumerableallows me to use LINQ which I feel is much nicer than standard querying overDataTableorDataViewobjects.In the
DropDownListdatabinding code I am selecting out a single column of your data and turning it into a collection of strings. I also callDistinctfor good measure so that duplicates are removed. I also take the liberty of adding a blank item to the list, so that users have the option to choose NO filter, and display everything.You’ll notice there is a little bit of code in the
SelectedIndexChangedevent handler to see if the DropDownList value is blank. This is not necessarily the most robust (would break down if one of your items actually had a blank “CategoryName” and you wanted to filter on it), but works for this example. An alternative would be to useddlCategory.SelectedIndex <> 0as a check for if a filter is selected.This is by no means a complete explanation of everything going on here, so feel free to ask questions. However this should help get you to a working example that you can expand on for future development.
Edit: This code requires that you have imported the
System.Collections.Genericnamespace and theSystem.Linqnamespace. In Visual Studio 2010, this is likely already automatically imported for you in the Web Application project. If not, you can choose to add them directly in your code file or on the Project Properties page for your web application under References > Imported Namespaces