2015-08-20 104 views
1

我有一个asp.net webform网站,其中存储数据在一个会话,并在第三页上显示的条目输入pg1 &第2页。检索选定的复选框值从上一页

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

我是新来使用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"); 
    } 
} 

回答

1

让我们假设你是第3页,并且单击后退按钮您重定向到2

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

所以,写了下面的代码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 
    } 
} 
+0

我仍然遇到同样的问题,当我返回页面时,默认复选框被重新选中,我选择的不是 – murday1983

+0

其实您的解决方案几乎可以工作。它不工作的原因是由于我的第一个复选框被默认选中。我必须找到另一种方式来做这个 – murday1983

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码:

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; 
     } 
    } 
} 
+0

不要以为这是相关的我的问题。我想要一个关于如何保留用户选中的复选框的解决方案,我不能在代码片段中的任何地方看到这一点 – murday1983