日期:2014-05-17  浏览次数:20449 次

select绑定数据库数据
在加载的时候 给<select></select>帮顶数据库数据 asp.net的 求高手们帮忙

------解决方案--------------------
C# code

   public static string AppendSelectControl(string id, string name, string classname, DataTable options, string textname, string valname, string curvalue)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("<select id=\"{0}\" name=\"{1}\" class=\"{2}\" >", id, name, classname);
            for (int i = 0; i < options.Rows.Count; i++)
            {
                if (options.Rows[i][valname].ToString() == curvalue)
                {
                    sb.AppendFormat(string.Concat(new object[] { "<option selected=\"selected\" value=\"", options.Rows[i][valname], "\">", options.Rows[i][textname], "</option>" }), new object[0]);
                }
                else
                {
                    sb.AppendFormat(string.Concat(new object[] { "<option value=\"", options.Rows[i][valname], "\">", options.Rows[i][textname], "</option>" }), new object[0]);
                }
            }
            return sb.ToString();
        }

------解决方案--------------------
两个选择,一是将数据源中数据拼成字符串放到页面隐藏控件中,然后页面加载时用Js取隐藏控件值拆分并给Select绑定,二是像1楼一样拼一个Select
------解决方案--------------------
Sub HtmlSelect(ByVal HtmlSelect As HtmlSelect, ByVal dt As DataTable, ByVal Text As String, ByVal Value As String)
HtmlSelect.Items.Add(New ListItem("请选择", ""))
Dim i As Integer
For i = 0 To dt.Rows.Count - 1
With dt.Rows(i)
HtmlSelect.Items.Add(New ListItem(.Item(Text), .Item(Value)))
End With
Next
End Sub