如何使用runat =" server"在select中调用方法

时间:2016-06-17 08:12:18

标签: asp.net

我有一个select html元素,其中填充了查询结果。标记如下:

<select runat="server" name="nation" id="nation" class="pure-u-1" data-rule-required="true">
    <option value="" selected></option>
    <%= CountryOptions %>
</select>

CountryOptionsString,它返回select元素的选项列表。在我添加runat="server"之前一切正常,因为我需要从后面的代码中访问select元素,现在CountryOptions没有初始化并且select是空的。代码有问题,如何解决?

1 个答案:

答案 0 :(得分:1)

你不应该使用普通的HTML元素,也许应该看看asp:DropDownList。

   <asp:DropDownList runat="server" DataSourceID="SomeDS" AppendDataBoundItems="true"> // Notice the last property
        <asp:ListItem Text="Please select an option" Value="" />
    </asp:DropDownList>