簡體   English   中英

ASP.NET C#使用下拉菜單中的會話數據

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

好,所以我有點迷路,可以提供一些幫助。

我正在創建一個程序,用戶可以將數據輸入到默認頁面上的表單中(我可以正常工作)。

然后,我使用會話變量從默認頁面上的文本框中獲取數據輸入,並將該數據放入Page2上的下拉菜單中(我可以正常工作)。

我現在想做的是使用從page2下拉菜單中選擇的數據並將其輸出到標簽上。 任何幫助,將不勝感激。

填充的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();


        }

您可以使用DropDownList SelectedIndexChanged事件來處理此問題。 您的DropDownBoxAutoPostBack屬性應設置為True

示例代碼如下:

設計代碼: 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>

代碼隱藏文件: page.aspx.cs

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

不確定這是否是您要查找的內容,但我想您是否希望您的下拉列表中有一個事件來獲取信息並將其放入會話中以傳遞到下一頁

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();
        }
    }
}

希望能有所幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM