【发布时间】: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 标题的列值为 validated 和 Not yet..。在这里您正在更改数据库中的位,因此在绑定时您可以根据状态位将选中的设置为 true 或 false false。
-
请也分享 Gridview 标记,这样会更有帮助。
-
我的 gridview 是自动生成的列,先生,所以我获取所有数据并将其绑定到数据表中。先生,您能否提供一个示例代码来说明它是如何完成的?
-
没有用吗?还有问题吗?
标签: asp.net redirect checkbox code-behind