各位老大,我是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)
{ }
}
<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)
{ }
}
<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>
<asp:ListItem Selected="True" Value="sss">all </asp:ListItem>
</asp:DropDownList>
中Value等于啥才能起到效果?
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();
<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();
}
<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: 表名无效 错误
==
string strSql = "SELECT YGBM, NAME, SEX, BRI, DWDM, XUELDM, ZHIWDM FROM YG_YGXX where 1 = 1";
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();
}
<asp:GridView ID="grvProducts" runat="server" AllowPaging="true" OnPageIndexChanging="grvProducts_PageIndexChanging">
排序的问题怎么解决呢?GridView“GridView1”激发了未处理的事件“Sorting”。
@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的字符串定界符是单引号,所以需要转义,用两个单引号表示一个单引号
<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",这次我们用到是刚才刚创建的存储过程