DropDownList绑定数据库中数据问题 查询语句写在数据库的一个存储过程中,查询出一个表字段,大概有几十条记录。如何将这些存储过程查询出的记录绑定在DropDownList控件上,小弟愚蠢,请各位高手指点,谢谢。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 直接把查询出来的结果绑定在DropDownList上就可以了啊 DropDownList1.DataSource=读取出来的数据源;DropDownList1.DataBind();------------ http://www.voddh.com QVOD免费电影网--------------- 参考这些,看看是怎样做绑定的:http://www.cnblogs.com/insus/articles/1431940.htmlhttp://www.cnblogs.com/insus/articles/1411016.html DropDownList1.Items.Add(table1);你试一下看看行不行,我没测试过 DropDownList1.DataSource=读取出来的数据源;DropDownList1.DataTextField= "表里对应要显示的text字段名"; DropDownList1.DataTextValue= "表里对应要显示的value字段名"; DropDownList1.DataBind(); HTML code<asp:datagrid id="DataGrid1" runat="server" AutoGenerateColumns="False" DataKeyField="au_id"> <Columns> <asp:BoundColumn DataField="au_id" HeaderText="au_id" /> <asp:TemplateColumn HeaderText="state"> <ItemTemplate> <asp:DropDownList id="DropDownList1" runat="server" DataSource='<%# GetStates() %>' DataTextField="state" Text='<%# Da… private void BindGrid(){ SqlConnection cn = new SqlConnection(@"server=.\SQLExpress;uid=sa;pwd=;database=pubs"); SqlDataAdapter da = new SqlDataAdapter("select au_id, state from authors", cn); DataSet ds = new DataSet(); cn.Open(); da.Fill(ds); cn.Close(); DataGrid1.DataSource = ds; DataGrid1.DataKeyField = "au_id"; DataGrid1.DataBind();}protected DataTable GetStates(){ SqlConnection cn = new SqlConnection(@"server=.\SQLExpress;uid=sa;pwd=;database=pubs"); SqlDataAdapter da = new SqlDataAdapter("select distinct state from authors", cn); DataSet ds = new DataSet(); cn.Open(); da.Fill(ds); cn.Close(); return ds.Tables[0];}protected void Page_Load(object sender, EventArgs e){ if (!IsPostBack) { BindGrid(); }}protected void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e){ DropDownList dropTemp; string strState; if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { strState = ((DataRowView)e.Item.DataItem).Row["state"].ToString(); //对DropDownList做数据绑定 dropTemp = e.Item.FindControl("DropDownList2") as DropDownList; if (dropTemp != null) { SqlConnection cn = new SqlConnection(@"server=.\SQLExpress;uid=sa;pwd=;database=pubs"); string strSql = "select distinct state from authors"; SqlCommand cmd = new SqlCommand(strSql, cn); cn.Open(); dropTemp.DataSource = cmd.ExecuteReader(); dropTemp.DataTextField = "state"; dropTemp.DataBind(); cn.Close(); //到DropDownList中根据type的值去找需要设置为选中状态的项目,将其设置为选中 ListItem item = dropTemp.Items.FindByText(strState); if (item != null) { item.Selected = true; } } }} this.DropDownList1.DataSource = ds.Tables[0].DefaultView; this.DropDownList1.DataValueField = "name2"; this.DropDownList1.DataTextField = "name2"; this.DropDownList1.DataBind(); string sql ="select id,name from table"; this.DropDownList1.DataSource = ds.Tables[0].DefaultView; this.DropDownList1.DataValueField = "id"; this.DropDownList1.DataTextField = "name"; this.DropDownList1.DataBind();我就是这样用的,结果是对的 if(!Ispostback){sqlconnection con=new sqlconnection("....");string str="select * from tb_friend";sqldatadapter ada=new sqldataadapter(str,con);dataset ds=new dataset();ada.fill(ds,"tb_friend");dropdownlist1.datasource=ds;dropdownlist1.datavaluefield="friendemail";//绑定某个字段dropdownlist1.databind();con.close();} 点下dropdownlist会有一个小图标,点下消图标会出现一个选择数据源,可以再这里对dropdownlist进行绑定,代码是自动生成的,里边有对你要绑定的数据有高级用法,可以自己写SQL语句想要得到什么都可以, 必须要用代码的么?打开dropdownlist 右上角的智能图标 选择数据源 新建 sql数据库 默认名称就行然后 选择连接字符串 然后 你慢慢看看 吧。呵呵。 同意楼上各位的假如查出的表结构是ID City1 北京2 上海3 深圳则绑定时一般为:this.DropDownList1.DataSource = 数据源;this.DropDownList1.DataValueField = "ID"; this.DropDownList1.DataTextField = "City"; this.DropDownList1.DataBind(); DropDownList1.DataSource=读取出来的数据源; DropDownList1.DataTextField= "表里对应要显示的字段名"; DropDownList1.DataTextValue= "表里对应要显示的字段名"; DropDownList1.DataBind(); 只要数据源没问题,这样写是完全可以的 请问淘宝那种标签切换搜索如何实现? 关于DropdownList 增加选项内容问题 做这样一个调查问卷多少钱? treeview问题 关于一个使用DataList问题 关于frame中传值的问题 为什么一个BUTTON按扭在点两次后才能进入它的事件啊? 急::dropdownlist如何动态填加(高分,在线等) 为什么我的开发环境中没有SYSTEM.IO这个包? 不可访问“System.IO.StreamWriter.StreamWriter()”,因为它受保护级别限制 Response.Write 如何实现这样的效果asp.
DropDownList1.DataBind();------------ http://www.voddh.com QVOD免费电影网---------------
http://www.cnblogs.com/insus/articles/1411016.html
你试一下看看行不行,我没测试过
DropDownList1.DataTextField= "表里对应要显示的text字段名";
DropDownList1.DataTextValue= "表里对应要显示的value字段名";
DropDownList1.DataBind();
HTML code
<asp:datagrid id="DataGrid1" runat="server" AutoGenerateColumns="False" DataKeyField="au_id">
<Columns>
<asp:BoundColumn DataField="au_id" HeaderText="au_id" />
<asp:TemplateColumn HeaderText="state">
<ItemTemplate>
<asp:DropDownList id="DropDownList1" runat="server" DataSource='<%# GetStates() %>' DataTextField="state" Text='<%# Da…
{
SqlConnection cn = new SqlConnection(@"server=.\SQLExpress;uid=sa;pwd=;database=pubs");
SqlDataAdapter da = new SqlDataAdapter("select au_id, state from authors", cn);
DataSet ds = new DataSet();
cn.Open();
da.Fill(ds);
cn.Close();
DataGrid1.DataSource = ds;
DataGrid1.DataKeyField = "au_id";
DataGrid1.DataBind();
}protected DataTable GetStates()
{
SqlConnection cn = new SqlConnection(@"server=.\SQLExpress;uid=sa;pwd=;database=pubs");
SqlDataAdapter da = new SqlDataAdapter("select distinct state from authors", cn);
DataSet ds = new DataSet();
cn.Open();
da.Fill(ds);
cn.Close();
return ds.Tables[0];
}protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
}
protected void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
{
DropDownList dropTemp;
string strState;
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
strState = ((DataRowView)e.Item.DataItem).Row["state"].ToString(); //对DropDownList做数据绑定
dropTemp = e.Item.FindControl("DropDownList2") as DropDownList;
if (dropTemp != null)
{
SqlConnection cn = new SqlConnection(@"server=.\SQLExpress;uid=sa;pwd=;database=pubs");
string strSql = "select distinct state from authors";
SqlCommand cmd = new SqlCommand(strSql, cn);
cn.Open();
dropTemp.DataSource = cmd.ExecuteReader();
dropTemp.DataTextField = "state";
dropTemp.DataBind();
cn.Close(); //到DropDownList中根据type的值去找需要设置为选中状态的项目,将其设置为选中
ListItem item = dropTemp.Items.FindByText(strState);
if (item != null)
{
item.Selected = true;
}
}
}
}
this.DropDownList1.DataValueField = "name2";
this.DropDownList1.DataTextField = "name2";
this.DropDownList1.DataBind();
this.DropDownList1.DataSource = ds.Tables[0].DefaultView;
this.DropDownList1.DataValueField = "id";
this.DropDownList1.DataTextField = "name";
this.DropDownList1.DataBind();我就是这样用的,结果是对的
{
sqlconnection con=new sqlconnection("....");
string str="select * from tb_friend";
sqldatadapter ada=new sqldataadapter(str,con);
dataset ds=new dataset();
ada.fill(ds,"tb_friend");
dropdownlist1.datasource=ds;
dropdownlist1.datavaluefield="friendemail";//绑定某个字段
dropdownlist1.databind();
con.close();
}
然后 选择连接字符串 然后 你慢慢看看 吧。呵呵。
ID City
1 北京
2 上海
3 深圳则绑定时一般为:
this.DropDownList1.DataSource = 数据源;
this.DropDownList1.DataValueField = "ID";
this.DropDownList1.DataTextField = "City";
this.DropDownList1.DataBind();
DropDownList1.DataTextField= "表里对应要显示的字段名";
DropDownList1.DataTextValue= "表里对应要显示的字段名";
DropDownList1.DataBind();
只要数据源没问题,这样写是完全可以的