日期:2014-05-18  浏览次数:20449 次

DropDownList按条件查询问题
我在ASP.NET界面放下一个TEXTBOX,一个DROPDWONLIST,datagrid,和一个按钮.DropDownList手动添加 "职工号 " "性别 " "学历 ", "部门号 "项(在表中有相应的字段),怎么样实现下面功能:
选择DropDownList的性别,在textbox3中输入 "男 "单击按钮,把查询结果绑定到datagrid.接着按别的项进行查询.我的代码如下:
在类定义protected   string   sql;
  DropDownList1事件:
private   void   DropDownList1_SelectedIndexChanged(object   sender,   System.EventArgs   e)//动态改变where后的字段
{
switch(DropDownList1.SelectedValue)
{
case "学历 ":sql= "select   Uno   as   '职工号 ',Uname   as   '姓名 ',Ueducation   as   '学历 '   from   [U_info]   where   Ueducation= ";break;
case "部门 ":sql= "select   Uno   as   '职工号 ',Uname   as   '姓名 ',Dno   as   '部门号 '   from   [U_info]   where   Dno= ";break;
case "职位 ":sql= "select   Uno   as   '职工号 ',Uname   as   '姓名 ',Uposition   as   '职位 '   from   [U_info]   where   Upoisition= ";break;
default:sql= "select   Uno   as   '职工号 ',Uname   as   '姓名 ',Usex   as   '姓别 '   from   [U_info]   where   Usex= ";break;

}
}
按钮事件:
private   void   ImageButton1_Click(object   sender,   System.Web.UI.ImageClickEventArgs   e)
{
string   input=TextBox3.Text;
                                                        sql=sql+input;//给where后面的字段付值.
                                                        DataAccess   db=new   DataAccess();
DataSet   ds=db.GetDataSet(sql);
DataGrid1.DataSource=ds;
DataGrid1.DataBind();

}
总是提示字符串的地方有错,望高手指点;

------解决方案--------------------
sql=sql+input+ " ' "
------解决方案--------------------
绑定datagrid
private void datagrid_bind(string strSql)
{
SqlDataAdapter sda=new SqlDataAdapter(strSql,strconn);
DataSet ds=new DataSet();
sda.Fill(ds);
this.DataGrid1.DataSource=ds.Tables[0].DefaultView;
this.DataGrid1.DataBind();
}
触发事件
private void Button1_Click(object sender, System.EventArgs e)
{
string strDDL=this.DropDownList1.SelectedValue.ToString();
string strTB=this.TextBox1.Text;
string strSql= "select id, "+ strDDL + " from testselect where "+strDDL+ "= ' "+strTB+ " ' ";
this.datagrid_bind(strSql);
}