如何拆分SQL查询结果以填充下拉列表

时间:2015-05-18 18:39:50

标签: c# sql asp.net sql-server

DataSet如下所示:

Spec1                                           Spec2                                           Spec3   Spec4   Spec5   Spec6   Spec7   Spec8   Spec9   Spec10
<a href="/spe.aspx?id=10" title="AI">AI</a>     <a href="/spe.aspx?id=40" title="BA">BA</a>

我想填充一个下拉列表:

<asp:dropdownlist ID="ddl1" runat="server"></asp:dropdownlist>

所以HTML输出是这样的(每列而不是每一行的条目):

<select>
    <option value="/spe.aspx?id=10">AI</option>
    <option value="/spe.aspx?id=40">BA</option>
</select>

1 个答案:

答案 0 :(得分:1)

您需要使用类似this的内容来解析它。并且您需要(可能)使用foreach循环和第二个DataSet(或Dictionary<string, string>)对象,您可以将值放入,然后使用数据绑定DropDownList

编辑:没有HTML Agility Pack:

Dictionary<string, string> dict1 = new Dictionary<string, string>();
foreach (DataRow r in my.Tables[0].Rows)
{
    foreach (DataColumn c in my.Tables[0].Columns)
    {
        if (r[c] == DBNull.Value || r[c].ToString().Trim() == "")
           continue;
        string spec = r[c].ToString();
        string href = spec.Substring(spec.IndexOf("href=");
        href = href.Trim("\"").Substring(0, spec.IndexOf("\""));
        ....
        dict1.Add(href, val);
    }
}
ddl1.DataSource = dict1;
ddl1.DataBind();

您可能能够使用该库获得更简单的解决方案,但我不确定它是如何处理缺少文档并且只有一个元素。但这应该表现得相当好。