可以直接用DataReader,比如
SqlConnection con=new SqlConnection("...");
SqlCommand cmd=new SqlCommand("select id,name from yourtable",con);
SqlDataReader dataReader=cmd.ExecuteReader();
DropDownList1.DataSource=dataReader;
DropDownList1.DataTextField="name";
DropDwonList1.DataValueField="id";
DropDwonList1.DataBind();
con.Close();
SqlConnection con=new SqlConnection("...");
SqlCommand cmd=new SqlCommand("select id,name from yourtable",con);
SqlDataReader dataReader=cmd.ExecuteReader();
DropDownList1.DataSource=dataReader;
DropDownList1.DataTextField="name";
DropDwonList1.DataValueField="id";
DropDwonList1.DataBind();
con.Close();
解决方案 »
- data类型和string类型的转换
- 系统goset同事的电脑,登陆csdn,竟显示同事的信息
- GridView学习总结
- 在asp.net中怎么获取服务器的时间
- 如何实现实时数据推送
- 为什么更新后没反应,也没提示错误
- 关于web.config配置的问题Server Error in '/' Application. 服务器是windows2003系统
- 如何在子窗口中更新父窗口的DataGrid??
- 如何在ASP.NET中实现,“当按按纽要删除的时候,用跳出提示框来询问!”
- 请教:DataGrid的分页问题?
- .net framework sdk的安装问题
- 在线求助:代码分离中,在Html中写的Javascript脚本函数,在C#中如何调用??
string id=DropDownList1.SelectedItem.value;
string text=DropDownList1.SelectedItem.Text;
You can set DataSource and set other property will ok!
You havnt need ArrayLis!
this two property is field in table of the database !
你只要榜定两个数据库中的字段就可以了!一帮情况下榜定一个ID和一个Caption就可以了!
2.可以直接用DataSet
3.可以直接用DataView
DropDwonList1.DataValueField="id";
的数值设置成一样吗?我的程序如下:
Source = new SqlConnection("data source=CYT;initial catalog=TK;persist security info=True;user id=sa;workstat" +
"ion id=CYT;packet size=4096");
Source.Open();
DataSet DS = new DataSet();
SqlDataAdapter NComm = new SqlDataAdapter(d, Source);
NComm.Fill(DS, "staff");
//Session["NowDS"]=DS;//宣告为Session
a.DataSource=DS.Tables["staff"].DefaultView;
a.DataTextField="JLR_NAME";
a.DataValueField="JLR_KIND";
a.DataBind();
private void Button1_Click(object sender, System.EventArgs e)
{
b.Text=a.SelectedItem.Text;
}但是按按钮后,文本框b的数值一直保持a中默认的第一个值!!!、????而且a中的值也自动变回默认的第一个值!
这句的意思是获得a中索引值最小的值的TEXT阿!!!!有谁知道应该怎样做阿?
DropDwonList1.DataValueField="id";
的数值设置成一样吗?可以
"ion id=CYT;packet size=4096");
Source.Open();
DataSet DS = new DataSet();
SqlDataAdapter NComm = new SqlDataAdapter(d, Source);
NComm.Fill(DS, "staff");
//Session["NowDS"]=DS;//宣告为Session
a.DataSource=DS.Tables["staff"].DefaultView;
a.DataTextField="JLR_NAME";
a.DataValueField="JLR_KIND";
a.DataBind();
放到
if(!Page.IsPostBack)
{
.........
}
里面
Dim arr(4, 2)
arr(0, 0) = "x"
arr(0, 1) = "1"
arr(1, 0) = "xx"
arr(1, 1) = "2"
arr(2, 0) = "xxx"
arr(2, 1) = "3"
arr(3, 0) = "xxxx"
arr(3, 1) = "4"
For i = 0 To 3
DDL.Items.Add(New ListItem(arr(i, 0), arr(i, 1)))
Next