按钮和下拉列表在 ASP.NET C# 中使用会话/应用程序变量



我将如何实现此方案?我在默认页面上有两个按钮,按钮 1 和按钮 2。如果单击 Button1,则第二页上的下拉列表的内容将为:a、b 和 c。但是,如果从"默认"页面单击 Button2,则第二页上的 DDL 内容将为:d 和 e。谢谢!

如果您

使用的是 ASP.NET WebForms,则可以在第一页中填充会话变量,并在单击任一按钮时确定内容。然后,我将下拉列表的数据源设置为会话变量。像这样:

页 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。

最新更新