0

我对 ASP.NET 比较陌生,但是我必须使用 VB 创建一个 .aspx 表单,当从 ddl 中选择客户时,它将使用 Access 数据库中的客户订单填充 gridview。

到目前为止,我的问题是,当我尝试选择除第一个客户之外的任何其他客户时,gridview 不会重新填充新信息。相反,它停留在第一个客户的信息上。

这是我到目前为止所拥有的:

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb" Inherits="Technical_Challenge.WebForm1" %>

<!DOCTYPE html>

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

        <asp:Label ID="Label1" runat="server" Text="Customers"></asp:Label>
        <br />
        <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1" DataTextField="CustomerName" DataValueField="CustomerID">
        </asp:DropDownList>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:TechChallengeConnectionString %>" ProviderName="<%$ ConnectionStrings:TechChallengeConnectionString.ProviderName %>" SelectCommand="SELECT [CustomerID], [CustomerName] FROM [Customers] ORDER BY [CustomerName]"></asp:SqlDataSource>
        <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:TechChallengeConnectionString %>" ProviderName="<%$ ConnectionStrings:TechChallengeConnectionString.ProviderName %>" SelectCommand="SELECT * FROM [Orders] WHERE ([CustomerID] = ?)">
            <SelectParameters>
                <asp:ControlParameter ControlID="DropDownList1" Name="CustomerID" PropertyName="SelectedValue" Type="Int32" />
            </SelectParameters>
        </asp:SqlDataSource>

        <br />
        <asp:Label ID="CustomerOrdersLabel" runat="server" Text="Customer Orders">        </asp:Label>
        <br />
        <asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False" CellPadding="4" DataKeyNames="OrderID" DataSourceID="SqlDataSource2" ForeColor="#333333" GridLines="None">
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
            <Columns>
                <asp:BoundField DataField="OrderID" HeaderText="OrderID" InsertVisible="False" ReadOnly="True" SortExpression="OrderID" />
                <asp:BoundField DataField="CustomerID" HeaderText="CustomerID" SortExpression="CustomerID" />
                <asp:BoundField DataField="OrderPrice" HeaderText="OrderPrice" SortExpression="OrderPrice" />
            </Columns>
            <EditRowStyle BackColor="#999999" />
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <SortedAscendingCellStyle BackColor="#E9E7E2" />
            <SortedAscendingHeaderStyle BackColor="#506C8C" />
            <SortedDescendingCellStyle BackColor="#FFFDF8" />
            <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
        </asp:GridView>

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

代码隐藏:

Public Class WebForm1
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Protected Sub SqlDataSource1_Selecting(sender As Object, e As SqlDataSourceSelectingEventArgs) Handles SqlDataSource1.Selecting

    End Sub
End Class

任何帮助将不胜感激。谢谢!

4

2 回答 2

0

我没有在您的下拉列表中看到 AutoPostBack=True 设置或将发送数据进行处理的提交按钮?还是您没有复制该代码?

于 2013-05-23T03:47:56.460 回答
0

您必须按照以下方式更改您的代码:-

.aspx 更改:-

<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1" DataTextField="CustomerName" DataValueField="CustomerID" AutoPostBack="True">
</asp:DropDownList>

代码隐藏:-

Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
     // write your query to select data from database 
    // bind with grid view
End Sub
于 2013-05-23T11:12:58.817 回答