如何分割SQL查询结果来填充一个DropDownList [英] How to split sql query result to populate a dropdownlist
问题描述
该数据集是这样的:
SPEC1 Spec2 Spec3 Spec4 Spec5 Spec6 Spec7 Spec8 Spec9 Spec10
< A HREF =/ spe.aspx ID = 10?称号=AI> AI< / A> < A HREF =/ spe.aspx ID = 40?称号=BA> BA< / A>
我想填充一个DropDownList:
< ASP:DropDownList的ID =ddl1=服务器>< / ASP:DropDownList的>
这样的HTML输出是这样的(每列,而不是每行一个条目):
<选择>
<期权价值=/ spe.aspx ID = 10?> AI< /选项>
<期权价值=/ spe.aspx ID = 40?> BA< /选项>
< /选择>
您需要使用类似的这个来解析出来。而你需要(可能)使用的foreach
环,第二的DataSet
(或字典<字符串,字符串>
)对象,你可以把值代入,然后使用数据绑定你的DropDownList
编辑:没有HTML敏捷性包:
词典<字符串,字符串> dict1 =新词典<字符串,字符串>();
的foreach(在my.Tables的DataRow - [R [0] .Rows)
{
的foreach(DataColumn的下在my.Tables [0] .Columns)
{
如果(R [C] == DBNull.Value || - [R [C]的ToString()。修剪()==)
继续;
串规格= R [C]的ToString();
字符串HREF = spec.Substring(spec.IndexOf(HREF =);
HREF = href.Trim(\\)子串(0,spec.IndexOf(\\));
....
dict1.Add(HREF,VAL);
}
}
ddl1.DataSource = dict1;
ddl1.DataBind();
您可能能够获得使用该库更容易的解决方案,但我不知道它如何处理缺少一个文件,只具有单一的元素。但是,应表现出相当好。
The DataSet looks like this:
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>
I would like to populate a dropdownlist:
<asp:dropdownlist ID="ddl1" runat="server"></asp:dropdownlist>
so the HTML output is this (an entry for each column instead of each row):
<select>
<option value="/spe.aspx?id=10">AI</option>
<option value="/spe.aspx?id=40">BA</option>
</select>
You'll need to use something like this to parse it out. And you'll need to (probably) use a foreach
loop and a second DataSet
(or a Dictionary<string, string>
) object which you can put the values into and then use to data bind your DropDownList.
EDIT: Without the 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();
You might be able to get an easier solution using the library but I'm not sure how well it handles a lack of a document and only having a single element. But that should demonstrate fairly well.
这篇关于如何分割SQL查询结果来填充一个DropDownList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!