簡體   English   中英

在ASP.NET,C#中使用會話/應用程序變量的Button和DropDownList

[英]Button and DropDownList using Session/Application Variable in ASP.NET, C#

我將如何實施此方案? 我在“默認”頁面上有兩個按鈕,Button1和Button2。 如果單擊Button1,則第二頁上的DropDownList的內容將為:a,b和c。 但是,如果從“默認”頁面單擊Button2,則第二頁上DDL的內容將為:d和e。 謝謝!

如果使用的是ASP.NET WebForms,則可以在第一頁中填充一個Session變量,其內容由單擊任一按鈕時確定。 然后,我將DropDown列表的DataSource設置為Session變量。 像這樣:

第1頁:

protected void Button1_Click(object sender, EventArgs e)
    {
        Session["ListSource"] = new List<string>
        {
            "a",
            "b",
            "c"
        };
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        Session["ListSource"] = new List<string>
        {
            "d",
            "e"
        };
    }

第2頁:

        protected void Page_Load(object sender, EventArgs e)
    {
        DropDownList1.DataSource = (List<string>)Session["ListSource"];
        DropDownList1.DataBind();
    }

在MVC中,您可以讓Controller動作生成List並將其作為模型提供給第二頁。 但是,如果您指的是DropDownList,聽起來好像您正在使用WebForms。

暫無
暫無

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

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