CheckBox未将检查值发送到数据库C#Asp

时间:2015-07-30 08:39:14

标签: c# sql asp.net checkbox

我正在制作一个考勤系统,在该系统中,我从Gridview中的学生表中获取学生记录,并带有复选框。勾选在场的学生的复选框,并在未选中时取消选中。 之后我将记录提交给考勤系统。 问题:如果我检查或未选中,它将在提交出勤后显示数据库结果中的缺席请检查我的代码。

HTML

 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="RecordTablesss.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:Label ID="LabelSr" runat="server" Text=<%#Eval("Sr_Number") %>></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:Label ID="LabelName" runat="server" Text=<%#Eval("Name") %>></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:Label ID="LabelFN" runat="server" Text=<%#Eval("F_Name") %>></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:CheckBox ID="CheckAttendence" runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
        <asp:Button ID="ButtonSubmit" runat="server" Text="Submit" OnClick="SaveAttendence" style="width: 61px" />
    </div>
    </form>
</body>
</html>

代码背后:

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

namespace RecordTablesss
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            loadData();
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void loadData()
        {
            SqlConnection con = new SqlConnection("Data Source=HammadMaqbool;Initial Catalog=FYP_Demo;Integrated Security=True");
            con.Open();
            SqlDataAdapter da = new SqlDataAdapter("Select Sr_Number,Name,F_Name From Registerd_Student",con);
            DataSet ds = new DataSet();
            da.Fill(ds);
            GridView1.DataSource = ds.Tables[0];
            GridView1.DataBind();
        }

        protected void SaveAttendence(object sender, EventArgs e)
        {
            foreach (GridViewRow row in GridView1.Rows)
            {
                if (row.RowType == DataControlRowType.DataRow)
                {
                    string sta = "A";
                    CheckBox chkVar_ = row.FindControl("CheckAttendence") as CheckBox;
                    if (chkVar_.Checked)
                        sta = "P";

                    string StudentName = (row.FindControl("LabelName") as Label).Text;
                    string F_Name = (row.FindControl("LabelFN") as Label).Text;
                    int Number = int.Parse( (row.FindControl("LabelSr") as Label).Text);
                    //From Here to onword Database Operations. ..  
                    SqlConnection conn = new SqlConnection("Data Source=HammadMaqbool;Initial Catalog=FYP_Demo;Integrated Security=True");
                    conn.Open();
                    SqlCommand cmd = new SqlCommand("Insert into Attendance_B values('"+Convert.ToInt32(Number)+"','"+StudentName+"','"+F_Name+"','"+sta+"')",conn);
                    cmd.ExecuteNonQuery();
                }
            }
        }

        protected void ButtonSubmit_Click(object sender, EventArgs e)
        {

        }
    }
}

3 个答案:

答案 0 :(得分:3)

我认为你并不真正写下你的意思:

string sta = "";
CheckBox chkVar_ = row.FindControl("CheckAttendence") as CheckBox;
if (chkVar_.Checked)
    sta = "P";
sta = "A";

我瘦你的意思是:

string sta = "";
CheckBox chkVar_ = row.FindControl("CheckAttendence") as CheckBox;
if (chkVar_.Checked)
{
    sta = "P";
}
else
{
    sta = "A";
}
你看到了区别吗?

答案 1 :(得分:3)

正如你所说@Thomas Krojer回答也没有解决你的问题,但应该这样做。

您获得的结果chkVar_.Checked始终为false。因为在Page_Load上您再次绑定gridview,而不检查它是否为postback。 试试这个。

 protected void Page_Load(object sender, EventArgs e)
   {
          if (!Page.IsPostBack)
            {
                loadData();
            }
    }

它肯定会解决你的问题..

答案 2 :(得分:1)

你只需要更正代码

if (chkVar_.Checked)
{
    sta = "P";
}
else
{
    sta = "A";
}