实现gridview模糊搜索


1、添加控件如图:

2、配置gridview1的数据源,选择customers表的CustomerID、CompanyName、ContactName、Country字段。然后点“WHERE”进入“添加WHERE”子句界面,“列”选择CustomerID,“运算符”为like,"源"为control,“控件id”为TextBox1。点"添加"就可以了。然后再同样的方式设置CompanyName like  DropDownList1 。点添加,就可以了。3、双击“Search”按钮进入Default.aspx.cs添加代码段如下:

     protected   void  Button1_Click( object  sender, EventArgs e)
    
{
        SqlDataSource1.SelectCommand 
= "SELECT [CustomerID], [CompanyName], [ContactName], 

[Country] FROM [Customers] WHERE (([CustomerID] LIKE 
'%' + @CustomerID + '%') AND ([CompanyName] 

LIKE 
'%' + @CompanyName + '%'))";
    }


呵呵,这个SELECT语句我是从Default.aspx页面拿过来的。
然后把Default.aspx里原来的SELECT语句改为很简单的

SelectCommand = " SELECT [CustomerID], [CompanyName], [ContactName], [Country] FROM [Customers]  "


4、对DropDownList的设置如下:

      < asp:DropDownList  ID ="DropDownList1"  runat ="server" >
        <asp:ListItem Selected=True></asp:ListItem>
        <asp:ListItem  >a</asp:ListItem>
        <asp:ListItem >aa</asp:ListItem>
        <asp:ListItem >ab</asp:ListItem>
        
    
</ asp:DropDownList >


Selected=True相当是DropDownList的默认值。

5、这样会看不到东西,要把

             < SelectParameters >
                
< asp:ControlParameter  ControlID ="TextBox1"  Name ="CustomerID"  PropertyName ="Text"
                    Type
="String" />
                
< asp:ControlParameter  ControlID ="DropDownList1"  Name ="CompanyName"  
PropertyName
="SelectedValue"
                    Type
="String"     />
            
</ SelectParameters >

改为:

             < SelectParameters >
                
< asp:ControlParameter  ControlID ="TextBox1"  Name ="CustomerID"  PropertyName ="Text"
                    Type
="String"   ConvertEmptyStringToNull ="False" />
                
< asp:ControlParameter  ControlID ="DropDownList1"  Name ="CompanyName"  
PropertyName
="SelectedValue"
                    Type
="String"   ConvertEmptyStringToNull="False"   />
            
</ SelectParameters >  

6、OK了!

下面提供代码:

一、Default.aspx的代码:

<% @ 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:TextBox  ID ="TextBox1"  runat ="server" ></ asp:TextBox >
        
&nbsp;   &nbsp;   &nbsp;   &nbsp;   &nbsp;   &nbsp;   &nbsp;
        
< asp:Button  ID ="Button1"  runat ="server"  OnClick ="Button1_Click"  Text ="search"  Width ="88px"   />< br  />
        
< asp:DropDownList  ID ="DropDownList1"  runat ="server" >
        
< asp:ListItem  Selected =True ></ asp:ListItem >
        
< asp:ListItem   > a </ asp:ListItem >
        
< asp:ListItem  > aa </ asp:ListItem >
        
< asp:ListItem  > ab </ asp:ListItem >
        
        
        
</ asp:DropDownList >
        
&nbsp;   &nbsp;   &nbsp;   &nbsp;   &nbsp;   &nbsp;   &nbsp;   &nbsp;   &nbsp;   &nbsp;   &nbsp;   &nbsp;
        
&nbsp;   &nbsp;   &nbsp;   &nbsp;   &nbsp;   &nbsp;   &nbsp;&nbsp;   &nbsp;   &nbsp;   &nbsp;   &nbsp;
        
&nbsp;   &nbsp;   &nbsp;   &nbsp;   &nbsp;
        
< br  />
        
< br  />
        
< asp:GridView  ID ="GridView1"  runat ="server"  AllowPaging ="True"  AllowSorting ="True"
            AutoGenerateColumns
="False"  DataKeyNames ="CustomerID"  DataSourceID ="SqlDataSource1"  OnSelectedIndexChanged ="GridView1_SelectedIndexChanged"  AutoGenerateEditButton ="True" >
            
