asp.net下拉框从数据库中取值

根据表newstype表中的newstypeid在下拉框中显示newstypename

首先,你要通过Session传过来的值判断newstypeid,然后你就填代码,代码如下:
protected void DropDownList1_content()
{
string querystr ="select newstypename from newstype where newstypeid='"+Session["id"]+"'";
SqlDataAdapter myda=new SqlDataAdapter(querystr,myconn);
DataSet myds=new DataSet();
myda.Fill(myds,"dm");
DropDownList1.DataSource=myds.Tables["dm"].DefaultView;
DropDownList1.DataValueField = myds.Tables["dm"].Columns[0].ColumnName;
DropDownList1.DataTextField = myds.Tables["dm"].Columns[0].ColumnName;
DropDownList1.DataBind();
}
代码完成以后,在设计视图中你把DropDownList1的autopostback属性设为true就可以了
温馨提示:答案为网友推荐,仅供参考
第1个回答  2009-04-01
全套代码?
简单给你写一下关键的吧,直接现在手打,大小写不一定对

首先假设有一个方法
privte dataset getdata()
{}
可以从你的数据库中读取newstype表

然后在 假设ddlist是你的dropdownlist控件
page_load()
{
this.ddlist.datascore=getdate()
this.ddlist.datatextfield="newstypeid";//绑定显示文字
this.ddlist.datavaluefield="id";//绑定值,如果有ID的话
ddllist.databind();
}本回答被提问者采纳
第2个回答  2009-04-02
拖一个ObjectDataSocus,邦定你查找newstype的方法,然后把你的下单菜单和这个ObjectDataSocus邦定,设置一下显示的值和选中值就OK了
第3个回答  2009-04-01
SqlConnection con= new SqlConnection("server=serverName;database=数据库;uid=sa;pwd=密码;");
con.Open();
DataTable dt = new DataTable();
string sql="select newstypename from newstype";
SqlDataAdapter da = new SqlDataAdapter(sql,con);
da.Fill(dt);
this.dropDownList.DataSource=dt;
this.dropDownList.DataTextField="newstypename";
this.dropDownList.DataBind();
con.Close();

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网