【问题标题】:How to set checkbox always checked when user redirect to another page?当用户重定向到另一个页面时,如何设置复选框始终选中?
【发布时间】:2015-10-06 12:15:09
【问题描述】:

即使用户重定向到另一个页面,我如何实现我的复选框以保持检查状态?例如,在我的代码中,当我选中复选框时,系统会更新数据库并显示 "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 所在的当前页面。

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

这与回发有关吗?请帮忙。

更新

我已经尝试过了,它会一直检查我是否重定向到此页面,但是当单击复选框时,状态不会从“已验证”更改为“尚未验证”,单击后回发不会更改。

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

【问题讨论】:

  • Status 标题的列值为 validatedNot yet..。在这里您正在更改数据库中的位,因此在绑定时您可以根据状态位将选中的设置为 true 或 false false
  • 请也分享 Gridview 标记,这样会更有帮助。
  • 我的 gridview 是自动生成的列,先生,所以我获取所有数据并将其绑定到数据表中。先生,您能否提供一个示例代码来说明它是如何完成的?
  • 没有用吗?还有问题吗?

标签: asp.net redirect checkbox code-behind


【解决方案1】:

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

在 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 ;
 }

【讨论】:

  • 代码提示我错误先生,System.Web.dll 中发生“System.ArgumentOutOfRangeException”类型的异常,但未在用户代码中处理附加信息:指定的参数超出范围的有效值。抱歉,我对此很陌生,我在哪里分配状态的位置?
  • 是的,我会在评论中提到,在Cells[] 中您需要添加名为Status 的列的索引。因为我不知道这个职位,所以你必须自己做,例如 row.Cells.Count - 1row.Cells.Count -2 同样。
  • row.Cells[&lt;Add Position Here&gt;].Text.Equals("Validated")。请参阅我的更新答案。
  • 先生,这是我的row.Cells[row.Cells.Count - 8].Text.Equals("Validated") 代码,因为我的状态在第 8 行,对吗?
  • 第 8 行或第 8 列?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多