【问题标题】:Transferring DropDownList values from one page to the next将 DropDownList 值从一页转移到下一页
【发布时间】:2013-01-31 19:51:04
【问题描述】:

我有 2 个页面,都有 3 个类似的 DropDownLists。第一个 DropDownList 需要在选择第二个之前填充,第二个在第三个之前填充(然后填充一个 gridview)。

通常,用户会在一个页面上查看数据,单击一个按钮,然后在下一页上查看数据(通常使用相同的 DropDownList 选择)。有没有办法让下拉列表预先填充他们的旧选择?

【问题讨论】:

    标签: c# asp.net visual-studio


    【解决方案1】:

    您可以使用 asp.net 为您提供的PreviousPage 参数将值从一个页面传递到另一个页面。一个小例子:

    您将提交按钮上的第二页设置为:

    PostBackUrl="SecondPage.aspx"
    

    在 SecondPage.aspx 上声明可以获取信息的位置

    <%@ PreviousPageType VirtualPath="~/FirstPage.aspx" %>
    

    然后你得到它们...

    if (Page.PreviousPage != null)
    {
        if(Page.PreviousPage.IsCrossPagePostBack == true)
        {
            // and get the controls of the previous page as
            var SomeVariable = PreviousPage.DropDownlListId.SelectedValue;
        }
    }
    

    一些参考。
    Cross-Page Posting in ASP.NET Web Pages
    ASP.NET how to access public properties?
    Cross-page postbacks and back again retaining data from source page
    Cross-page posting. Is it a good pratice to use PreviousPage in Asp.net?

    【讨论】:

    • 现在如何让我的 DropDownList 可以公开访问?错误消息是“DDL​​ 由于其保护级别而无法访问。”
    • @digitalfrenchfry 创建一个可以访问它们的公共变量,例如Public DropDownList getDDList { get{DropDownListID;}}
    • 我想我们已经到了那里,但还没有完全做到。这就是我最终得到的结果... public string getWeekOf { get { return ddl_selectDate.SelectedValue; } } if (Page.PreviousPage != null) { string WeekOf = PreviousPage.getWeekOf;列表 objList = 新列表(); objList = new mainSQL().getList(WeekOf); GridView.DataSource = objList; GridView.DataBind(); }
    • @digitalfrenchfry 我忘记退货了……没那么难,多试几次就会找到public DropDownList getDDList { get{ return DropDownListID;}}
    • 我问了一个新的(相关的)问题,因为我无法正确回答,如果你想试一试:)
    【解决方案2】:

    如果您有用户单击的按钮发布到第二页的表单,则第二页可以查找 DDL 的值并在页面加载时设置它们。

    【讨论】:

      【解决方案3】:

      有办法:

      1. 通过会话或查询字符串将选择传递到第二页
      2. 填充第一个下拉列表
      3. 选择合适的项目
      4. 对其余的下拉菜单重复 2-3

      如果您正在寻找一种“能做到”的神奇解决方案,我认为这是不可能的。

      附言您可能会重新考虑您的设计(如果这是一个选项)并在第一页上放置网格;当用户从最后一个下拉列表中选择一个值时绑定网格(确保在最后一个下拉列表中将 AutoPostback 设置为 True 以触发最终回发)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-12-15
        • 1970-01-01
        • 1970-01-01
        • 2010-12-06
        • 2013-05-16
        • 1970-01-01
        • 2018-10-29
        • 1970-01-01
        相关资源
        最近更新 更多