各位老大,我是c#初学者,最近我想通过急于求成的方式取得效果,就是利用多个dropdownlist和gridview组合实现对数据库组合查询功能,其实就是用不写代码的方式,把gridview连接的数据库绑到dropdownlist上,现在我已经实现在每个dropdownlist选择相应内容的情况下显示相应的数据,但我想在每个dropdownlist上加个项,如“不选择”或“all",使这个dropdownlist不起作用,请问,有没有简便的办法实现这个功能,---------问题很菜,请各位老大见谅。<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource1"
            DataTextField="DWMC" DataValueField="DWDM" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" AppendDataBoundItems="True">
            <asp:ListItem Selected="True" Value=*>all</asp:ListItem>
        </asp:DropDownList>
        <asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource2"
            DataTextField="SEX" DataValueField="SEX">
            <asp:ListItem Selected="True" Value="sss">all</asp:ListItem>
        </asp:DropDownList><asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:drop2 %>"
            ProviderName="<%$ ConnectionStrings:drop2.ProviderName %>" SelectCommand='SELECT * FROM "YG_SEX"'>
        </asp:SqlDataSource>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:drop1 %>"
            ProviderName="<%$ ConnectionStrings:drop1.ProviderName %>" SelectCommand='SELECT * FROM "YG_DWDM"'>
        </asp:SqlDataSource>
        <asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource3" AllowPaging="True" AllowSorting="True">
            <Columns>
                <asp:BoundField DataField="YGBM" HeaderText="YGBM" SortExpression="YGBM" />
                <asp:BoundField DataField="NAME" HeaderText="NAME" SortExpression="NAME" />
                <asp:BoundField DataField="SEX" HeaderText="SEX" SortExpression="SEX" />
                <asp:BoundField DataField="BRI" HeaderText="BRI" SortExpression="BRI" />
                <asp:BoundField DataField="DWDM" HeaderText="单位" SortExpression="DWDM" />
                <asp:BoundField DataField="XUELDM" HeaderText="XUELDM" SortExpression="XUELDM" />
                <asp:BoundField DataField="JSZJQX" HeaderText="JSZJQX" SortExpression="JSZJQX" />
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:ygxx %>"
            ProviderName="<%$ ConnectionStrings:ygxx.ProviderName %>" SelectCommand='SELECT * FROM "YG_YGXX" WHERE (("SEX" = :SEX) AND ("DWDM" = :DWDM))'>
            <SelectParameters>
                <asp:ControlParameter ControlID="DropDownList2" Name="SEX" PropertyName="SelectedValue"
                    Type="String" />
                <asp:ControlParameter ControlID="DropDownList1" Name="DWDM" PropertyName="SelectedValue"
                    Type="String" />
            </SelectParameters>
        </asp:SqlDataSource>
    
    </div>
    </form>
</body>
</html>.cs:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {    }
}

