当用户重定向到另一个页面时,如何设置复选框始终被选中?

How to set checkbox always checked when user redirect to another page?

即使用户重定向到另一个页面,我如何实现我的复选框以保持选中状态?例如,在我后面的代码中,当我选中复选框时,系统会更新数据库并显示 "Validated" ,但是当我按下 GoBackTeacher_Click 事件时,它将重定向到另一个页面。在另一个页面中有一个按钮功能,它将重定向到我的代码隐藏功能实现并选中复选框的当前页面。

Aspx 代码:

<div class="container" style="text-align: center; margin: 0 auto;">
    <br />
    <h1>Validation of Subjects</h1>
    <br />
    <asp:GridView runat="server" CssClass="table table-hover table-bordered" ID="ValidateSubject" Style="text-align: center"></asp:GridView>
</div>

<div style="float: right; padding-right: 75px;">
    <button type="button" runat="server" class="btn btn-success" onserverclick="GoBackTeacher_Click">Go Back</button>
</div>

隐藏代码:

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

namespace SoftwareAnalysisAndDesign.SAD
{
    public partial class ValidateSubjectTeacher : System.Web.UI.Page
    {
        CheckBox check = new CheckBox();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["ValidateSubject"] == null)
            {
                Response.Redirect("TeacherPage.aspx", true);
            }

            if (!IsPostBack)
            {
                ValidateSubject.DataSource = Session["ValidateSubject"];
                ValidateSubject.DataBind();
            }
            //Add a checkbox in the last row of GridView Progmatically
            foreach (GridViewRow row in ValidateSubject.Rows)
            {
                check = row.Cells[row.Cells.Count - 1].Controls[0] as CheckBox; //position Check column on last row in gridview
                check.Enabled = true;
                check.CheckedChanged += ValidateSubject_Click; //Bind the event on the button
                check.AutoPostBack = true; //Set the AutoPostBack property to true
            }
        }
        protected void ValidateSubject_Click(object sender, EventArgs e)
        {
            CheckBox chk = (CheckBox)sender;
            GridViewRow grvRow = (GridViewRow)chk.NamingContainer;//This will give row

            string validated = "Validated";
            string notyetvalidated = "Not yet validated";
            string studid = grvRow.Cells[0].Text;
            string coursenum = grvRow.Cells[1].Text;

            if (chk.Checked)
            {
                grvRow.Cells[10].Text = validated;
                //Open Connection
                using (SqlConnection conn = new SqlConnection("Data Source=Keith;Initial Catalog=SAD;Integrated Security=True"))
                {
                    //Open Connection to database
                    try
                    {
                        conn.Open();
                    }
                    catch (Exception E)
                    {
                        Console.WriteLine(E.ToString());
                    }

                    using (SqlCommand cmd = new SqlCommand("Update AssessmentForm set Status = @Validated where StudentID = @studentID and CourseNo = @Coursenumber" ,conn))
                    {
                        cmd.Parameters.AddWithValue("@Validated", validated);
                        cmd.Parameters.AddWithValue("@studentID", studid);
                        cmd.Parameters.AddWithValue("@Coursenumber", coursenum);
                        cmd.ExecuteNonQuery();
                    }

                    //Close Connection to database
                    try
                    {
                        conn.Close();
                    }
                    catch (Exception E)
                    {
                        Console.WriteLine(E.ToString());
                    }
                }
            }
            else
            {
                grvRow.Cells[10].Text = notyetvalidated;
                //Open Connection
                using (SqlConnection conn = new SqlConnection("Data Source=Keith;Initial Catalog=SAD;Integrated Security=True"))
                {
                    //Open Connection to database
                    try
                    {
                        conn.Open();
                    }
                    catch (Exception E)
                    {
                        Console.WriteLine(E.ToString());
                    }
                    //query database to update the Status
                    using (SqlCommand cmd = new SqlCommand("Update AssessmentForm set Status = @Validated where StudentID = @studentID and CourseNo = @Coursenumber", conn))
                    {
                        cmd.Parameters.AddWithValue("@Validated", notyetvalidated);
                        cmd.Parameters.AddWithValue("@studentID", studid);
                        cmd.Parameters.AddWithValue("@Coursenumber", coursenum);
                        cmd.ExecuteNonQuery();
                    }

                    //Close Connection to database
                    try
                    {
                        conn.Close();
                    }
                    catch (Exception E)
                    {
                        Console.WriteLine(E.ToString());
                    }
                }
            }
        }
        protected void GoBackTeacher_Click(object sender, EventArgs e)
        {
            Response.Redirect("TeacherPage.aspx");
        }
    }
}

为了进一步理解我的问题,这里有一张图片来进一步解释它。

这是我在不按返回按钮的情况下选中复选框的情况

我在这个地方按下了返回按钮,在另一个页面中有一个继续按钮可以重定向到我的 gridview 所在的当前页面。

复选框未选中,状态显示已验证。我如何实现复选框保持选中状态的代码?

