从下拉列表传递数据

发布于 2024-11-04 01:39:10 字数 3888 浏览 0 评论 0原文

我一直在尝试将选定的数据从两个下拉列表(page1.aspx)传递到(page2.aspx),但我没有任何运气,因为选择时数据似乎没有被传递。请帮助,这看起来并不那么困难,但我无法让它工作。在 (page2.aspx) 上,下拉列表中的数据将传递到存储过程,所有结果将显示在网格视图中。

这是我的代码:

Page1.aspx

<asp:DropDownList ID="ddlState" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource1" DataTextField="ST_Code" DataValueField="ST_Code" />
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:dbConnection %>"SelectCommand="SELECT [ST_Code] FROM [State]">
</asp:SqlDataSource> City:  <asp:DropDownList ID="ddlCity" runat="server" DataSourceID="SqlDataSource2" DataTextField="RS_City" DataValueField="RS_City" />
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:dbConnection %>"SelectCommand="ListbyStateSPROC"SelectCommandType="StoredProcedure">`
        <SelectParameters>
            <asp:ControlParameter ControlID="ddlState" Name="State" PropertyName="SelectedValue"  Type="String" />       
        </SelectParameters>
</asp:SqlDataSource>

       <asp:Button ID="Submit" runat="server" Text="Submit" />
    &nbsp;

</asp:Content>

Page1.aspx.vb

Imports System.Data
Imports System.Data.Common
Imports System.Data.SqlClient
Imports System.Web.UI.WebControls.DataGrid
Imports System.Web.UI.WebControls.DropDownList


Partial Public Class LiveEventSearch
    Inherits System.Web.UI.Page

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

    End Sub

    Public Sub ddlState_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        SqlDataSource2.SelectParameters.Clear()
        SqlDataSource2.SelectParameters.Add(New Parameter("@State", DbType.String, ddlState.SelectedValue))
        ddlCity.DataBind()


    End Sub

    Protected Sub ddlCity_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ddlCity.SelectedIndexChanged

    End Sub


    Protected Sub Submit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Submit.Click

    End Sub
End Class

Page2.aspx

 <asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False"
        DataSourceID="SqlDataSource1" Style="z-index: 100; left: 324px; position: absolute;
        top: 226px">
        <Columns>
            <asp:BoundField DataField="Code" HeaderText="Code" ReadOnly="True" SortExpression="R_Code" />
            <asp:BoundField DataField="Name" HeaderText="Name" ReadOnly="True" SortExpression="R_Name" />
            <asp:BoundField DataField="Number" HeaderText="Number" ReadOnly="True" SortExpression="RS_Number" />
            <asp:BoundField DataField="Addr_1" HeaderText="Addr_1" ReadOnly="True" SortExpression="RS_Addr_1" />
            <asp:BoundField DataField="City" HeaderText="City" ReadOnly="True" SortExpression="RS_City" />
            <asp:BoundField DataField="State" HeaderText="State" ReadOnly="True" SortExpression="RS_State" />
            <asp:BoundField DataField="RS_Zip" HeaderText="RS_Zip" ReadOnly="True" SortExpression="RS_Zip" />
        </Columns>
    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:dbConnection %>"
        SelectCommand="ListbyCityStSPROC" SelectCommandType="StoredProcedure">
        <SelectParameters>
            <asp:QueryStringParameter Name="City" QueryStringField="RS_City" Type="String" />
            <asp:QueryStringParameter Name="State" QueryStringField="ST_Code" Type="String" />
        </SelectParameters>
    </asp:SqlDataSource>

</asp:Content>

I have been trying to pass selected data from two dropdownlist (page1.aspx) to (page2.aspx) I am not having any luck as it seems the data is not being passed when selected. Please help, this doesn't seem that difficult, but I can not get it work. On (page2.aspx) the data from the dropdownlists will be passed to a stored procedure and all results will be in a gridview.

Here is my code:

Page1.aspx

