选中复选框后,我无法更新数据库并单击“验证”按钮。注意:(我使用Session来逐页传递我的数据)
在页面后面的另一个代码中,我在gridview中添加了一个列复选框。
//Add a colum check row
SubjectlistTable.Columns.Add("Check", Type.GetType("System.Boolean"));
Session["ValidateSubject"] = SubjectlistTable;
Response.Redirect("ValidateSubjectTeacher.aspx");
在ValidateSubjectTeacherPage:
中这是该计划 ASPX:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ValidateSubjectTeacher.aspx.cs" Inherits="SoftwareAnalysisAndDesign.SAD.ValidateSubjectTeacher" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Student Assessment Form</title>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<link rel="stylesheet" href="Bootstrap/css/bootstrap-theme.css" />
<link rel="stylesheet" href="Bootstrap/css/bootstrap.min.css"/>
<link rel="stylesheet" type="text/css" href="Stylesheets/ValidateSubjectTeacherStyle.css"/>
<!--Side bar link-->
<link rel="stylesheet" href="SidebarBootstrap/css/bootstrap.min.css" />
<link rel="stylesheet" href="SidebarBootstrap/css/simple-sidebar.css" />
</head>
<body>
<form id="form1" runat="server">
<div class="headerwrapper">
<div id="headercontent" class="jumbotron">
<img id="img1" src="usjrlogo.png" />
<h1 id="title">Online AppSess System</h1>
</div>
</div>
<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-primary" onserverclick="ValidateSubject_Click">Validate</button>
<button type="button" runat="server" class="btn btn-success" onserverclick="GoBackTeacher_Click">Go Back</button>
</div>
</form>
<script src="jquery/jquery.min.js"></script>
<script src="Bootstrap/js/bootstrap.min.js"></script>
</body>
</html>
Aspx Code Behind:
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;
}
}
protected void GoBackTeacher_Click(object sender, EventArgs e)
{
Response.Redirect("TeacherPage.aspx");
}
}
}
代码正在运行但是当我点击提交按钮时,系统提示我输入错误,我无法更新我的数据库:
错误讯息:
发生了'System.InvalidCastException'类型的异常 SoftwareAnalysisAndDesign.dll但未在用户代码中处理 附加信息:无法转换类型的对象 输入'System.Web.UI.HtmlControls.HtmlButton' 'System.Web.UI.WebControls.CheckBox'。
我的复选框事件有问题:
CheckBox chk = (CheckBox)sender;
似乎无法同时处理复选框事件和按钮事件。 请帮助,这是我想解决的唯一问题,以便我的系统完全正常工作。
答案 0 :(得分:1)
当您单击按钮时,您正在处理的“事件”不是来自GridView中的CheckBox
。它来自您的<button>
元素。
您的应用程序的架构方式,似乎您不需要按钮 - 只要您单击复选框以选择或取消选择它,它就会对该单行数据执行操作。所以当按下按钮时你不清楚你想要发生什么。
如果您愿意,可以使用相同的方法为这两个事件提供服务,在它开始时执行以下操作:
protected void ValidateSubject_Click(object sender, EventArgs e) {
if ( sender.GetType().FullName == "System.Web.UI.WebControls.CheckBox" ) {
// Have what you currently have for checkboxes here
} else if ( sender.GetType().FullName == "System.Web.UI.HtmlControls.HtmlButton" ) {
// Do something else for the button, probably involving getting the GridView's rows and iterating through them
}
}
但我建议为单独的事件分别设置事件处理程序。