在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>

 

相关文章:

  • 2022-01-21
  • 2022-12-23
  • 2021-11-24
  • 2022-12-23
  • 2022-12-23
  • 2022-02-10
  • 2021-08-24
  • 2022-01-01
猜你喜欢
  • 2022-02-18
  • 2021-08-18
  • 2021-07-09
  • 2021-05-30
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案