<asp:DropDownList ID="ddlState" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource1" DataTextField="ST_Code" DataValueField="ST_Code" />
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:dbConnection %>"SelectCommand="SELECT [ST_Code] FROM [State]">
</asp:SqlDataSource> City:  <asp:DropDownList ID="ddlCity" runat="server" DataSourceID="SqlDataSource2" DataTextField="RS_City" DataValueField="RS_City" />
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:dbConnection %>"SelectCommand="ListbyStateSPROC"SelectCommandType="StoredProcedure">`
        <SelectParameters>
            <asp:ControlParameter ControlID="ddlState" Name="State" PropertyName="SelectedValue"  Type="String" />       
        </SelectParameters>
</asp:SqlDataSource>

       <asp:Button ID="Submit" runat="server" Text="Submit" />
     

</asp:Content>

Page1.aspx.vb

Imports System.Data
Imports System.Data.Common
Imports System.Data.SqlClient
Imports System.Web.UI.WebControls.DataGrid
Imports System.Web.UI.WebControls.DropDownList


Partial Public Class LiveEventSearch
    Inherits System.Web.UI.Page

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

    End Sub

    Public Sub ddlState_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        SqlDataSource2.SelectParameters.Clear()
        SqlDataSource2.SelectParameters.Add(New Parameter("@State", DbType.String, ddlState.SelectedValue))
        ddlCity.DataBind()


    End Sub

    Protected Sub ddlCity_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ddlCity.SelectedIndexChanged

    End Sub


    Protected Sub Submit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Submit.Click

    End Sub
End Class

Page2.aspx

 <asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False"
        DataSourceID="SqlDataSource1" Style="z-index: 100; left: 324px; position: absolute;
        top: 226px">
        <Columns>
            <asp:BoundField DataField="Code" HeaderText="Code" ReadOnly="True" SortExpression="R_Code" />
            <asp:BoundField DataField="Name" HeaderText="Name" ReadOnly="True" SortExpression="R_Name" />
            <asp:BoundField DataField="Number" HeaderText="Number" ReadOnly="True" SortExpression="RS_Number" />
            <asp:BoundField DataField="Addr_1" HeaderText="Addr_1" ReadOnly="True" SortExpression="RS_Addr_1" />
            <asp:BoundField DataField="City" HeaderText="City" ReadOnly="True" SortExpression="RS_City" />
            <asp:BoundField DataField="State" HeaderText="State" ReadOnly="True" SortExpression="RS_State" />
            <asp:BoundField DataField="RS_Zip" HeaderText="RS_Zip" ReadOnly="True" SortExpression="RS_Zip" />
        </Columns>
    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:dbConnection %>"
        SelectCommand="ListbyCityStSPROC" SelectCommandType="StoredProcedure">
        <SelectParameters>
            <asp:QueryStringParameter Name="City" QueryStringField="RS_City" Type="String" />
            <asp:QueryStringParameter Name="State" QueryStringField="ST_Code" Type="String" />
        </SelectParameters>
    </asp:SqlDataSource>

</asp:Content>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苄①跕圉湢 2024-11-11 01:39:10

实际上,ASP.NET 应用程序中的每个页面本身就像一个应用程序,事物不会跨页面持续,因此您要么需要实现一个类来保存 SQLDataSource,以便可以跨页面调用它,要么需要实现跨页面复制数据的某种方法。

您还可以使用 FindControl 函数跨页查找 SQLDataSource:

SqlDataSource sql = (SqlDataSource)Page.Master.Findcontrol("SqlDataSource1");

Effectively each page in an ASP.NET application is like a single app in itself, things don't last across pages, so you either need to implement a class that keeps hold of your SQLDataSource so can call it across pages or you need to implement some way to copy the data across the pages.

You could also use the FindControl function to find the SQLDataSource across pages:

SqlDataSource sql = (SqlDataSource)Page.Master.Findcontrol("SqlDataSource1");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文