GridView + SqlDataSource + 分页+ 搜索

ASP.Net 2.0 中直接GridView加SqlDataSource 实现增删改的文章网上非常多了,这里就不重复了。

GridView + SqlDataSource 分页也很简单,直接点GridView的智能标示,然后启用分页。

加上搜索条件的话其实也不难,主要是要在GridView生成之前先设置好数据源。

否则就会导致点击其他分页又重新绑定了原始数据。

很简单,在这里我是利用GridView的PreRender事件,这个事件是在控件生成之前触发的,在这个事件里设置好SQL语句即可。

假设以Northwind为示例

页面源码

         < asp:GridView ID = " GridView1 "  runat = " server "  AutoGenerateColumns = " False "  
            CellPadding
= " 4 "  DataKeyNames = " ProductID "  DataSourceID = " SqlDataSource1 "  
            ForeColor
= " #333333 "  GridLines = " None "  AllowPaging = " True "  
            onprerender
= " GridView1_PreRender " >
            
< FooterStyle BackColor = " #507CD1 "  Font - Bold = " True "  ForeColor = " White "   />
            
< RowStyle BackColor = " #EFF3FB "   />
            
< Columns >
                
< asp:BoundField DataField = " ProductName "  HeaderText = " 产品名 "  
                    SortExpression
= " ProductName "   />
                
< asp:BoundField DataField = " SupplierID "  HeaderText = " SupplierID "  
                    SortExpression
= " SupplierID "   />
                
< asp:BoundField DataField = " CategoryID "  HeaderText = " CategoryID "  
                    SortExpression
= " CategoryID "   />
                
< asp:BoundField DataField = " UnitPrice "  HeaderText = " UnitPrice "  
                    SortExpression
= " UnitPrice "   />
                
< asp:BoundField DataField = " UnitsInStock "  HeaderText = " UnitsInStock "  
                    SortExpression
= " UnitsInStock "   />
                
< asp:BoundField DataField = " UnitsOnOrder "  HeaderText = " UnitsOnOrder "  
                    SortExpression
= " UnitsOnOrder "   />
                
< asp:BoundField DataField = " ReorderLevel "  HeaderText = " ReorderLevel "  
                    SortExpression
= " ReorderLevel "   />
                
< asp:CheckBoxField DataField = " Discontinued "  HeaderText = " Discontinued "  
                    SortExpression
= " Discontinued "   />
                
< asp:CommandField ShowDeleteButton = " True "  ShowEditButton = " True "   />
            
</ Columns >
            
< PagerStyle BackColor = " #2461BF "  ForeColor = " White "  HorizontalAlign = " Center "   />
            
< SelectedRowStyle BackColor = " #D1DDF1 "  Font - Bold = " True "  ForeColor = " #333333 "   />
            
< HeaderStyle BackColor = " #507CD1 "  Font - Bold = " True "  ForeColor = " White "   />
            
< EditRowStyle BackColor = " #2461BF "   />
            
< AlternatingRowStyle BackColor = " White "   />
        
</ asp:GridView >
        
< asp:SqlDataSource ID = " SqlDataSource1 "  runat = " server "  
            ConnectionString
= " <%$ ConnectionStrings:NorthwindConnectionString %> "  
            DeleteCommand
= " DELETE FROM [Products] WHERE [ProductID] = @ProductID "  
            InsertCommand
= " INSERT INTO [Products] ([ProductName], [SupplierID], [CategoryID], [QuantityPerUnit], [UnitPrice], [UnitsInStock], [UnitsOnOrder], [ReorderLevel], [Discontinued]) VALUES (@ProductName, @SupplierID, @CategoryID, @QuantityPerUnit, @UnitPrice, @UnitsInStock, @UnitsOnOrder, @ReorderLevel, @Discontinued) "  
            SelectCommand
= " select * from Products  "  
            UpdateCommand
= " UPDATE [Products] SET [ProductName] = @ProductName, [SupplierID] = @SupplierID, [CategoryID] = @CategoryID, [QuantityPerUnit] = @QuantityPerUnit, [UnitPrice] = @UnitPrice, [UnitsInStock] = @UnitsInStock, [UnitsOnOrder] = @UnitsOnOrder, [ReorderLevel] = @ReorderLevel, [Discontinued] = @Discontinued WHERE [ProductID] = @ProductID " >
            
< DeleteParameters >
                
< asp:Parameter Name = " ProductID "  Type = " Int32 "   />
            
</ DeleteParameters >
            
< UpdateParameters >
                
< asp:Parameter Name = " ProductName "  Type = " String "   />
                
< asp:Parameter Name = " SupplierID "  Type = " Int32 "   />
                
< asp:Parameter Name = " CategoryID "  Type = " Int32 "   />
                
< asp:Parameter Name = " QuantityPerUnit "  Type = " String "   />
                
< asp:Parameter Name = " UnitPrice "  Type = " Decimal "   />
                
< asp:Parameter Name = " UnitsInStock "  Type = " Int16 "   />
                
< asp:Parameter Name = " UnitsOnOrder "  Type = " Int16 "   />
                
< asp:Parameter Name = " ReorderLevel "  Type = " Int16 "   />
                
< asp:Parameter Name = " Discontinued "  Type = " Boolean "   />
                
< asp:Parameter Name = " ProductID "  Type = " Int32 "   />
            
</ UpdateParameters >
            
< InsertParameters >
                
< asp:Parameter Name = " ProductName "  Type = " String "   />
                
< asp:Parameter Name = " SupplierID "  Type = " Int32 "   />
                
< asp:Parameter Name = " CategoryID "  Type = " Int32 "   />
                
< asp:Parameter Name = " QuantityPerUnit "  Type = " String "   />
                
< asp:Parameter Name = " UnitPrice "  Type = " Decimal "   />
                
< asp:Parameter Name = " UnitsInStock "  Type = " Int16 "   />
                
< asp:Parameter Name = " UnitsOnOrder "  Type = " Int16 "   />
                
< asp:Parameter Name = " ReorderLevel "  Type = " Int16 "   />
                
< asp:Parameter Name = " Discontinued "  Type = " Boolean "   />
            
</ InsertParameters >
        
</ asp:SqlDataSource >
        
< asp:Button ID = " Button1 "  runat = " server "  onclick = " Button1_Click "  Text = " Button "   />
        
< asp:TextBox ID = " TextBox1 "  runat = " server " ></ asp:TextBox >

CS代码

 

         protected   void  Button1_Click( object  sender, EventArgs e)
        
{
            ViewState[
"search"= this.TextBox1.Text;
        }


        
protected   void  GridView1_PreRender( object  sender, EventArgs e)
        
{
            
this.SqlDataSource1.SelectCommand = "select * from Products where ProductName like '%" + ViewState["search"+ "%' ";
            
        }

.Net2.0 如果想实现更复杂的操作,还是建议强类型化。

如果是.Net 3.0/3.5 的话 使用Linq 会更加方便,更适合做小项目,不必要考虑太多性能因素,控制好性能就足够了。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值