C#/ASP.NET - Cannot have repeater inside HTMLselec

2019-07-19 06:00发布

问题:

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') %>" runat="server"><%# Eval("Area") %></option>
    </ItemTemplate>
    </asp:Repeater>
</select>

I get an error stating that I cannot have a repeater inside a HTMLSelect.

Any suggestions would be very welcome, thanks!

回答1:

Remove the runat="server". You can get the value by Request.Form["area"].ToString()



回答2:

Why is your <select> being run at the server? Remove that attribute and it should compile fine.

As the error says, the only valid child element of an HtmlSelect are options or optgroup thingy, not asp repeaters (even if they do create options, the compiler isn't aware of this).



回答3:

Try removing the runat="server" from select tag