从下拉列表中传递数据 [英] pass data from dropdownlist

查看:123
本文介绍了从下拉列表中传递数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



这是我的代码:



Page1.aspx

 < asp:DropDownList ID =ddlState =serverAutoPostBack =TrueDataSourceID =SqlDataSource1DataTextField =ST_CodeDataValueField =ST_Code/> 
< asp:SqlDataSource ID =SqlDataSource1runat =serverConnectionString =<%$ ConnectionStrings:dbConnection%>SelectCommand =SELECT [ST_Code] FROM [State]>
< / asp:SqlDataSource>城市:< asp:DropDownList ID =ddlCityrunat =serverDataSourceID =SqlDataSource2DataTextField =RS_CityDataValueField =RS_City/>
< asp:SqlDataSource ID =SqlDataSource2runat =serverConnectionString =<%$ ConnectionStrings:dbConnection%>SelectCommand =ListbyStateSPROCSelectCommandType =StoredProcedure>`
< SelectParameters>
< asp:ControlParameter ControlID =ddlStateName =StatePropertyName =SelectedValueType =String/>
< / SelectParameters>
< / asp:SqlDataSource>

< asp:Button ID =提交runat =serverText =Submit/>
& nbsp;

< / asp:Content>

Page1.aspx.vb

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


部分公共类LiveEventSearch
继承System.Web.UI.Page

受保护的子Page_Load( ByVal sender As Object,ByVal e As System.EventArgs)处理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

受保护的子ddlCity_SelectedIndexChanged(ByVal sender As System.Object,ByVal e As System.EventArgs)处理ddlCity.SelectedIndexChanged

结束Sub


受保护的Sub Submit_Click(ByVal sender As System.Object,ByVal e As System.EventArgs)处理Submit.Click

End Sub
结束Class

Page2.aspx

 < asp:GridView ID =GridView1runat =serverAllowSorting =TrueAutoGenerateColumns =False
DataSourceID =SqlDataSource1Style =z-index:左:324px;位置:绝对
顶部:226px>
< Columns>
< asp:BoundField DataField =CodeHeaderText =CodeReadOnly =TrueSortExpression =R_Code/>
< asp:BoundField DataField =NameHeaderText =NameReadOnly =TrueSortExpression =R_Name/>
< asp:BoundField DataField =NumberHeaderText =Number ReadOnly =TrueSortExpression =RS_Number/>
< asp:BoundField DataField =Addr_1HeaderText =Addr_1ReadOnly =TrueSortExpression =RS_Addr_1/>
< asp:BoundField DataField =CityHeaderText =CityReadOnly =TrueSortExpression =RS_City/>
< asp:BoundField DataField =StateHeaderText =StateReadOnly = TrueSortExpression =RS_State/>
< asp:BoundField DataField =RS_ZipHeaderText =RS_ZipReadOnly =TrueSortExpression =RS_Zip/>
< / Columns& ;
< / asp:GridView>
< asp:SqlDataSource ID =SqlD ataSource1runat =serverConnectionString =<%$ ConnectionStrings:dbConnection%>
SelectCommand =ListbyCityStSPROCSelectCommandType =StoredProcedure>
< SelectParameters>
< asp:QueryStringParameter Name =CityQueryStringField =RS_CityType =String/>
< asp:QueryStringParameter Name =StateQueryStringField =ST_CodeType =String/>
< / SelectParameters>
< / asp:SqlDataSource>

< / asp:Content>


解决方案

有效地,ASP.NET应用程序中的每个页面都像一个单一的应用程序本身,事情不会跨页面,所以你需要实现一个类保持你的SQLDataSource,所以可以在页面之间调用它,或者你需要实现一些方式来复制数据跨页面。 / p>

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

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


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" />
    &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>

解决方案

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");

这篇关于从下拉列表中传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