简体   繁体   English

ASP.NET C#使用下拉菜单中的会话数据

[英]ASP.NET C# use session data from dropdown

OK so i am a bit lost and could do with some help. 好,所以我有点迷路,可以提供一些帮助。

I am creating a program that a user inputs data into a form on the default page (I have that working). 我正在创建一个程序,用户可以将数据输入到默认页面上的表单中(我可以正常工作)。

Then i use session variables to get the data input from a text box on the default page and place that data into a drop down menu on Page2 (I have that working). 然后,我使用会话变量从默认页面上的文本框中获取数据输入,并将该数据放入Page2上的下拉菜单中(我可以正常工作)。

What im trying to do now is use the data selected from the drop down on the page2 and output it on to a label. 我现在想做的是使用从page2下拉菜单中选择的数据并将其输出到标签上。 any help would be appreciated. 任何帮助,将不胜感激。

Page2 code bellow session that populates drop down 填充的Page2代码下面的会话

public partial class About : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {


            MyFruit = Session["Fruitname"] as List<string>;
            //Create new, if null
            if (MyFruit == null)
                MyFruit = new List<string>();
            DropDownList1.DataSource = MyFruit;
            DropDownList1.DataBind();


        }

You can use SelectedIndexChanged event of DropDownList to handle this. 您可以使用DropDownList SelectedIndexChanged事件来处理此问题。 your AutoPostBack property of DropDownBox should be set to True 您的DropDownBoxAutoPostBack属性应设置为True

sample code as below: 示例代码如下:

Design code: page.aspx 设计代码: page.aspx

<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
        <asp:ListItem>name1</asp:ListItem>
        <asp:ListItem>name2</asp:ListItem>
    </asp:DropDownList>
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

CodeBehind File: page.aspx.cs 代码隐藏文件: page.aspx.cs

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Label1.Text = DropDownList1.SelectedValue.ToString();
        }

Not sure if this is what you are looking for but what I am guessing is you want an event for your drop down list to get the info and place it into a session to pass onto the next page 不确定这是否是您要查找的内容,但我想您是否希望您的下拉列表中有一个事件来获取信息并将其放入会话中以传递到下一页

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    string item=DropDownList.SelectedItem;
    Session["selectedItem"]=item;
    Response.Redirect("TheNextPageURL")
}

public partial class TheNextPage : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(Session["selectedItem"]!=null)
        {
            Label1.Text=Session["selectedItem"].toString();
        }
    }
}

Hope that helps 希望能有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM