Label显示gridview中显示的datasource的总行数

Label displays the total number of rows in datasource shown in gridview

我在尝试解决这个问题时已经到了停滞不前的地步。所以我的 web 应用程序有一个填充 gridview 的下拉列表。然后在 gridview 下方的标签中,它应该显示 selected 作者的数据库中的所有行(此 Web 应用程序将使用分页)。每当我 select 其他作者时,我的标签就会抛出一些疯狂的价值观。我需要在我的代码中调整什么?

CSS代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Homemade01.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Welcome to our Master/Detail Filtering with a DropDownList example</title>
<style type="text/css">
    .auto-style1 {
        font-size: xx-large;
    }
    .auto-style2 {
        color: #FF0000;
    }
</style>
</head>
<body>
<form id="form1" runat="server">
<div>

    <span class="auto-style1">Welcome to our Master/Detail Filtering with a DropDownList example:</span><br />
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" SelectCommand="SELECT firstName + '  ' + lastName AS FullName, lastName, authorID, firstName FROM Authors ORDER BY lastName, authorID"></asp:SqlDataSource>

    <br />
    Please select an author from the list:&nbsp;
    <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1" Height="16px" Width="248px" AutoPostBack="True" DataTextField="FullName" DataValueField="authorID">
    </asp:DropDownList>
    <br />
    <br />
    <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" SelectCommand="SELECT Authors.firstName, Authors.lastName, AuthorISBN.ISBN, Titles.title, Publishers.publisherName, Titles.price, Titles.editionNumber FROM (((Authors INNER JOIN AuthorISBN ON Authors.authorID = AuthorISBN.authorID) INNER JOIN Titles ON AuthorISBN.ISBN = Titles.ISBN) INNER JOIN Publishers ON Titles.publisherID = Publishers.publisherID) WHERE (Authors.authorID = ?)">
        <SelectParameters>
            <asp:ControlParameter ControlID="DropDownList1" Name="?" PropertyName="SelectedValue" />
        </SelectParameters>
    </asp:SqlDataSource>
    <br />
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource2" AllowPaging="True" AllowSorting="True" CellPadding="4" ForeColor="#333333" GridLines="None" >
        <AlternatingRowStyle BackColor="White" />
        <Columns>
            <asp:BoundField DataField="firstName" HeaderText="First Name" SortExpression="firstName" />
            <asp:BoundField DataField="lastName" HeaderText="Last Name" SortExpression="lastName" />
            <asp:BoundField DataField="ISBN" HeaderText="ISBN" SortExpression="ISBN" />
            <asp:BoundField DataField="title" HeaderText="Title" SortExpression="title" />
            <asp:BoundField DataField="publisherName" HeaderText="Publisher" SortExpression="publisherName" />
            <asp:BoundField DataField="price" HeaderText="Price" SortExpression="price" DataFormatString="{0:c}" >
            <ItemStyle HorizontalAlign="Right" />
            </asp:BoundField>
            <asp:BoundField DataField="editionNumber" HeaderText="Edition" SortExpression="editionNumber" >
            <ItemStyle HorizontalAlign="Right" />
            </asp:BoundField>
        </Columns>
        <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
        <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
        <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
        <SortedAscendingCellStyle BackColor="#FDF5AC" />
        <SortedAscendingHeaderStyle BackColor="#4D0000" />
        <SortedDescendingCellStyle BackColor="#FCF6C0" />
        <SortedDescendingHeaderStyle BackColor="#820000" />
    </asp:GridView>

    <br />
    <br />
    <span class="auto-style2">The number of books is:</span>
    <asp:Label ID="Label1" runat="server" style="color: #FF0000"></asp:Label>

</div>
</form>

C#代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

namespace Homemade01
{
public partial class WebForm1 : System.Web.UI.Page
{


    protected void Page_Load(object sender, EventArgs e)
    {
        int rowCount = GridView1.Rows.Count;
        Label1.Text = "Books found: " + rowCount.ToString();
    }

}
}

如果问题是使用 SQL 数据源打印绑定到网格的记录或行的总数,那么您可以尝试如下修改代码。

对于 SQL 数据源,一旦您完成 select 从下拉列表中输入一个值,就会发生另一个事件,即.. SqlDataSource2_Selected() . 如果您使用的是 sqldatasource,则可以使用在 select 操作完成后触发的 'Selected' 事件来计算总行数。

protected void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)
{
  int rowCount = e.AffectedRows;
} 

