Nick
Nick

Reputation: 3965

C#/ASP.NET - Repeater with a HTML select

I'm trying to display some data within a select using the following code:

<select name="area" runat="server">
    <asp:Repeater ID="RepeaterArea" runat="server">
    <ItemTemplate>
        <option value="<%# Eval("Id") %>"><%# Eval("Area") %></option>
    </ItemTemplate>
    </asp:Repeater>
</select>

But unfortunately I get the following error: The server tag is not well formed.

If I try to change it to value="<%# Eval('Id') %>" I get an error saying: CS1012: Too many characters in character literal

Any suggestions would be very welcome, thank you!

Upvotes: 2

Views: 3727

Answers (3)

Lucky Pierre
Lucky Pierre

Reputation: 570

As the other poster mentioned, use a DropDownList. They are very easy to use.

On your ASPX page:

<asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList>

And in your code behind:

DataTable dtItems = GetData();
DropDownList1.DataSource = dtItems; // Pretty much anything that can be iterated over or a collection (DataTable, List, etc)
DropDownList1.DataTextField = "Some Field"; // This is what the user sees. If this is a DataTable, "Some Field" is the name of a column in the table.
DropDownList1.DataValueVield = "Some Unique Value"; // Again, "Some Unique Value" is a column in the DataTable
DropDownList1.DataBind();

It's really easier than you might think, even if you're new to ASP.Net.

Upvotes: 3

user2621192
user2621192

Reputation:

Why do you want do it this way, why not just use a dropdownlist control.

Upvotes: 2

Mahesh
Mahesh

Reputation: 174

<option value='<%# Eval("Id") %>'><%# Eval("Area") %></option>

You need single quotes around the eval statement.

Upvotes: 0

Related Questions