【问题标题】:Button and DropDownList using Session/Application Variable in ASP.NET, C#在 ASP.NET、C# 中使用会话/应用程序变量的按钮和 DropDownList
【发布时间】:2018-09-28 03:21:01
【问题描述】:

我将如何实现这个场景?我在默认页面上有两个按钮,Button1 和 Button2。如果单击 Button1,则第二页上 DropDownList 的内容将是:a、b 和 c。但是,如果从 Default 页面单击 Button2,则第二页上的 DDL 的内容将是:d 和 e。谢谢!

【问题讨论】:

    标签: c# asp.net session-variables application-variables


    【解决方案1】:

    如果您使用的是 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 中,您可以让控制器操作生成列表并将其作为模型提供给您的第二个页面。不过,鉴于您指的是 DropDownList,听起来您正在使用 WebForms。

    【讨论】:

    • 非常感谢!这真的帮助我理解!是的,我正在使用 WebForms。
    猜你喜欢
    • 1970-01-01
    • 2011-06-04
    • 2012-05-04
    • 1970-01-01
    • 2012-08-17
    • 2012-08-03
    • 1970-01-01
    • 2013-06-19
    • 2014-02-01
    相关资源
    最近更新 更多