各位:小弟是初学者,现在要用多个dropdownlist进行条件筛选,不用Button,用autopostback=true
这是我的2个dropdownlist代码,用多个dropdownlist应该用什么方法?
 
Protected Sub DrListedp_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DrListedp.SelectedIndexChanged
        Dim conn1 As OleDbConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("~/data/pers.mdb"))
        Dim sql As String
        conn1.Open()
        sql = "select * from base where 1=1"
        If (DrListedp.SelectedValue <> "all") Then
            sql += "and b_dep='" & DrListedp.SelectedValue & "'"            If (DrListsex.SelectedValue <> "all") Then
                sql += "and b_sex='" & DrListsex.SelectedValue & "'"
            End If
        Else
            If (DrListsex.SelectedValue <> "all") Then
                sql += "and b_sex='" & DrListsex.SelectedValue & "'"
            End If
        End If
        AccessDataSource1.SelectCommand = sql
        GridView1.DataSource = AccessDataSource1.Select(DataSourceSelectArguments.Empty)
        GridView1.DataBind()
    End Sub    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim conn1 As OleDbConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("~/data/pers.mdb"))
        Dim sql As String
        conn1.Open()
        sql = "select * from base"
        AccessDataSource1.SelectCommand = sql
        conn1.Close()
        GridView1.DataSource = AccessDataSource1.Select(DataSourceSelectArguments.Empty)
        GridView1.DataBind()    End Sub   
   
    Protected Sub DrListsex_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DrListsex.SelectedIndexChanged
        Dim conn1 As OleDbConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("~/data/pers.mdb"))
        Dim sql As String
        conn1.Open()
        sql = "select * from base where 1=1"
        If (DrListsex.SelectedValue <> "all") Then
            sql += "and b_sex='" & DrListsex.SelectedValue & "'"            If (DrListedp.SelectedValue <> "all") Then
                sql += "and b_dep='" & DrListedp.SelectedValue & "'"
            End If
        End If
        AccessDataSource1.SelectCommand = sql
        GridView1.DataSource = AccessDataSource1.Select(DataSourceSelectArguments.Empty)
        GridView1.DataBind()    End Sub
End Class

解决方案 »

  1.   

    和dropdownlist联动差不多,就是相当于多条件查询
      

  2.   

    怎能在Page_Load中每一次回发都绑定GridView1?只有第一次(not IsPostback)的时候才应该绑定。
      

  3.   

    多个DropdownList,那么就把你DrListedp_SelectedIndexChanged里边所做的事情复制到多个DropdownList的事件中,绑定GridView1的时候考虑多个DropdownList中的值来形成查询参数,就可以了。不过web这种东西用户体验不好、服务器负担重,其实并不适合频繁地交互。最好是不使用autopostback,而是放一个Button,用户点击Button的时候才开始查询。
      

  4.   

    pageload必须要有not IsPostback不然你的dropdownlist每次都是初始化值,只是在dropdownlist SelectedIndexChanged里面绑定下一级的dropdownlist值就可以了
      

  5.   

    绑定数据的时候,根据每个dropdownlist的值来获取就是了
    如果需要联动,恩,貌似dropdownlist联动的代码四处可见
      

  6.   

    恩,sp1234已经说得很清楚了
    page_load要加if not ispostback,才不会每次都重新绑定多个dropdownlist和一个,其实差不多,就是每个dropdownlist的SelectedIndexChanged都要判断所有的dropdownlist是什么状态,把各个条件拼齐,别漏了就行了
      

  7.   

    多个dropdownlist和一个,其实差不多,就是每个dropdownlist的SelectedIndexChanged都要判断所有的dropdownlist是什么状态,把各个条件拼齐,别漏了就行了恩,我也这样想,不过如果像我这样用if语句,用多个dropdownlist以后if循环过多,那有没有什么方法代码可以简单一点?
    sp1234兄建议用button,不过最多2,3人用,可以不用考虑服务器负担,用autopostback方便一些
      

  8.   

    根据SelectedIndexChanged事件判断每个DropDownList的选中状态,构造SQL语句!~~
    可以写一个方法,在每次SelectedIndexChanged时遍历一下各个DropDownList,取得所有选中值添加到HashTable里,再构造SQL语句,不知道这样会不会方便一点!~~
      

  9.   

    page_load要加if not ispostback,才不会每次都重新绑定 多个dropdownlist和一个,其实差不多,就是每个dropdownlist的SelectedIndexChanged都要判断所有的dropdownlist是什么状态,把各个条件拼齐,别漏了就行了 
     
      

  10.   

    page_load中把绑定事件写在        if (!IsPostBack){}里面。
      

  11.   

    主要是每个dropdownlist条件涉及“空”,“非空”,“all”