简体   繁体   English

HTML select标记内的中继器

[英]Repeater inside an HTML select tag

I am forced to use a select tag so please don't suggest the asp dropdownlist. 我被迫使用选择标记,因此请不要建议asp下拉列表。

My problem is that the select tag is not visible unless there is a runat=server . 我的问题是,除非有runat = server,否则select标签是不可见的。 If I put a runat=server, I get an error stating that I cannot have a repeater inside a HTMLSelect . 如果我放置了runat = server,则会收到一条错误消息,指出我无法在HTMLSelect内使用中继器

Any suggestions would be very welcome, thanks! 任何建议将非常欢迎,谢谢!

<select name="medicineName" class="grey-text initialized">
   <option value="" disabled selected>Choose medicine</option>
      <asp:Repeater ID="Repeater1" runat="server">
          <ItemTemplate>
              <option value='<%# Eval("id")%>'>
                        <%# Eval("name") %> | <%# Eval("unit") %> | <%#Eval ("quantity") %>
              </option>
          </ItemTemplate>
      </asp:Repeater>
</select>

The proper way to resolve your problem would be to do: 解决问题的正确方法是:

  1. Apply runat="server" to the Select. runat="server"应用于Select。
  2. Delete the Repeater 删除中继器

You don't need the Repeater , all it will do is loop through a Collection and output in a formatting you build. 您不需要Repeater ,它所要做的就是循环遍历Collection并以您构建的格式输出。 From code behind you can actually apply a DataSource like the Repeater , then it will output in your Select. 从后面的代码中,您实际上可以应用DataSource例如Repeater ,然后它将在您的Select中输出。 How you achieve this: 您如何实现这一目标:

// Build Model:
public class Medicine
{
     public int? Id { get; set; }
     public string Name { get; set; }
}

You have your model, now you would Query<Medicine>(...) . 您有了模型,现在将Query<Medicine>(...) Which would build a Collection of your Model. 这将建立您的模型的Collection Then you would do the following: 然后,您将执行以下操作:

// Dropdown:
drpMedicine.DataSource = Query<Medicine>(...);
drpMedicine.DataBind();

Then on your front-end, you would do the following: 然后在您的前端,执行以下操作:

<select id="drpMedicine" runat="server" datatextfield="Name" datavaluefield="Id"></select>

That would allow you to bind your List without manually creating your loop. 这样一来,您无需手动创建循环即可绑定List

Please note that Query is a custom method, which will build and format the data. 请注意,查询是一种自定义方法,它将建立并格式化数据。 You need to do the following. 您需要执行以下操作。

In order to achieve the usage of a DropDownList control (instead of Repeater), make sure that you concatenate the database columns as one column from your SQL statement. 为了实现DropDownList控件(而不是Repeater)的用法,请确保将数据库列连接为SQL语句中的一列。

For example: 例如:

SELECT id, name + ' | ' + unit + ' | ' + CONVERT(varchar, quantity) AS medicine
FROM medicines_table

I assumed that your quantity column is using an integer data type that's why I converted it to a string value. 我假设您的数量列使用的是整数数据类型,这就是为什么我将其转换为字符串值的原因。

From your code-behind, you can now set the values for DataTextField and DataValueField properties : 现在,您可以从代码的后面为DataTextField和DataValueField属性设置值:

// other code here
yourDropDownList.DataTextField = "medicine";
yourDropDownList.DateValueField = "id";
// other code here

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

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