【问题标题】:ASP.NET/VB dropdownlist not properly populating gridviewASP.NET/VB 下拉列表未正确填充 gridview
【发布时间】:2013-05-23 19:12:58
【问题描述】:

我对 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

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

【问题讨论】:

  • 如果它解决了您的问题,您应该接受答案。

标签: asp.net vb.net


【解决方案1】:

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

【讨论】:

  • 我想我没有意识到我需要改变它,而且我还没有找到任何专门用于 VB 的东西。我发现的所有东西都在 C# 中,我都不熟悉。我想我假设当从 ddl 切换选择时它会自动更新。我在 Visual Studio 中运行的测试查询似乎可以工作。所以我想我应该看看添加提交按钮以及如何合并 AutoPostBack=True 声明?
  • 太棒了。 AutoPostBack="true" 是我想要发生的。谢谢你的指点。对于任何想知道同样事情的人,我只是为我想要填充网格视图的 ddl 输入 AutoPostBack="true"。对于 Visual Studio,您可以在所选下拉列表的行为下将 AutoPostBack 属性设置为 true。感谢您的帮助!
  • 您可以将DropDownListAutoPosBack 设置为True,或者使用提交按钮继续PostBack 并使用您的选择过滤的数据重新绑定网格。
【解决方案2】:

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

.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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-01
    • 1970-01-01
    相关资源
    最近更新 更多