Friday, 27 June 2014

Checkbox and Gridview code behind file

using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public partial class StudentMarks : System.Web.UI.Page
{
    public SqlConnection con = new SqlConnection("Data Source=THIYAGU-PC\\SQLEXPRESS;Initial Catalog=TEST;Integrated Security=True");
    public SqlCommand cmd;
    public DataSet ds;
    SqlDataAdapter da;

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

    }
    protected void txt_m4_TextChanged(object sender, EventArgs e)
    {
        int a = Convert.ToInt32(txt_m1.Text);
        int b = Convert.ToInt32(txt_m2.Text);
        int c = Convert.ToInt32(txt_m3.Text);
        int d = Convert.ToInt32(txt_m4.Text);
        int f = a + b + c +d ;
        txt_tot.Text = Convert.ToString(f);
    }
    protected void gvCheckboxes_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void gvCheckboxes_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow &&
   (e.Row.RowState == DataControlRowState.Normal ||
    e.Row.RowState == DataControlRowState.Alternate))
        {
            CheckBox chkBxSelect = (CheckBox)e.Row.Cells[1].FindControl("chkBxSelect");
            CheckBox chkBxHeader = (CheckBox)this.gvCheckboxes.HeaderRow.FindControl("chkBxHeader");
            chkBxSelect.Attributes["onclick"] = string.Format
                                                   (
                                                      "javascript:ChildClick(this,'{0}');",
                                                      chkBxHeader.ClientID
                                                   );
        }
    }
    protected void Btn_Submit_Click(object sender, EventArgs e)
    {
        con.Open();
        cmd = new SqlCommand("sp_student", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@Sname", txt_Sname.Text);
        cmd.Parameters.AddWithValue("@m1", txt_m1.Text);
        cmd.Parameters.AddWithValue("@m2", txt_m2.Text);
        cmd.Parameters.AddWithValue("@m3", txt_m3.Text);
        cmd.Parameters.AddWithValue("@m4", txt_m4.Text);
        cmd.ExecuteNonQuery();
        con.Close();
        viewdata();
     
    }

    public DataSet viewdata()
    {
        cmd = new SqlCommand("select *from Student",con);
        da = new SqlDataAdapter(cmd);
        ds=new DataSet();
        da.Fill(ds);
        gvCheckboxes.DataSource = ds;
        gvCheckboxes.DataBind();
        con.Close();
        return ds;
       
    }

}

Gridview with checkbox

 <script type="text/javascript">
        var TotalChkBx;
        var Counter;

        window.onload = function () {
            //Get total no. of CheckBoxes in side the GridView.
            TotalChkBx = parseInt('<%= this.gvCheckboxes.Rows.Count %>');

            //Get total no. of checked CheckBoxes in side the GridView.
            Counter = 0;
        }

        function HeaderClick(CheckBox) {
            //Get target base & child control.
            var TargetBaseControl =
       document.getElementById('<%= this.gvCheckboxes.ClientID %>');
            var TargetChildControl = "chkBxSelect";

            //Get all the control of the type INPUT in the base control.
            var Inputs = TargetBaseControl.getElementsByTagName("input");

            //Checked/Unchecked all the checkBoxes in side the GridView.
            for (var n = 0; n < Inputs.length; ++n)
                if (Inputs[n].type == 'checkbox' &&
                Inputs[n].id.indexOf(TargetChildControl, 0) >= 0)
                    Inputs[n].checked = CheckBox.checked;

            //Reset Counter
            Counter = CheckBox.checked ? TotalChkBx : 0;
        }

        function ChildClick(CheckBox, HCheckBox) {
            //get target control.
            var HeaderCheckBox = document.getElementById(HCheckBox);

            //Modifiy Counter;
            if (CheckBox.checked && Counter < TotalChkBx)
                Counter++;
            else if (Counter > 0)
                Counter--;

            //Change state of the header CheckBox.
            if (Counter < TotalChkBx)
                HeaderCheckBox.checked = false;
            else if (Counter == TotalChkBx)
                HeaderCheckBox.checked = true;
        }
    </script>
<asp:GridView ID="gvCheckboxes" runat="server" AutoGenerateColumns="False" 
                        OnRowCreated="gvCheckboxes_RowCreated" 
                        onselectedindexchanged="gvCheckboxes_SelectedIndexChanged" 
                        DataKeyNames="sid">
                        <Columns>
                            <asp:TemplateField HeaderText="Select">
                                <ItemTemplate>
                                    <asp:CheckBox ID="chkBxSelect" runat="server" />
                                </ItemTemplate>
                                <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="50px" />
                                <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="50px" />
                                <HeaderTemplate>
                                    <asp:CheckBox ID="chkBxHeader" onclick="javascript:HeaderClick(this);" runat="server" />
                                </HeaderTemplate>
                            </asp:TemplateField>
                            <asp:BoundField HeaderText="Student Name" DataField="Sname" ReadOnly="True">
                                <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="50px" />
                                <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="50px" />
                            </asp:BoundField>
                            <asp:BoundField DataField="m1" HeaderText="Mark1" ReadOnly="True" />
                            <asp:BoundField DataField="m2" HeaderText="Mark2" ReadOnly="True" />
                            <asp:BoundField DataField="m3" HeaderText="Mark3" ReadOnly="True" />
                            <asp:BoundField DataField="m4" HeaderText="Mark4" ReadOnly="True" />
                            
                        </Columns>
                    </asp:GridView>

 protected void gvCheckboxes_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow &&
   (e.Row.RowState == DataControlRowState.Normal ||
    e.Row.RowState == DataControlRowState.Alternate))
        {
            CheckBox chkBxSelect = (CheckBox)e.Row.Cells[1].FindControl("chkBxSelect");
            CheckBox chkBxHeader = (CheckBox)this.gvCheckboxes.HeaderRow.FindControl("chkBxHeader");
            chkBxSelect.Attributes["onclick"] = string.Format
                                                   (
                                                      "javascript:ChildClick(this,'{0}');",
                                                      chkBxHeader.ClientID
                                                   );
        }
    }
    protected void Btn_Submit_Click(object sender, EventArgs e)
    {
        con.Open();
        cmd = new SqlCommand("sp_student", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@Sname", txt_Sname.Text);
        cmd.Parameters.AddWithValue("@m1", txt_m1.Text);
        cmd.Parameters.AddWithValue("@m2", txt_m2.Text);
        cmd.Parameters.AddWithValue("@m3", txt_m3.Text);
        cmd.Parameters.AddWithValue("@m4", txt_m4.Text);
        cmd.ExecuteNonQuery();
        con.Close();
        viewdata();
       
    }

    public DataSet viewdata()
    {
        cmd = new SqlCommand("select *from Student",con);
        da = new SqlDataAdapter(cmd);
        ds=new DataSet();
        da.Fill(ds);
        gvCheckboxes.DataSource = ds;
        gvCheckboxes.DataBind();
        con.Close();
        return ds;
        
    }