单击按钮时不会显示GridView

时间:2016-06-25 18:52:06

标签: asp.net vb.net gridview vbscript

我有一个用VB编写的ASP.NET页面,我试图只在单击按钮时显示gridView,并且它应该显示产品ID,产品名称,供应商名称和数量为客户购买的所有产品购买(来自下拉列表)。由于某些原因,我无法弄清楚,无论我做什么,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 个答案:

答案 0 :(得分:0)

设置DataSource的{​​{1}}或DataSourceID属性后,您需要调用DataBind()作为将网格绑定到数据的最后一步。