下拉列表,DropDownList 同时显示静态和动态数据
程序员文章站
2022-06-07 19:44:53
...
.aspx
动态下拉:
<asp:DropDownList ID="DropDownList1" runat="server" >
<asp:ListItem Selected="True">----动态下拉----</asp:ListItem>
<asp:ListItem>123456</asp:ListItem>
</asp:DropDownList>
--------------------------------------------------------------
.cs
protected void Page_Load(object sender, EventArgs e) { SqlConnection myConnection = new SqlConnection("Data Source=ZL4306;Initial Catalog=kk;User ID=sa");//Data Source=这里是数据库服务器的主机名,Initial Catalog=这里是数据库名,ID=这里是登陆用户名 SqlDataAdapter myCommand = new SqlDataAdapter("select dmsm1,dmz from kk_code where dmlb='6'", myConnection); DataSet ds = new DataSet(); myCommand.Fill(ds, "dmsm1"); DropDownList1.DataSource = ds.Tables["dmsm1"].DefaultView; DropDownList1.DataTextField = "dmz"; DropDownList1.DataValueField = "dmz"; DropDownList1.DataBind(); DropDownList1.Items.Insert(0, "----请选择----"); //这个就是插入的静态的数据 }
上一篇: Python常见用法汇总