< Columns >
                
< asp:BoundField  DataField ="CustomerID"  HeaderText ="CustomerID"  ReadOnly ="True"  SortExpression ="CustomerID"   />
                
< asp:BoundField  DataField ="CompanyName"  HeaderText ="CompanyName"  SortExpression ="CompanyName"   />
                
< asp:BoundField  DataField ="ContactName"  HeaderText ="ContactName"  SortExpression ="ContactName"   />
                
< asp:BoundField  DataField ="Country"  HeaderText ="Country"  SortExpression ="Country"   />
                
< asp:TemplateField  HeaderText ="City" >
                
                    
< ItemTemplate >
                        
< asp:Label  ID ="Label1"  runat ="server"  Text ='<%#  Bind("City") % > ' Width="184px"> </ asp:Label >
                    
</ ItemTemplate >                     
                    
< EditItemTemplate >
                        
< asp:DropDownList  ID ="DropDownList2"  runat ="server"  SelectedValue ='<%#  Bind ("City") % > ' DataSourceID="SqlDataSource1" DataTextField="City" DataValueField="City">
                        
</ asp:DropDownList >
                    
</ EditItemTemplate >

                
</ asp:TemplateField >
            
</ Columns >
        
</ asp:GridView >
        
< asp:SqlDataSource  ID ="SqlDataSource1"  runat ="server"  ConnectionString ="<%$ ConnectionStrings:NorthwindConnectionString %>"
            SelectCommand
="SELECT [CustomerID], [CompanyName], [ContactName], [Country], [City] FROM [Customers]"  
            DeleteCommand
="DELETE FROM [Customers] WHERE [CustomerID] = @CustomerID"  
            InsertCommand
="INSERT INTO [Customers] ([CustomerID], [CompanyName], [ContactName], [Country], [City]) VALUES (@CustomerID, @CompanyName, @ContactName, @Country, @City)"  
            UpdateCommand
="UPDATE [Customers] SET [CompanyName] = @CompanyName, [ContactName] = @ContactName, [Country] = @Country, [City] = @City WHERE [CustomerID] = @CustomerID" >
            
< DeleteParameters >
                
< asp:Parameter  Name ="CustomerID"  Type ="String"   />
            
</ DeleteParameters >
            
< UpdateParameters >
                
< asp:Parameter  Name ="CompanyName"  Type ="String"   />
                
< asp:Parameter  Name ="ContactName"  Type ="String"   />
                
< asp:Parameter  Name ="Country"  Type ="String"   />
                
< asp:Parameter  Name ="City"  Type ="String"   />
                
< asp:Parameter  Name ="CustomerID"  Type ="String"   />
            
</ UpdateParameters >
            
< InsertParameters >
                
< asp:Parameter  Name ="CustomerID"  Type ="String"   />
                
< asp:Parameter  Name ="CompanyName"  Type ="String"   />
                
< asp:Parameter  Name ="ContactName"  Type ="String"   />
                
< asp:Parameter  Name ="Country"  Type ="String"   />
                
< asp:Parameter  Name ="City"  Type ="String"   />
            
</ InsertParameters >
            
< SelectParameters >
                
< asp:ControlParameter  ControlID ="TextBox1"  Name ="CustomerID"  PropertyName ="Text"
                    Type
="String"   ConvertEmptyStringToNull ="False" />
                
< asp:ControlParameter  ControlID ="DropDownList1"  Name ="CompanyName"  
                       PropertyName
="SelectedValue"
                    Type
="String"    ConvertEmptyStringToNull ="False"   />
            
</ SelectParameters >  
        
</ asp:SqlDataSource >
    
    
</ div >
    
</ form >
</ body >
</ html >

二、Default.aspx.cs代码:

     protected   void  Button1_Click( object  sender, EventArgs e)
    
{
        SqlDataSource1.SelectCommand 
= "SELECT [CustomerID], [CompanyName], [ContactName], [Country], [City] FROM [Customers] WHERE (([CustomerID] LIKE '%' + @CustomerID + '%') AND ([CompanyName] LIKE '%' + @CompanyName + '%'))";
    }

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值