2016-06-25 75 views
0

我有一個ASP.NET頁面,我在VB中編寫,我試圖只在按鈕被點擊時顯示gridView,並且它應該顯示產品ID,產品名稱,供應商名稱和客戶購買的所有產品的數量(來自下拉列表)。出於某種原因,我無法弄清楚,無論我做什麼gridView都不顯示。永遠。任何幫助將不勝感激。當點擊按鈕時,GridView不會顯示

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %> 

<!DOCTYPE html> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 

     <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" SelectCommand="SELECT [CompanyName] FROM [Customers] ORDER BY [CompanyName]"></asp:SqlDataSource> 
     <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" SelectCommand="SELECT Customers_1.CustomerID, Customers_1.CompanyName, (SELECT COUNT(Orders.OrderID) AS Expr1 FROM Orders INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID WHERE (Customers.CompanyName = @p1)) AS [Number of Orders], SUM([Order Details].UnitPrice * [Order Details].Quantity - [Order Details].Discount) AS [Total Cost of Order] FROM Customers AS Customers_1 INNER JOIN Orders AS Orders_1 ON Customers_1.CustomerID = Orders_1.CustomerID INNER JOIN [Order Details] ON Orders_1.OrderID = [Order Details].OrderID WHERE (Customers_1.CompanyName = @p1) GROUP BY Customers_1.CustomerID, Customers_1.CompanyName"> 
      <SelectParameters> 
       <asp:ControlParameter ControlID="DropDownList1" Name="p1" PropertyName="SelectedValue" /> 
      </SelectParameters> 
     </asp:SqlDataSource> 
     <asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" SelectCommand="SELECT Products.ProductID, Products.ProductName, Suppliers.CompanyName, [Order Details].Quantity FROM Orders INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID INNER JOIN [Order Details] ON Orders.OrderID = [Order Details].OrderID INNER JOIN Products ON [Order Details].ProductID = Products.ProductID INNER JOIN Suppliers ON Products.SupplierID = Suppliers.SupplierID WHERE (Customers.CustomerID = @p1)"> 
      <SelectParameters> 
       <asp:ControlParameter ControlID="DropDownList1" Name="p1" PropertyName="SelectedValue" /> 
      </SelectParameters> 
     </asp:SqlDataSource> 
     <br /> 

     <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource1" DataTextField="CompanyName" DataValueField="CompanyName"> 
      <asp:ListItem>Select A Customer</asp:ListItem> 
     </asp:DropDownList> 

     <br /> 
     <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" BackColor="White" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" CellPadding="4" DataKeyNames="CustomerID" DataSourceID="SqlDataSource2" Height="50px" Width="125px"> 
      <EditRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" /> 
      <Fields> 
       <asp:BoundField DataField="CustomerID" HeaderText="CustomerID" ReadOnly="True" SortExpression="CustomerID" /> 
       <asp:BoundField DataField="CompanyName" HeaderText="CompanyName" SortExpression="CompanyName" /> 
       <asp:BoundField DataField="Number of Orders" HeaderText="Number of Orders" ReadOnly="True" SortExpression="Number of Orders" /> 
       <asp:BoundField DataField="Total Cost of Order" HeaderText="Total Cost of Order" ReadOnly="True" SortExpression="Total Cost of Order" /> 
      </Fields> 
      <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" /> 
      <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" /> 
      <PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" /> 
      <RowStyle BackColor="White" ForeColor="#330099" /> 
     </asp:DetailsView> 
     <br /> 
     <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" CellPadding="4" DataKeyNames="ProductID" DataSourceID="SqlDataSource3" EmptyDataText="N/A" Visible="False"> 
      <Columns> 
       <asp:BoundField DataField="ProductID" HeaderText="ProductID" InsertVisible="False" ReadOnly="True" SortExpression="ProductID" /> 
       <asp:BoundField DataField="ProductName" HeaderText="ProductName" SortExpression="ProductName" /> 
       <asp:BoundField DataField="CompanyName" HeaderText="CompanyName" SortExpression="CompanyName" /> 
       <asp:BoundField DataField="Quantity" HeaderText="Quantity" SortExpression="Quantity" /> 
      </Columns> 
      <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" /> 
      <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" /> 
      <PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" /> 
      <RowStyle BackColor="White" ForeColor="#330099" /> 
      <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" /> 
      <SortedAscendingCellStyle BackColor="#FEFCEB" /> 
      <SortedAscendingHeaderStyle BackColor="#AF0101" /> 
      <SortedDescendingCellStyle BackColor="#F6F0C0" /> 
      <SortedDescendingHeaderStyle BackColor="#7E0000" /> 
     </asp:GridView> 
     <br /> 
     <asp:Button ID="Button1" runat="server" Text="Order Summary" /> 

    </div> 
    </form> 
</body> 
</html> 

我的代碼隱藏頁看起來是這樣的:

Partial Class _Default 
    Inherits System.Web.UI.Page 

    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 
     GridView1.Visible = True 
     GridView1.DataSourceID = "SqlDataSource3" 
     GridView1. 
    End Sub 
End Class 

任何幫助,將不勝感激

+1

最後一行應該是GridView1.DataBind() –

+0

謝謝! @DenisWessels – MicroBM

+0

我已發佈爲answer.Please標記爲答案,如果它幫助你 –

回答

0

設置的DataSourceDataSourceID財產後,GridView需要調用DataBind()爲將網格綁定到數據的最後一步。