Retrieve selected checkbox value from previous page

I have an asp.net webform website that stores data in a session, and the entries entered on pg1 and pg2 are displayed on the third page.

On the first page, the first flags are preselected by default, but if the user selects / deselects the flag when the user is on the second page, the back button appears, but when it is pressed, I don’t know how to re-display the flags selected / not selected, since only the default value is checked.

I am new to using asp and storage of the session, so I can do something completely wrong, so please let me know if I am, but also help me to solve my situation, and not just vote for a message without explanation.

Anyway, my code is:

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> 

Code for

 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"); } 

I know this should be in my Page_Load , just not sure how to do this.

Below is what I have for the switches and test fields on another page

 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
source share
2 answers

Suppose you are on page 3 and you clicked the back button, which redirects you to page 2.

When loading page 2, you need to check if it is a new download (and not reverse), and if it contains values ​​in the session. If both values ​​are true, you need to get the value from the session and check the box.

So write the following code in 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
source

check the simple implementation below and improve it according to your requirement and can do it with one page

aspx code:

 <%@ 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; } } } 
0
source

All Articles