跟postback有关系吗?请帮忙。

更新

我试过了,它会在我重定向到这个页面时保持检查状态,但是当复选框被点击时,状态不会从 "Validated" 变为 "Not yet validated" 它不会在回发时改变点击了。

        foreach (GridViewRow row in ValidateSubject.Rows)
        {
            bool isChecked = default(bool);
            if (row.Cells[row.Cells.Count - 2].Text.Equals("Validated")) // Here please assign the position of **Status** column.
            {
                isChecked = true;
                check = row.Cells[row.Cells.Count - 1].Controls[0] as CheckBox; //position Check column on last row in gridview
                check.Enabled = true;
                check.CheckedChanged += ValidateSubject_Click; //Bind the event on the button
                check.AutoPostBack = true; //Set the AutoPostBack property to true
                check.Checked = isChecked; //Set checkbox checked based on status ;
            }
            else if (row.Cells[row.Cells.Count - 2].Text.Equals("Not yet validated"))
            {
                isChecked = false;
                check = row.Cells[row.Cells.Count - 1].Controls[0] as CheckBox; //position Check column on last row in gridview
                check.Enabled = true;
                check.CheckedChanged += ValidateSubject_Click; //Bind the event on the button
                check.AutoPostBack = true; //Set the AutoPostBack property to true
                check.Checked = isChecked; //Set checkbox checked based on status ;
            }
        }

在此函数中,上述条件不会更改数据库,而是仅在单击复选框时刷新页面。

protected void ValidateSubject_Click(object sender, EventArgs e)
        {
            CheckBox chk = (CheckBox)sender;
            GridViewRow grvRow = (GridViewRow)chk.NamingContainer;//This will give row

            string validated = "Validated";
            string notyetvalidated = "Not yet validated";
            string studid = grvRow.Cells[0].Text;
            string coursenum = grvRow.Cells[1].Text;

            if (chk.Checked)
            {
                grvRow.Cells[10].Text = validated;
                //Open Connection
                using (SqlConnection conn = new SqlConnection("Data Source=Keith;Initial Catalog=SAD;Integrated Security=True"))
                {
                    //Open Connection to database
                    try
                    {
                        conn.Open();
                    }
                    catch (Exception E)
                    {
                        Console.WriteLine(E.ToString());
                    }

                    using (SqlCommand cmd = new SqlCommand("Update AssessmentForm set Status = @Validated where StudentID = @studentID and CourseNo = @Coursenumber" ,conn))
                    {
                        cmd.Parameters.AddWithValue("@Validated", validated);
                        cmd.Parameters.AddWithValue("@studentID", studid);
                        cmd.Parameters.AddWithValue("@Coursenumber", coursenum);
                        cmd.ExecuteNonQuery();
                    }

                    //Close Connection to database
                    try
                    {
                        conn.Close();
                    }
                    catch (Exception E)
                    {
                        Console.WriteLine(E.ToString());
                    }
                }
            }
            else
            {
                grvRow.Cells[10].Text = notyetvalidated;
                //Open Connection
                using (SqlConnection conn = new SqlConnection("Data Source=Keith;Initial Catalog=SAD;Integrated Security=True"))
                {
                    //Open Connection to database
                    try
                    {
                        conn.Open();
                    }
                    catch (Exception E)
                    {
                        Console.WriteLine(E.ToString());
                    }
                    //query database to update the Status
                    using (SqlCommand cmd = new SqlCommand("Update AssessmentForm set Status = @Validated where StudentID = @studentID and CourseNo = @Coursenumber", conn))
                    {
                        cmd.Parameters.AddWithValue("@Validated", notyetvalidated);
                        cmd.Parameters.AddWithValue("@studentID", studid);
                        cmd.Parameters.AddWithValue("@Coursenumber", coursenum);
                        cmd.ExecuteNonQuery();
                    }

                    //Close Connection to database
                    try
                    {
                        conn.Close();
                    }
                    catch (Exception E)
                    {
                        Console.WriteLine(E.ToString());
                    }
                }
            }
        }

您需要修改您在 gridview 行内迭代并分配处理程序和设置复选框的部分中的 Page_Load 方法 属性。

在 gridview 中,状态列我猜你没有将它保存为文本,所以我将它作为文本进行比较。请参阅以下内容并执行相同的操作。

foreach (GridViewRow row in ValidateSubject.Rows)
 {
     bool isChecked = default(bool);
     if (row.Cells[row.Cells.Count - 1].Text.Equals("Validated")) // Here please assign the position of **Status** column.
          isChecked = true;
     check = row.Cells[row.Cells.Count - 1].Controls[0] as CheckBox; //position Check column on last row in gridview
     check.Enabled = true;
     check.CheckedChanged += ValidateSubject_Click; //Bind the event on the button
     check.AutoPostBack = true; //Set the AutoPostBack property to true
     check.Checked = isChecked //Set checkbox checked based on status ;
 }