PDA

View Full Version : Listing categories


QBall777
11-06-2007, 10:02 PM
Hi

I've got a <asp: dropdownlist> that lists categories form my database, when you click a category the page is posted back with the products for that category. My question is I do not want to use a drop down list I wish to use something I can style like a repeater. I'm really struggling with the repeater, below is the code I'm using.

Drop down list. This works fine

<div class="main_nav">
<asp:DropDownList ID="ddlCategory" runat="server"
AutoPostBack="True"
DataSourceID="SqlDataSource1"
DataTextField="name"
DataValueField="catid" AppendDataBoundItems="false">
</asp:DropDownList>


Repeater. This is not working

<asp:Repeater id="myRepeaterUL" runat="server" DataSourceID="SqlDataSource1">
<HeaderTemplate>
<ul>
</HeaderTemplate>
<ItemTemplate>

<li><a href="<%# DataBinder.Eval(Container.DataItem, "catid") %>">
<%# DataBinder.Eval(Container.DataItem, "name") %></a></li>
</ItemTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
</asp:Repeater>

Hope someone can help.

SouthwaterDave
11-07-2007, 07:50 PM
The problem is that your ItemTemplate provides a means of binding data for output but it does not provide a control that reports back to an event handler in server-side code.

The answer is something like:


<ul>
<asp:RepeaterID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand">
<ItemTemplate>
<li>
<asp:LinkButton CommandName='<%#Eval("catid")%>' ID="Link1" runat="server" Text='<%#Eval("name")%>'/>
</li>
</ItemTemplate>
</asp:Repeater>
</ul>

And then in your server-side code:


Protected Sub Repeater1_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.RepeaterCommandEventArgs)
'Do something with e.CommandName
End Sub

nikkiH
11-07-2007, 08:01 PM
Are you looking for a postback to occur, or just a request that passes the category id?

For the latter, use whatever you want the URL parameter to be (say, catid) and this format for the link:

<li><a href="?catid=<%# DataBinder.Eval(Container.DataItem, "catid") %>">
<%# DataBinder.Eval(Container.DataItem, "name") %></a></li>

If you need a postback, then you can't just use a link like that. You need a linkbutton.

<li><asp:LinkButton runat="server" ID="linkBtn" Text='<%# Eval("name") %>' CommandArgument='<%# Eval("catid") %>'></asp:LinkButton></li>

QBall777
11-08-2007, 07:08 PM
That's great guys

Ive now sorted it, thanks for the tips. I'm new to programming so please excuse my ignorance. :thumbsup: