我将含有参数的查询语句结果绑定到DropDrowList中,代码如下:
public void getUnitName()
{
string un =Session["userName"].ToString();
string connStr = "server=.;database=tlxt;uid=sa;pwd=;integrated security=SSPI;persist security info=False";
SqlConnection sqlConn = new SqlConnection(connStr);
string queryStr = "select ItemInf_Name from xt_ItemInf where Item_Username='" + un + "'";
DataSet ds = new DataSet();
SqlDataAdapter adapter =new SqlDataAdapter (queryStr ,connStr );
adapter.Fill(ds, "xt_ItemInf");
DropDownList1.DataSource = ds;
DropDownList1.DataBind(); }
但是绑定后的结果却不对,DropDrowList中绑定单元的名称变成了System.Data.DataRowView,而不是查询到的数据,还请大家帮帮忙。
public void getUnitName()
{
string un =Session["userName"].ToString();
string connStr = "server=.;database=tlxt;uid=sa;pwd=;integrated security=SSPI;persist security info=False";
SqlConnection sqlConn = new SqlConnection(connStr);
string queryStr = "select ItemInf_Name from xt_ItemInf where Item_Username='" + un + "'";
DataSet ds = new DataSet();
SqlDataAdapter adapter =new SqlDataAdapter (queryStr ,connStr );
adapter.Fill(ds, "xt_ItemInf");
DropDownList1.DataSource = ds;
DropDownList1.DataBind(); }
但是绑定后的结果却不对,DropDrowList中绑定单元的名称变成了System.Data.DataRowView,而不是查询到的数据,还请大家帮帮忙。
解决方案 »
- 求怎么在网页运行前加载开场动画。进者帮顶有分
- 实际项目中 ,字段名应该是开头大写还是小写呢?
- Tab 菜单导航 js 刷新传值问题 急候高手相助!!!
- 想学asp.net 但不知从何入手,该从哪本书入手
- 500分寻求一个全动态生成FormView及其连接SqlDataSource的例子,FormView可进行增删改
- 紧急求助,客户端脚本Id问题,有高分!!
- 如何提交带有图片的文章?大家帮忙顶!
- 在已经绑定好的DataGrid,加入一个新列,怎么加
- 关于session的问题
- 求关于visual studio 7.0 中关于asp.net 运用的书
- 怎么保存一个用户搜索过的若干关键词,放在搜索框旁边,点击后可以回到搜索框中
- Vb.net 怎样实现点一个按钮弹出窗口->处理->再关闭窗口.
this.DropDownList1.DataTextField = "显示文本";// ItemInf_Name ?
this.DropDownList1.DataValueField = "隐藏值";
this.DropDownList1.DataBind();
this.DropDownList1.DataValueField = "隐藏值";
这两句什么意思啊,什么是显示文本,什么又是隐藏值?
DropDownList1.DataValueField = "temInf_Name"; DataTextField和DataValueField可以绑定不同的数据表字段。
完整代码如下:
public void getUnitName()
{
string un =Session["userName"].ToString();
string connStr = "server=.;database=tlxt;uid=sa;pwd=;integrated security=SSPI;persist security info=False";
SqlConnection sqlConn = new SqlConnection(connStr);
string queryStr = "select ItemInf_Name from xt_ItemInf where Item_Username='" + un + "'";
DataSet ds = new DataSet();
SqlDataAdapter adapter =new SqlDataAdapter (queryStr ,connStr );
adapter.Fill(ds, "xt_ItemInf");
DropDownList1.DataSource = ds; DropDownList1.DataTextField = "temInf_Name";
DropDownList1.DataValueField = "temInf_Name"; DropDownList1.DataBind(); }
public void getUnitName()
{
string un =Session["userName"].ToString();
string connStr = "server=.;database=tlxt;Trusted_connection=true"; //信任连接
SqlConnection sqlConn = new SqlConnection(connStr);
string queryStr = "select ItemInf_Name from xt_ItemInf where Item_Username='" + un + "'";
TRY{
sqlconn.open();//打开数据源
DataSet ds = new DataSet();
SqlDataAdapter adapter =new SqlDataAdapter(queryStr,connStr);
adapter.Fill(ds);
sqlConn.close();//关闭数据源
this.DropDownList1.DataSource = ds;
this.DropDownList1.DataTextField = "设置显示字段";//要在下拉列表中显示的文字
this.DropDownList1.DataValueField = "设置值字段"; //也就是Value的值一般是这个表的ID因为一般不用它 this.DropDownList1.DataBind();//绑定数据源
}
CATCH(Sqlexception ee){
Response.write("<script>alert('数据库出错,错误为:'"+ee.message.ToString()+"'');</script>" );
}
finally{
sqlConn.close();
} }
DropDownList1.DataTextField="ItemInf_Name" ;//这个是在DropDownList显示的文本
DropDownList1.DataBind();
如果没有设置DataValueField的话,DropDownList的SelectedValue与显示文本相同
{
string un =Session["userName"].ToString();
string connStr = "server=.;database=tlxt;uid=sa;pwd=;integrated security=SSPI;persist security info=False";
SqlConnection sqlConn = new SqlConnection(connStr);
string queryStr = "select ItemInf_Name from xt_ItemInf where Item_Username='" + un + "'";
DataSet ds = new DataSet();
SqlDataAdapter adapter =new SqlDataAdapter (queryStr ,connStr );
adapter.Fill(ds, "xt_ItemInf");
DropDownList1.DataTextField = "ItemInf_Name";
DropDownList1.DataValueField = "ItemInf_Name";
DropDownList1.DataSource = ds;
DropDownList1.DataBind(); }
this.DropDownList1.DataValueField = "隐藏值";
没有这两句话,你就没有指定你要绑定到哪个字段
如果你的表里面有10个字段,你不写这两句话,你想你的DROPDOWNLIST绑定哪两个字段呀
想用ddl显示数据源,就需要指定DataTextField,DataValueField 必须指定
你这样绑定
DropDownList1.DataSource = ds;
DropDownList1.DataBind(); 请问,ddl下拉的时候显示什么内容?显示的内容又是什么值?这种基础真是......无话可说
this.DropDownList1.DataValueField = "隐藏值";
这两句什么意思啊,什么是显示文本,什么又是隐藏值?-------
DropDownList显示文本就是选择的时候显示的一排文本,隐藏值就是对应文本的值
this.DropDownList1.DataSource = ds; //赋数据源
this.DropDownList1.DataTextField = "字段名称";// 你需要显示的字段
this.DropDownList1.DataValueField = "隐藏值"; //这个就相当于绑定下的Value值,当你选择某项时,
可以取得该选择项的value值this.DropDownList1.SelectedValue
this.DropDownList1.DataBind();//呈现数据