从上一页检索选定的复选框值

时间:2015-08-20 13:29:35

标签: c# asp.net webforms

我有一个asp.net webform网站,用于在会话中存储数据,第三页显示在pg1&上输入的条目。 PG2。

在第一页上,默认情况下会预先选中第一个复选框,但如果用户选择/取消选中复选框,当用户在第二页时,会有一个后退按钮但是当它被点击时我不知道如何重新显示选中/取消选中的复选框,因为只选中默认选项。

我刚开始使用asp和会话存储,所以我可能会做一些完全错误的事情,所以请告诉我,如果我是,也帮助我解决我的情况,而不是仅仅在没有解释的情况下投票

无论如何我的代码是:

HTML

<div class="form-group">
     <div class="col-xs-offset-0 col-sm-offset-4 col-sm-3">All services</div>
     <div class="col-sm-1">
          <asp:CheckBox ID="Step02AllServices" runat="server" Checked="True" />
     </div>
</div>
<div class="form-group">
     <div class="col-xs-offset-0 col-sm-offset-4 col-sm-3">Site content uploading only</div>
     <div class="col-sm-1">
          <asp:CheckBox ID="Step02ContentUploading" runat="server" />
     </div>
</div>
<div class="form-group">
     <div class="col-xs-offset-0 col-sm-offset-4 col-sm-3">Site content &amp; layout checking</div>
     <div class="col-sm-1">
          <asp:CheckBox ID="Step02ContentLayoutChecking" runat="server" Enabled="False" />
     </div>
</div>

代码背后

protected void Step02SubmitButton_Click(object sender, EventArgs e)
{
     Session["Step02AllServices"] = Step02AllServices.Checked;
     Session["Step02ContentUploading"] = Step02ContentUploading.Checked;
     Session["Step02ContentLayoutChecking"] = Step02ContentLayoutChecking.Checked;
     Response.Redirect("/Quotation/pg3.aspx");
}

我知道需要在我的Page_Load中,不知道该怎么做。

以下是我在另一页上的单选按钮和测试字段所拥有的内容

if (txtData2.Text == string.Empty && Session["pg2"] != null)
{
     txtData2.Text = Session["pg2"].ToString();

     if (Session["pg2Yes"].ToString() == "Yes")
     {
          pg2Yes.Checked = Session["pg2Yes"].Equals("Yes");
     }

     if (Session["pg2No"].ToString() == "No")
     {
          pg2No.Checked = Session["pg2No"].Equals("No");
     }
}

2 个答案:

答案 0 :(得分:1)

我们假设您在第3页,然后单击后退按钮,将您重定向到第2页。

在第2页的加载中,您需要检查它是否是新加载(不是回发)以及它是否包含会话值。如果两者都为真,则需要从会话中获取值并选中复选框。

因此,请在Page2Load中编写以下代码

//if you are loading the new page
if (!Page.IsPostBack)
{
    if(Session["Step02AllServices"] != null)
    {
        Step02AllServices.Checked = (bool) Session["Step02AllServices"];
        //similarly assign other checkbox values as they are in session already
    }
    else
    {
        //do normal assignment
    }
}

答案 1 :(得分:0)

检查以下简单实现并根据您的要求进行增强,可以使用单页

完成

aspx代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="WebApplication1.WebForm2" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div id="div1" runat="server">
            <asp:CheckBox ID="CheckBox1" runat="server" />
            <asp:CheckBox ID="CheckBox2" runat="server" /><asp:Button ID="Button1" runat="server" Text="Next" OnClick="Button1_Click" />
        </div>
        <div id="div2" runat="server" visible="false">
            <asp:Button ID="Button2" runat="server" Text="Back" OnClick="Button2_Click"/>
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
            <asp:Button ID="Button3" runat="server" Text="Next" OnClick="Button3_Click"/>
        </div>
         <div id="div3" runat="server" visible="false">
             <asp:Button ID="Button4" runat="server" Text="Back" OnClick="Button4_Click"/>
             <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
             <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
             <asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>
             <asp:Label ID="Label4" runat="server" Text="Label"></asp:Label>
        </div>
    </form>
</body>
</html>

cs code:

using System;

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

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            div1.Visible = false;
            div2.Visible = true;
            div3.Visible = false;
        }
        protected void Button2_Click(object sender, EventArgs e)
        {
            div1.Visible = true;
            div2.Visible = false;
            div3.Visible = false;
        }
        protected void Button3_Click(object sender, EventArgs e)
        {
            div1.Visible = false;
            div2.Visible = false;
            div3.Visible = true;
            Label1.Text = CheckBox1.Checked.ToString();
            Label2.Text = CheckBox2.Checked.ToString();
            Label3.Text = TextBox1.Text;
            Label4.Text = TextBox2.Text;
        }
        protected void Button4_Click(object sender, EventArgs e)
        {
            div1.Visible = false;
            div2.Visible = true;
            div3.Visible = false;
        }
    }
}