关于下拉选择框
我要自己订制下拉列表中的内容,从表中读取并做处理,请问.net下如何实现?
<td style= "width: 135px ">
<asp:DropDownList ID= "fulei " runat= "server "> </asp:DropDownList>
<%call show_fulei%> </td>
' <%call show_fulei%> 只能放在droplist外面,里面不让放
sub show_fulei
Dim connstr As String = ConfigurationSettings.AppSettings( "ConnectionString ")
Dim queryString As String = "select id,mingcheng from lanmu where fulei = 0 order by paixu "
Using connection As New OdbcConnection(connstr)
Dim command As New OdbcCommand(queryString, connection)
connection.Open()
Dim reader As OdbcDataReader = command.ExecuteReader()
While reader.Read()
'response.write(reader.getString(0)& " | "&reader.getString(1) & " <br> ")
response.Write( " <asp:ListItem Value= " " " & reader.getString(0) & " " "> " & reader.getString(1) & " </asp:ListItem> ")
End While
reader.Close()
End Using
end sub
------解决方案--------------------fulei.DataSource = 此处为数据源
fulei.DataTextField = "你要显示的字段名称 ";
fulei.DataValueField = "×× ";fulei的value值
fulei.DataBind();
------解决方案--------------------在一个DropDownList 里显示这些吗?还是用2个好些吧
------解决方案--------------------试一下
用select * from [TableName]获取所有的数据填充到数据集中,如ds然后
void CreateDropList(ListItem itm)
{
DataView dv = new DataView()
dv.Table = ds.Tables[0];
dv.RowFilter = (itm == null) ? "id=0 " : string.Format( "fu={0} ",itm.Value);
foreach(DataRowView drv in dv)
{
ListItem item = new ListItem();
item.Text = drv[ "mingcheng "].ToString();
item.Value = drv[ "id "].ToString();
fulei.Items.Add(item);
CreateDropList(item);
}
}
第一次调用此方法使用CreateDropList(null);