protected void Page_Load(object sender, EventArgs e)
{
string strConnection="Provider=Microsoft.Jet.OleDb.4.0;Data Source=";
strConnection+=Server.MapPath("1.mdb");
OleDbConnection objConnection=new OleDbConnection (strConnection);
if(!IsPostBack)
{
string   sql2   =   "select   ClassID,Classname   from   DS_ClassInf";    
  OleDbDataAdapter   custDA   =new OleDbDataAdapter(sql2   ,   objConnection);   
  DataSet   custDS   =   new DataSet();   
  custDA.Fill(custDS);    
  DropDownList1.DataSource   =   custDS.Tables[0];   
  DropDownList1.DataTextField   =   "Classname";   
  DropDownList1.DataValueField   =   "ClassID";   
  DropDownList1.DataBind();
  this.DropDownList1.Items.Insert(0,new   ListItem("所有类别",""));
}
}
private void sousuo()
{
//搜索的语句
}
<asp:DropDownList ID="DropDownList1" runat="server" value="2" AutoPostBack="true" ></asp:DropDownList>
请问一下。应该还写什么样的代码才可以让这个下拉列表被选定的值来作为sousuo()里面的搜索语句的条件?。并且默认的时候为搜索所有。
还望专家们解说详细点。

解决方案 »

  1.   

    拼接sql字符串:
    StringBuilder sbWhere = new StringBuilder(128);
    sbWhere = sbWhere.Append("select   ClassID,Classname   from   DS_ClassInf where 1 = 1 ");if (this.DropDownList1.SelectedValue == "所有类别")
     { }
    else if(this.DropDownList1.SelectedValue == "Other")
    {
    sbWhere = sbWhere.Append(" and class = 1 ");
    }
      

  2.   

    郁闷。没说明白啊
    就是说。这个下拉列表框被选了以后。
    后面有一个搜索按钮。这个按钮把下列列表被选的项拿来做条件。搜索出数据来。
    我的问题是怎么来读取这个下拉列表的值。
    SelectIndexChange()应该怎么写啊?我不会。。望指教。
      

  3.   

    Aden(Aden):
    这段代码应该写在什么地方?
    直接写在Button的搜索函数sousuo()里面还是其他什么地方?
      

  4.   

    对阿,你把value的值传过去
    class = value 做条件进行查询
      

  5.   

    在DropDownList SelectIndexChange()里去做数据库的操作 传的值就是DropDownList1.SelectItem.value把DropDownList1d  AutoPostBack设置成true
      

  6.   

    查询数据时,然后绑定数据.sousuo里
      

  7.   

    if(DropDownList1.SelectValue.Trim().Length>0)
    {
       //example
       string sql = string.Format("select * From 表 Where ClassId={0}",DropDownList1.SelectValue.Trim().);
    }
      

  8.   

    按钮的onclick里面DropDownList1.SelectedValue
      

  9.   

    按照LZ的意思:this.DropDownList1.Items.Insert(0,new   ListItem("所有类别","-1"));然后
    <asp:Button id="searchBtn" runat="server" Text="搜索" OnClick="searchBtn_Click" />
    protected void searchBtn_Click(object sender,EventArgs e)
    {
     int Id=int.Parse(DropDownList1.SelectedValue);
     sousuo(int keyId)}
    然后
    private void sousuo(int keyId)
    {
     if(keyId==-1)
    //搜索的语句 ,搜索所有的记录
      else
      //搜索的语句,搜索指定classid的记录
    }
      

  10.   

    if(DropDownList1.SelectValue.Trim() != "")
    {
       ViewState["Filter"]= "ClassID = " + Convert.ToInt32(DropDownList1.SelectValue)
    }
    再用dvMember.RowFilter = ViewState["Filter"].ToString();筛选一下
      

  11.   

    大哥,我的搜索代码是下面这样的。我就是不会用这个。传递参数。
    这里应该怎么写啊。
    private void sousuo()
    {
     string strConnection="Provider=Microsoft.Jet.OleDb.4.0;Data Source=";
    strConnection+=Server.MapPath("1.mdb");
    OleDbConnection objConnection=new OleDbConnection (strConnection);
    String sql = "select bookname from chuhuo where classname=''";
    DataSet ds  = new DataSet();
    OleDbDataAdapter myada = new OleDbDataAdapter(sql,strConnection);
    myada.Fill(ds,"chuhuo");
    dgrdMain.DataSource = ds.Tables["chuhuo"].DefaultView;
    dgrdMain.DataBind(); 
    }
      

  12.   

    xray2005:
    好先进的方法。
      

  13.   

    郁闷。没说明白啊
    就是说。这个下拉列表框被选了以后。
    后面有一个搜索按钮。这个按钮把下列列表被选的项拿来做条件。搜索出数据来。
    我的问题是怎么来读取这个下拉列表的值。
    SelectIndexChange()应该怎么写啊?我不会。。望指教。
    -------------------------------------------------------------------------------
    既然后面有个搜索按钮,那么就不需要用SelectIndexChange()事件,也不需要autopostback,这样会影响效率。
    直接在按钮,如button1的Click事件中,取得dropdownlist的value值进行搜索,取得的方法是DropDownList1.SelectedValue,在查询时把这个作为变量传给SQL语句,查询前对DropDownList1.SelectedValue值进行判断,如果是默认值,那么SQL语句就不加条件,就是搜索所有值了~
      

  14.   

    int Id=int.Parse(DropDownList1.SelectedValue)
    这句是什么意思啊
      

  15.   

    DropDownList1.SelectedValue是string型,转成int
      

  16.   

    很简单,
    申明
    public string A="";
    在按钮的ONCLICK事件里写
    A=DropDownList1.Text.ToString();sousou里这样改
    string strConnection="Provider=Microsoft.Jet.OleDb.4.0;Data Source=";
    strConnection+=Server.MapPath("1.mdb");
    OleDbConnection objConnection=new OleDbConnection (strConnection);
    String sql = "select bookname from chuhuo where classname='"+A+"'";//条件在这里
    DataSet ds  = new DataSet();
    OleDbDataAdapter myada = new OleDbDataAdapter(sql,strConnection);
    myada.Fill(ds,"chuhuo");
    dgrdMain.DataSource = ds.Tables["chuhuo"].DefaultView;
    dgrdMain.DataBind(); 然后调用就可以了..
      

  17.   

    //这里改下就好了
    private void sousuo(string selectedValue)
    {
    SqlConnection con=new SqlConnection("connectionstring");
    SqlCommand cmd=new SqlCommand();
    cmd.Connection=con; if(selectedValue=="")
    cmd.CommandText="select * from table";
    else
    cmd.CommandText="select * from table where condition='"+selectedValue+"';

    ...
    //搜索的语句
    }
      

  18.   

    DropDownList1.SelectItem.value,这个就是选定的值!