zoukankan      html  css  js  c++  java
  • GridView + SqlDataSource + 分页+ 搜索

     摘自:http://blog.csdn.net/ccp5780199/archive/2008/04/28/2337660.aspx

      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 会更加方便,更适合做小项目,不必要考虑太多性能因素,控制好性能就足够了。

    本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/ccp5780199/archive/2008/04/28/2337660.aspx

  • 相关阅读:
    abp 框架定时任务(一)
    jenkins构建maven项目:找不到本地依赖包的解决办法
    python异常处理及常用异常
    python中unittest常用断言
    python自动化中遇到问题--Assert断言失败了,但是在异常捕获了,生成测试报告却是pass的。
    python+unittest框架
    linux虚拟机内网突然不通了
    curl 命令-接口测试
    python中使用os.path.join()
    ActiveMQ消费重试机制
  • 原文地址:https://www.cnblogs.com/wzyexf/p/1530492.html
Copyright © 2011-2022 走看看