【问题标题】:Trying to keep contents of checkbox List when returned to page返回页面时尝试保留复选框列表的内容
【发布时间】:2013-12-05 18:47:30
【问题描述】:

我在第 1 页上有一个复选框列表和一个按钮,在第 2 页上有一个标签和一个按钮。我要做的是记住从第 2 页返回到第 1 页时检查了哪些复选框。有什么想法吗?我迷路了,这是我的代码。我尝试制作一个集合,但没有奏效?还是应该使用数组??

Page1.aspx

 namespace Form
 {
 public partial class Page2 : System.Web.UI.Page
 {
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        string daysrequested = "";
        int count = 0;

        foreach (ListItem daysItem in Checkboxlist1.Items)
        {
            if (daysItem.Selected)
            {

                daysrequested += "  <br /> " + daysItem.Value;
                count++;

            }
        }

        Session["daysre"] = daysrequested;
        Response.Redirect("Page2.aspx");
    }

Page2.aspx

  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.UI;
  using System.Web.UI.WebControls;

  namespace Form
 {
  public partial class Page21 : System.Web.UI.Page
 {
    protected void Page_Load(object sender, EventArgs e)
    {
        string days = (string)Session["daysre"];
        daysLabel.Text = String.Format("You picked" + days);
    }

    protected void btn_Click(object sender, EventArgs e)
    {
        Response.Redirect ( "Page1.aspx");
    }
    }
    }

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    不要使用string 作为数据类型来保存复选框列表项的值,而是使用List&lt;string&gt;,因为ListItemValue 属性是string,如下所示:

    protected void Button1_Click(object sender, EventArgs e)
    {
        List<string> daysrequested = new List<string>();
    
        foreach (ListItem daysItem in Checkboxlist1.Items)
        {
            if (daysItem.Selected)
            {
                daysrequested.Add(daysItem.Value);
            }
        }
    
        Session["daysre"] = daysrequested;
        Response.Redirect("Page2.aspx");
    }
    

    现在在第 2 页的Page_Load 中,您可以将列表从Session 中拉出,如下所示:

    protected void Page_Load(object sender, EventArgs e)
    {
        List<string> requestedDays = new List<string>();
        requestedDays = Session["daysre"] as List<string>;
    
        StringBuilder theRequestedDaysStringBuilder = new StringBuilder();
    
        foreach(string day in requestedDays)
        {
            theRequestedDaysStringBuilder.Append(day);
        }
    
        daysLabel.Text = String.Format("You picked" + theRequestedDaysStringBuilder.ToString());
    }
    

    最后,要重新选中第 1 页中的复选框,请在 Page_Load 中执行此操作:

    protected void Page_Load(object sender, EventArgs e)
    {
        // Check to see if daysre is in session cache
        if(Session["daysre"] != null)
        {
            // Get list from session cache
            List<string> requestedDays = new List<string>();
            requestedDays = Session["daysre"] as List<string>;
    
            // Loop through each value in list
            foreach(string day in requestedDays)
            {
                theRequestedDaysStringBuilder.Append(day);
    
                // Loop through each item in check box list
                foreach (ListItem daysItem in Checkboxlist1.Items)
                {
                    // Check to see if this item needs to be checked by 
                    // comparing its value with current value in list
                    if(daysItem.Value == day)
                    {
                        // We have a match
                        // Make item checked and break out of loop
                        daysItem.Checked = true;
                        break;
                    }
                }
            }
        }
    
    
        StringBuilder theRequestedDaysStringBuilder = new StringBuilder();
    
        foreach(string day in requestedDays)
        {
            theRequestedDaysStringBuilder.Append(day);
        }
    
        daysLabel.Text = String.Format("You picked" + theRequestedDaysStringBuilder.ToString());
    }
    

    【讨论】:

    • 好的,这可行,但是当我返回页面加载中的第 1 页时,如何重新检查复选框
    • @FrancisGall - 当然没问题,也可以随意对答案进行投票。 :-)
    【解决方案2】:

    在第 1 页:

    On Button Click 将检查列表的检查值存储在会话中 并再分配一个 session("Page2Visited")="1"

    在page_load函数中

    如果 session("Page2Visited") = "1" 那么

    将所有存储的会话值分配为检查列表

    其他

    将清单中的所有值保留为未选中

    如果结束

    清除所有会话

    【讨论】:

      【解决方案3】:

      首先,将所选项目保存在字符串变量中并稍后存储在 Session 对象中并不是一个好主意。您可以将它们添加到数组或通用列表中。稍后,您可以格式化显示字符串(在 Page2 页面的 page_load 方法中)读取您的数组项。

      如果您返回第 1 页,您的项目将不会被选中,因为您没有处理它。您将需要阅读您之前设置的 Session 对象并选择所选项目(在 Page1 Page_Load 方法内)。

      【讨论】:

        【解决方案4】:

        我刚刚做了一个重现错误的解决方案,复制粘贴了你的整个代码但没有得到任何错误,实际上它完全按照你的意愿工作

        所以也许检查一次页码 因为你提供的代码说

        public partial class Page21 : System.Web.UI.Page
        

        在您的第 2 页上

        【讨论】:

        • 另外请检查您的 (ListItem daysItem in Checkboxlist1.Items) 是否正确
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-06-10
        • 1970-01-01
        • 2013-04-03
        • 2011-10-10
        • 1970-01-01
        • 1970-01-01
        • 2023-03-14
        相关资源
        最近更新 更多