解决方案 »

  1.   

    <asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource2" DataTextField="SEX" DataValueField="SEX" >
                <asp:ListItem Selected="True" Value="sss">all </asp:ListItem> 
    </asp:DropDownList> 
    ==
    <asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource2" DataTextField="SEX" DataValueField="SEX" AppendDataBoundItems="True"> > 
                <asp:ListItem Selected="True" Value="sss">all </asp:ListItem> 
    </asp:DropDownList> 
      

  2.   

    老大又是您热情解答,这个我知道了,现在是
          <asp:ListItem Selected="True" Value="sss">all </asp:ListItem> 
    </asp:DropDownList> 
    中Value等于啥才能起到效果?
      

  3.   

    参考:         DropDownList age = new DropDownList();
            DropDownList sex = new DropDownList();
            DropDownList address = new DropDownList();        string sqlstr = "select * from tb_students where age=" + age.SelectedValue
                           + " and sex='" + sex.SelectedValue + "' and  address like'"
                            + address.SelectedValue + "%'";        GridView gv1 = new GridView();
            gv1.DataSource = SqlHelper.ExecuteReader(执行上个语句);
            gv1.DataBind();
      

  4.   

    谢谢楼上,但没解决dropdownlist为空时(或all)时的问题
      

  5.   

    你必须在程序中动态设置你的SelectCommand
      

  6.   

    aspx
    <asp:DropDownList ID="dropCategoryID" runat="server" AppendDataBoundItems="True" DataSourceID="sdsCategoryID" DataTextField="CategoryName" DataValueField="categoryid">
        <asp:ListItem Value="0">All</asp:ListItem>
    </asp:DropDownList>
    <asp:DropDownList ID="dropSupplierID" runat="server" AppendDataBoundItems="True" DataSourceID="sdsSupplierID" DataTextField="CompanyName" DataValueField="SupplierID">
        <asp:ListItem Value="0">All</asp:ListItem>
    </asp:DropDownList>
    <asp:Button ID="btnQuery" runat="server" Text="查询" OnClick="btnQuery_Click" />
    <asp:GridView ID="grvProducts" runat="server">
    </asp:GridView>
    <asp:SqlDataSource ID="sdsCategoryID" runat="server" ConnectionString="Data Source=.\sqlexpress;Initial Catalog=Northwind;Integrated Security=True" ProviderName="System.Data.SqlClient" SelectCommand="select categoryid,CategoryName from Categories"></asp:SqlDataSource>
    <asp:SqlDataSource ID="sdsSupplierID" runat="server" ConnectionString="Data Source=.\sqlexpress;Initial Catalog=Northwind;Integrated Security=True" ProviderName="System.Data.SqlClient" SelectCommand="select SupplierID, CompanyName from Suppliers"></asp:SqlDataSource>
    <asp:SqlDataSource ID="sdsProducts" runat="server" ConnectionString="Data Source=.\sqlexpress;Initial Catalog=Northwind;Integrated Security=True" ProviderName="System.Data.SqlClient"></asp:SqlDataSource>protected void btnQuery_Click(object sender, EventArgs e)
    {
        string strSql = "select * from products where 1 = 1";    if (dropCategoryID.SelectedValue != "0")
            strSql += " and CategoryID = '" + dropCategoryID.SelectedValue + "'";    if (dropSupplierID.SelectedValue != "0")
            strSql += " and SupplierID = '" + dropSupplierID.SelectedValue + "'";    sdsProducts.SelectCommand = strSql;    grvProducts.DataSource = sdsProducts.Select(DataSourceSelectArguments.Empty);
        grvProducts.DataBind();
    }
      

  7.   

    sdsCategoryID和sdsSupplierID这两个SqlDataSource是用来绑定两个DropDownList的sdsProducts是用来绑定GridView的点击查询按钮,根据两个DropDownList的选择来动态地决定sdsProducts的SelectCommand ,再去绑定GridView
      

  8.   

    string sqlCommand="select * from table where id='"+this.dropdownlist.text+"' and name='"+this.dropdownlist.text+"'";
      

  9.   

    <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>无标题页</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        
        </div>
            <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource2"
                DataTextField="DWMC" DataValueField="DWDM" AppendDataBoundItems="True">
                <asp:ListItem Value="0">all</asp:ListItem>
            </asp:DropDownList><asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
                ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" SelectCommand='SELECT * FROM "YG_DWDM"'>
            </asp:SqlDataSource>
            <asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="SqlDataSource3"
                DataTextField="SEX" DataValueField="SEXDM" AppendDataBoundItems="True">
                <asp:ListItem Value="0">all</asp:ListItem>
            </asp:DropDownList><asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
                ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" SelectCommand='SELECT * FROM "YG_SEX"'>
            </asp:SqlDataSource>
            <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" AllowPaging="True" AllowSorting="True">
                <Columns>
                    <asp:BoundField DataField="YGBM" HeaderText="YGBM" SortExpression="YGBM" />
                    <asp:BoundField DataField="NAME" HeaderText="NAME" SortExpression="NAME" />
                    <asp:BoundField DataField="SEX" HeaderText="SEX" SortExpression="SEX" />
                    <asp:BoundField DataField="BRI" HeaderText="BRI" SortExpression="BRI" />
                    <asp:BoundField DataField="DWDM" HeaderText="DWDM" SortExpression="DWDM" />
                    <asp:BoundField DataField="XUELDM" HeaderText="XUELDM" SortExpression="XUELDM" />
                    <asp:BoundField DataField="ZHIWDM" HeaderText="ZHIWDM" SortExpression="ZHIWDM" />
                </Columns>
            </asp:GridView>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
                ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" SelectCommand='SELECT "YGBM", "NAME", "SEX", "BRI", "DWDM", "XUELDM", "ZHIWDM" FROM "YG_YGXX"'>
            </asp:SqlDataSource>
        </form>
    </body>
    </html>
    C#using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;public partial class _Default : System.Web.UI.Page 
    {
        protected void Page_Load(object sender, EventArgs e)
        {    }
        protected void Button1_Click(object sender, EventArgs e)
        {
            string strSql = "SELECT 'YGBM', 'NAME', 'SEX', 'BRI', 'DWDM', 'XUELDM', 'ZHIWDM' FROM 'YG_YGXX' where 1 = 1";        if (DropDownList1.SelectedValue != "0")
                strSql += " and DWDM = '" + DropDownList1.SelectedValue + "'";        if (DropDownList2.SelectedValue != "0")
                strSql += " and SEX = '" + DropDownList2.SelectedValue + "'";        SqlDataSource1.SelectCommand = strSql;        GridView1.DataSource = SqlDataSource1.Select(DataSourceSelectArguments.Empty);
            GridView1.DataBind();    }
    }点Button,出现ORA-00903: 表名无效 错误
      

  10.   

    string strSql = "SELECT 'YGBM', 'NAME', 'SEX', 'BRI', 'DWDM', 'XUELDM', 'ZHIWDM' FROM 'YG_YGXX' where 1 = 1"; 
    ==
    string strSql = "SELECT YGBM, NAME, SEX, BRI, DWDM, XUELDM, ZHIWDM FROM YG_YGXX where 1 = 1"; 
      

  11.   

    你在创建oracle这些表和字段的时候大小写是如何的?尤其是 YG_YGXX表
      

  12.   

    给你一个帖子参考http://topic.csdn.net/t/20050717/12/4149729.html
      

  13.   

    谢谢,功能已经实现,但成功只有最后一步,点击分页时,即点击第二页时出现错误:GridView“GridView1”激发了未处理的事件“PageIndexChanging”。 
      

  14.   


    private void BindGridView()

        string strSql = "select * from products where 1 = 1";    if (dropCategoryID.SelectedValue != "0")
            strSql += " and CategoryID = '" + dropCategoryID.SelectedValue + "'";    if (dropSupplierID.SelectedValue != "0")
            strSql += " and SupplierID = '" + dropSupplierID.SelectedValue + "'";    sdsProducts.SelectCommand = strSql;
        grvProducts.DataSource = sdsProducts.Select(DataSourceSelectArguments.Empty);
        grvProducts.DataBind();    
    }protected void btnQuery_Click(object sender, EventArgs e)
    {
        BindGridView();
    }protected void grvProducts_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grvProducts.PageIndex = e.NewPageIndex;
        BindGridView();
    }
      

  15.   

    前台
    <asp:GridView ID="grvProducts" runat="server" AllowPaging="true" OnPageIndexChanging="grvProducts_PageIndexChanging">
      

  16.   

    这个问题,成功解决。
    排序的问题怎么解决呢?GridView“GridView1”激发了未处理的事件“Sorting”。
      

  17.   

    首先你在你得数据库里加一个存储过程create proc GetProcucts
    @categoryid int,
    @supplierid int
    as
    declare @sql nvarchar(4000)
    set @sql = 'select * from products where 1 = 1'

    if(@categoryid <> 0)
    set @sql = @sql + ' and CategoryID = ''' + Convert(nvarchar, @categoryid) + '''' if(@supplierid <> 0)
    set @sql = @sql + ' and supplierid = ''' + Convert(nvarchar, @supplierid) + '''' exec sp_executesql @sql
    go 请特别注意上面的
    set @sql = @sql + ' and CategoryID = ''' + Convert(nvarchar, @categoryid) + ''''红色的两个单引号表示一个单引号,不过因为sql的字符串定界符是单引号,所以需要转义,用两个单引号表示一个单引号
      

  18.   

    <asp:DropDownList ID="dropCategoryID" runat="server" AppendDataBoundItems="True" DataSourceID="sdsCategoryID" DataTextField="CategoryName" DataValueField="categoryid">
        <asp:ListItem Value="0">All</asp:ListItem>
    </asp:DropDownList>
    <asp:DropDownList ID="dropSupplierID" runat="server" AppendDataBoundItems="True" DataSourceID="sdsSupplierID" DataTextField="CompanyName" DataValueField="SupplierID">
        <asp:ListItem Value="0">All</asp:ListItem>
    </asp:DropDownList>
    <asp:Button ID="btnQuery" runat="server" Text="查询"/>
    <asp:GridView ID="grvProducts" runat="server" AllowPaging="True" AllowSorting="True" DataSourceID="sdsProducts">
    </asp:GridView>
    <asp:SqlDataSource ID="sdsCategoryID" runat="server" ConnectionString="Data Source=.\sqlexpress;Initial Catalog=Northwind;Integrated Security=True" ProviderName="System.Data.SqlClient" SelectCommand="select categoryid,CategoryName from Categories"></asp:SqlDataSource>
    <asp:SqlDataSource ID="sdsSupplierID" runat="server" ConnectionString="Data Source=.\sqlexpress;Initial Catalog=Northwind;Integrated Security=True" ProviderName="System.Data.SqlClient" SelectCommand="select SupplierID, CompanyName from Suppliers"></asp:SqlDataSource>
    <asp:SqlDataSource ID="sdsProducts" runat="server" ConnectionString="Data Source=.\sqlexpress;Initial Catalog=Northwind;Integrated Security=True" ProviderName="System.Data.SqlClient" SelectCommand="GetProcucts" SelectCommandType="StoredProcedure">
        <SelectParameters>
            <asp:ControlParameter ControlID="dropCategoryID" Name="categoryid" PropertyName="SelectedValue"
                Type="Int32" />
            <asp:ControlParameter ControlID="dropSupplierID" Name="supplierid" PropertyName="SelectedValue"
                Type="Int32" />
        </SelectParameters>
    </asp:SqlDataSource>这里特别要注意SelectCommandType="StoredProcedure",这次我们用到是刚才刚创建的存储过程
      

  19.   

    刚才,看了,但我的数据库是orcale,存储过程不知怎么加
      

  20.   

    鲜血,忘了这件事用存储过程你的程序会很简单,我明天到公司试试吧,这里没有oracle的环境
      

  21.   

    谢谢!明天新开帖吧使用存储过程写组合查询 向amandag老师提问 http://topic.csdn.net/u/20080613/01/0464c7ba-111b-4b94-9ed3-353c424c9755.html