在拼sql语句的时候,如果dropdownlist为空值,不加filter条件!

解决方案 »

  1.   

    你要根据DropDownList的选项来动态地拼接你的SQL语句
      

  2.   

                        <asp:DropDownList ID="DropDownList2" runat="server">
                            <asp:ListItem Value="0">全部</asp:ListItem>
                            <asp:ListItem Value="1">其他</asp:ListItem>
                            <asp:ListItem Value="2">初中及以下</asp:ListItem>
                            <asp:ListItem Value="3">高中/中专</asp:ListItem>
                            <asp:ListItem Value="4">大学专科</asp:ListItem>
                            <asp:ListItem Value="5">大学本科</asp:ListItem>
                            <asp:ListItem Value="6">研究生及双学士</asp:ListItem>
                            <asp:ListItem Value="7">博士及以上</asp:ListItem>
                        </asp:DropDownList></td>
    我这里VALUE 1-7 是写死的,你可以从数据库取的
      

  3.   

    谢谢大家,我的不是写死的,是动态,我也不知道应该贴些什么代码上来,因为我基本上都没有hard code。我datagrid SQL如下:
    SELECT Students.SchoolID, Students.LastName, Students.FirstName, ProgramsStudents.CourseID FROM ((Students INNER JOIN ProgramsStudents ON Students.Username = ProgramsStudents.UserName) INNER JOIN Programs ON ProgramsStudents.CourseID = Programs.CourseID) WHERE (Students.SchoolID = ?) AND (ProgramsStudents.CourseID = ?)
    其中Students.SchoolID和ProgramsStudents.CourseID 就是被dropdownlist控制的,请大家帮帮忙。谢谢
    两个dropdownlist的代码如下:
    Select School<br />
        <asp:DropDownList ID="DropDownListSchool" runat="server" DataSourceID="AccessDataSourceSelectSchool" DataTextField="SchoolName" DataValueField="SchoolID" AppendDataBoundItems="True" AutoPostBack="True">
        </asp:DropDownList><asp:AccessDataSource ID="AccessDataSourceSelectSchool" runat="server"
            DataFile="~/App_Data/GLC.mdb" SelectCommand="SELECT [SchoolName], [SchoolID] FROM [Schools]">
        </asp:AccessDataSource>
        <br />
        <br />
        Select Program<br />
        <asp:DropDownList ID="DropDownListProgram" runat="server" DataSourceID="AccessDataSourceSelectProgram" DataTextField="CourseID" DataValueField="CourseID" AppendDataBoundItems="True" AutoPostBack="True">
        </asp:DropDownList>
        
        <asp:AccessDataSource ID="AccessDataSourceSelectProgram" runat="server"
            DataFile="~/App_Data/GLC.mdb" SelectCommand="SELECT [CourseID] FROM [Programs]">
        </asp:AccessDataSource>
      

  4.   

    谢谢大家,已部分解决:
    加一个value 为 ""的item然后:
    Protected Sub DropDownListSchool_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownListSchool.SelectedIndexChanged
     
    If DropDownListSchool.SelectedValue = "" Then
    AccessDataSourceGridView.SelectParameters.Clear()
    AccessDataSourceGridView.SelectCommand = "SELECT Students.SchoolID, Students.LastName, Students.FirstName, ProgramsStudents.CourseID FROM ((Students INNER JOIN ProgramsStudents ON Students.Username = ProgramsStudents.UserName) INNER JOIN Programs ON ProgramsStudents.CourseID = Programs.CourseID) WHERE (ProgramsStudents.CourseID = )"
            End If
        End Sub
    可是我不知道ProgramsStudents.CourseID =后面怎么写了,我想用这个等于第二个dropdownlist的value,不知道在这个句SQL中如何实现?谢谢大家