或许您应该尝试将您的代码从 page_load 移至上述事件。

我终于更正了这个问题并且 运行 正确了。我可能在 SQL DATA Source 1 的某个方面进行了双重编码。我使用的是 Visual Studio 2012,所以如果以后有人引用这个 post 记得检查属性中的事件处理程序对于您的 SQL 数据源,只需双击选定的。

这就是给我带来一堆问题的原因,因为尽管选择的数据源在我的 c# 代码中,但 CSS 代码中并未引用它。祝以后遇到此问题的其他人好运。这正确地返回了结果。无论您在哪个页面,该网页都应该显示在下拉列表中为所选作者找到的记录总数。

这是CSS:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" 
Inherits="Homemade01.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Welcome to our Master/Detail Filtering with a DropDownList example</title>
<style type="text/css">
    .auto-style1 {
        font-size: xx-large;
    }
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<span class="auto-style1">Welcome to our Master/Detail Filtering with a DropDownList example:</span><br />
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" SelectCommand="SELECT firstName + '  ' + lastName AS FullName, lastName, authorID, firstName FROM Authors ORDER BY lastName, authorID" OnSelected="SqlDataSource1_Selected"  ></asp:SqlDataSource>

    <br />
    Please select an author from the list:&nbsp;
    <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1" Height="16px" Width="248px" AutoPostBack="True" DataTextField="FullName" DataValueField="authorID">
    </asp:DropDownList>
    <br />
    <br />
    <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" SelectCommand="SELECT Authors.firstName, Authors.lastName, AuthorISBN.ISBN, Titles.title, Publishers.publisherName, Titles.price, Titles.editionNumber FROM (((Authors INNER JOIN AuthorISBN ON Authors.authorID = AuthorISBN.authorID) INNER JOIN Titles ON AuthorISBN.ISBN = Titles.ISBN) INNER JOIN Publishers ON Titles.publisherID = Publishers.publisherID) WHERE (Authors.authorID = ?)" OnSelected="SqlDataSource2_Selected">
        <SelectParameters>
            <asp:ControlParameter ControlID="DropDownList1" Name="?" PropertyName="SelectedValue" />
        </SelectParameters>
    </asp:SqlDataSource>
    <br />
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource2" AllowPaging="True" AllowSorting="True" CellPadding="4" ForeColor="#333333" GridLines="None"  >
        <AlternatingRowStyle BackColor="White" />
        <Columns>
            <asp:BoundField DataField="firstName" HeaderText="First Name" SortExpression="firstName" />
            <asp:BoundField DataField="lastName" HeaderText="Last Name" SortExpression="lastName" />
            <asp:BoundField DataField="ISBN" HeaderText="ISBN" SortExpression="ISBN" />
            <asp:BoundField DataField="title" HeaderText="Title" SortExpression="title" />
            <asp:BoundField DataField="publisherName" HeaderText="Publisher" SortExpression="publisherName" />
            <asp:BoundField DataField="price" HeaderText="Price" SortExpression="price" DataFormatString="{0:c}" >
            <ItemStyle HorizontalAlign="Right" />
            </asp:BoundField>
            <asp:BoundField DataField="editionNumber" HeaderText="Edition" SortExpression="editionNumber" >
            <ItemStyle HorizontalAlign="Right" />
            </asp:BoundField>
        </Columns>
        <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
        <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
        <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
        <SortedAscendingCellStyle BackColor="#FDF5AC" />
        <SortedAscendingHeaderStyle BackColor="#4D0000" />
        <SortedDescendingCellStyle BackColor="#FCF6C0" />
        <SortedDescendingHeaderStyle BackColor="#820000" />
    </asp:GridView>

    <br />
    <br />
    <asp:Label ID="Label1" runat="server" style="color: #FF0000"></asp:Label>

</div>
</form>

现在是 C# 代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

namespace Homemade01
{
public partial class WebForm1 : System.Web.UI.Page
{
    //int rowCount;

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)
    {
        //int rowCount = e.AffectedRows;
    }

    protected void SqlDataSource2_Selected(object sender, SqlDataSourceStatusEventArgs e)
    {
        int rowCount = e.AffectedRows; 
        //Above find the affected rows in SQL Data Source 2 and counts them then assigns them to an INT
        Label1.Text = "The number of Books found is: " + rowCount.ToString();
        //Label prints its text plus the rowCount variable which is converted to a string
    }

    }
   }