繁体   English   中英

C#/ ASP.NET-具有HTML选择的中继器

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

我正在尝试使用以下代码在选择中显示一些数据:

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

但不幸的是,我收到以下错误: The server tag is not well formed.

如果我尝试将其更改为value="<%# Eval('Id') %>" CS1012: Too many characters in character literal收到错误消息: CS1012: Too many characters in character literal

任何建议都将非常欢迎,谢谢!

正如其他提到的那样,请使用DropDownList。 它们非常易于使用。

在您的ASPX页面上:

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

并在后面的代码中:

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();

即使您不是ASP.Net的新手,它确实比您想象的要容易。

为什么要用这种方式,为什么不只使用dropdownlist控件呢?

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

您需要在eval语句周围加上单引号。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM