jackyrong

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
在asp.net 2.0中,gridview是和sqldatasource控件绑定的,那么如何得到sqldatasource返回的记录的行数呢?
比如sqldatasource控件中用select * from ....,如何返回其记录行数?在.net 2.0中,可以通过sqldatasource的
OnSelected事件实现,并且对select事件SqlDataSourceStatusEventArgs参数中的AffectedRows属性设置一下就可以了,具体核心代码如下:

.........(开头部分省略)

<script runat="Server">

    protected void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)

   {

        totalRows.Text = e.AffectedRows.ToString();

    }

        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="CustomerID"

            DataSourceID="SqlDataSource1" AllowPaging="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" />

            </Columns>

        </asp:GridView>

        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="Data Source=(local);Initial Catalog=Northwind;user id=sa;password=123456;"

            ProviderName="System.Data.SqlClient" SelectCommand="SELECT [CustomerID], [CompanyName], [ContactName] FROM [Customers]" OnSelected="SqlDataSource1_Selected">

        </asp:SqlDataSource>

        <asp:Label ID="Label1" runat="server" Text="Number of total rows:"></asp:Label>

        <asp:Label ID="totalRows" runat="server" Text="Label"></asp:Label>

 

posted on 2005-09-30 09:23  jackyrong的世界  阅读(4884)  评论(3编辑  收藏  举报