jeudi 1 octobre 2015

Checkbox not validated when checked

I'm having a problem with my Status when checked, when the user clicks the checkbox the page loads it and will not change the status to "Validated" and updates the Status of it. The checkbox will load when it is checked, and updates the Status "to Validated".

Here the status text is set to "Not yet Validated" when checkbox is not checked.

Aspx Web Form:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TeacherPage.aspx.cs" Inherits="SoftwareAnalysisAndDesign.SAD.TeacherPage" %>

<!DOCTYPE html>

<html xmlns="http://ift.tt/lH0Osb">
<head runat="server">
    <title>Teacher Page</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/TeacherStyle.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="titlebackground">
                <div class="container">
                    <div class="logo">
                        <img id="img1" src="usjrlogo.png" />
                    </div>
                    <h1 id="title" style="float: left;">Online AppSess System</h1>
                </div>
            </div>
            <div>
                <nav class="navbar navbar-inverse">
                    <div class="container-fluid">
                        <div class="navbar-header">
                            <a class="navbar-brand" runat="server" href="TeacherPage.aspx"><span>
                                <asp:Label runat="server" ID="labelsession"></asp:Label></span></a>
                        </div>
                        <div>
                            <ul class="nav navbar-nav">
                                <li><a runat="server" onserverclick="TeacherAccountDetails_Click">Account Details</a></li>
                            </ul>
                            <ul class="nav navbar-nav pull-right">
                                <li><a class="pull-right" runat="server" onserverclick="Logout_Click">Logout</a></li>
                            </ul>
                        </div>
                    </div>
                </nav>
            </div>
        </div>

        <!-- Contains Teacher Information and List of Subjects -->
        <div class="container">
            <div id="teacherinfo" class="row">
                <div class="form form-inline">
                    <!-- Teacher ID Group -->
                    <div id="idteacher" class="form-group">
                        <label>&nbsp;Teacher ID:</label>
                        <asp:TextBox runat="server" ID="teachid" size="25" CssClass="form-control" ReadOnly="true"></asp:TextBox>
                    </div>
                    <!-- Position Group -->
                    <div id="position" class="form-group" style="float: right">
                        <label>Position:</label>
                        <asp:TextBox runat="server" ID="teacherposition" size="25" CssClass="form-control" ReadOnly="true"></asp:TextBox>
                    </div>

                </div>
                <br />
                <!-- Full Name and Group -->
                <div class="form form-inline">
                    <div class="form-group">
                        <label>&nbsp; Full Name:</label>
                        <asp:TextBox runat="server" ID="fullname" size="25" CssClass="form-control" ReadOnly="true"></asp:TextBox>
                    </div>

                    <div id="depart" class="form-group" style="float: right">
                        <label>Department:</label>
                        <asp:TextBox runat="server" ID="Department" size="25" CssClass="form-control" ReadOnly="true"></asp:TextBox>
                    </div>
                </div>
                <br />
            </div>
            <div style="display: inline-block; text-align: center;margin-left:450px;">
                <h4>List of Subjects</h4>
                <asp:DropDownList runat="server" ID="SubjectList"></asp:DropDownList>
                <button type="button" runat="server" onserverclick="TeacherSubjects_Click">Proceed</button>
                <br />
            </div>
            <div>
                <asp:GridView runat="server" CssClass="table table-hover table-bordered" ID="ValidateSubject"></asp:GridView>
            </div>
        </div>
    </form>

    <script src="jquery/jquery.min.js"></script>
    <script src="Bootstrap/js/bootstrap.min.js"></script>

    <!-- jQuery -->
    <script src="SidebarBootstrap/js/jquery.js"></script>
    <!-- Bootstrap for JavaScript -->
    <script src="SidebarBootstrap/js/bootstrap.min.js"></script>
    <!-- Menu Toggle Script -->
    <script>

        $("#menu-toggle").click(function (e) {
            e.preventDefault();
            $("#wrapper").toggleClass("toggled");
        });

    </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;
using System.Data.SqlClient;
using MSSQLConnector;

namespace SoftwareAnalysisAndDesign.SAD
{
    public partial class TeacherPage : System.Web.UI.Page
    {
        private MSConnector connector = new MSConnector();
        private DataSet SubjectlistData;
        private DataTable SubjectlistTable;
        string query = null;
        CheckBox check = new CheckBox();
        protected void Page_Load(object sender, EventArgs e)
        {
            //If session is null, go to login page
            if (Session["teacherlogin"] == null)
            {
                Response.Redirect("OnlineAppSyss.aspx", true);
            }
            //Display the Session Data into these tags from the OnlineAppSyss Page using Session
            labelsession.Text = "Welcome " + Session["teacherfirstname"].ToString();
            teachid.Text = "" + Session["teacherlogin"].ToString();
            fullname.Text = "" + Session["teacherfirstname"].ToString() + " " + Session["teacherlastname"].ToString();
            Department.Text = "" + Session["department"].ToString();
            teacherposition.Text = "" + Session["position"].ToString();

         if(!IsPostBack)
         {
            //Populate The Select Tag with Subjects
            SubjectList.DataSource = Session["TeacherSubjectList"];
            SubjectList.DataTextField = "CourseNo";
            SubjectList.DataValueField = "CourseNo";
            SubjectList.DataBind();
         }

        }
        protected void TeacherSubjects_Click(object sender, EventArgs e)
        {
            string getText = SubjectList.SelectedItem.Text;
            //Connection String
            connector.ConnectionString = "Data Source=keith;Initial Catalog=SAD;Integrated Security=True";

            query = "select StudentID,CourseNo,CourseDescription,Units,Day,StartTime,EndTime,Room,Instructor,Amount,Status from assessmentform where CourseNo = '" + getText + "'";

            SubjectlistData = connector.ExecuteQuery(query);
            SubjectlistTable = SubjectlistData.Tables[0];

            //Add a colum check row
            SubjectlistTable.Columns.Add("Check", Type.GetType("System.Boolean"));

            //Bind Data
            ValidateSubject.DataSource = SubjectlistTable;
            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 TeacherAccountDetails_Click(object sender, EventArgs e)
        {
            Response.Redirect("AccountDetailsTeacher.aspx");
        }
        protected void Logout_Click(object sender, EventArgs e)
        {
            Session.Clear();
            Session.RemoveAll();
            Session.Abandon();
            Response.Redirect("OnlineAppSyss.aspx");
        }
    }
}

I want my gridview "Status" when checkbox is check the status text will changed to "Validated"

Is it something to do with PostBack? I've tried this in my page_load and teachersubject_click

Page_Load Binding Data on Dropdownlist:

if (!IsPostBack)
{
   //Populate The Select Tag with Subjects
   SubjectList.DataSource = Session["TeacherSubjectList"];
   SubjectList.DataTextField = "CourseNo";
   SubjectList.DataValueField = "CourseNo";
   SubjectList.DataBind();
}

TeacherSubject_Click Binding Data on GridView:

   if (!IsPostBack)
    {
        //Bind Data
        ValidateSubject.DataSource = SubjectlistTable;
        ValidateSubject.DataBind();
    }

But When I Click Proceed button the gridview will not display.

This question is somewhat related to this POST and this ONE

But this time I didn't declare the subjects to pass it into session and redirects to another page.




Aucun commentaire:

Enregistrer un commentaire