2017-04-21 45 views
0

我試圖解決這個問題時已經達到了一個靜止點。所以我的web應用程序有一個填充gridview的下拉列表。然後,在gridview下面的標籤中,它應該顯示數據庫中所選作者的所有行(此Web應用程序將使用分頁)。每當我選擇其他作者時,我的標籤都會拋出一些瘋狂的價值。我需要在代碼中調整什麼?標籤顯示gridview中顯示的數據源中的總行數

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(); 
    } 

} 
} 
+0

把代碼在Page_Load中,如果{} –

+0

我(的IsPostBack!)這樣做的結果是,每個作者的標籤現在都顯示爲零。我的代碼如下:'code'public部分類WebForm1的:(!的IsPostBack)System.Web.UI.Page { 保護無效的Page_Load(對象發件人,EventArgs的) { 如果 { INT rowCount時= GridView1 .Rows.Count; Label1.Text = rowCount.ToString(); }} } }'code' – iuliko

回答

1

我終於得到了這個糾正和正確運行。我可能在SQL DATA Source 1的一個方面進行了雙重編碼。我使用的是Visual Studio 2012,所以如果將來有人引用此帖,請記住檢查SQL DataSources的屬性中的事件處理程序,然後雙擊選中。

這是什麼導致了我的一堆問題,因爲雖然被選中的數據源是在我的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 
    } 

    } 
    } 
0

如果問題是打印使用SQL數據綁定到網格的記錄或行總數的計數來源,比你可以嘗試修改你的代碼有點像下面。

對於SQL數據源,只要您完成從下拉列表中選擇一個值,就應該發生另一個事件,即...... SqlDataSource2_Selected()。 如果您正在使用sqldatasource,則可以使用在選擇操作完成後觸發的「Selected」事件來統計總行數。

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

也許您應該嘗試將代碼從page_load移動到上述事件。

+0

動了我的頁面加載代碼到SqlDataSource1_Selected和沒有任何顯示在標籤中。不太確定SqlDataSource2_Selected()應該是什麼? '代碼'{保護無效的Page_Load(對象發件人,EventArgs的) { } 保護無效SqlDataSource1_Selected(對象發件人,SqlDataSourceStatusEventArgs E){ 如果(e.AffectedRows> 0){ INT rowCount時= e.AffectedRows; Label1.Text = rowCount.ToString(); }} }}'code' – iuliko