简体   繁体   English

在ASP.NET,C#中使用会话/应用程序变量的Button和DropDownList

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

How would I implement this scenario? 我将如何实施此方案? I have two buttons, Button1 and Button2, on the Default page. 我在“默认”页面上有两个按钮,Button1和Button2。 If Button1 is clicked, the contents of the DropDownList on the second page would be: a, b, and c. 如果单击Button1,则第二页上的DropDownList的内容将为:a,b和c。 However, if Button2 is clicked from the Default page, the contents of the DDL on the second page would be: d and e. 但是,如果从“默认”页面单击Button2,则第二页上DDL的内容将为:d和e。 Thank you! 谢谢!

If you are using ASP.NET WebForms, you could populate a Session variable within your first page, with the contents being determined when either button is clicked. 如果使用的是ASP.NET WebForms,则可以在第一页中填充一个Session变量,其内容由单击任一按钮时确定。 I would then set the DataSource for the DropDown list to the Session variable. 然后,我将DropDown列表的DataSource设置为Session变量。 Something like this: 像这样:

Page 1: 第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"
        };
    }

Page 2: 第2页:

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

In MVC, you could have a Controller action generate the List and provide it as the model to your second page. 在MVC中,您可以让Controller动作生成List并将其作为模型提供给第二页。 Given you are referring to a DropDownList, though, it sounds like you are using WebForms. 但是,如果您指的是DropDownList,听起来好像您正在使用WebForms。

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

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