Click here to Skip to main content
14,691,039 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Gridview qwith Checkbox using Java script
select all options
Posted

1 solution

<script type="text/javascript">


        function checkAll(objRef) {
            var GridView = objRef.parentNode.parentNode.parentNode;
            var inputList = GridView.getElementsByTagName("input");
            for (var i = 0; i < inputList.length; i++) {
                //Get the Cell To find out ColumnIndex
                var row = inputList[i].parentNode.parentNode;
                if (inputList[i].type == "checkbox" && objRef != inputList[i]) {
                    if (objRef.checked) {
                        //If the header checkbox is checked
                        //check all checkboxes
                        //and highlight all rows
                        //row.style.backgroundColor = "#ff5959";
                        inputList[i].checked = true;
                    }
                    else {
                        //If the header checkbox is checked
                        //uncheck all checkboxes
                        //and change rowcolor back to original
                        if (row.rowIndex % 2 == 0) {
                            //Alternating Row Color
                            // row.style.backgroundColor = "#ececec";
                        }
                        else {
                            //row.style.backgroundColor = "#d8d8d8";
                        }
                        inputList[i].checked = false;
                    }
                }
            }
        }
        function Check_Click(objRef) {
            //Get the Row based on checkbox
            var row = objRef.parentNode.parentNode;
            if (objRef.checked) {
                //If checked change color to Aqua
                //row.style.backgroundColor = "#ff5959";
            }
            else {
                //If not checked change back to original color
                if (row.rowIndex % 2 == 0) {
                    //Alternating Row Color
                    // row.style.backgroundColor = "#ececec";
                }
                else {
                    // row.style.backgroundColor = "#d8d8d8";
                }
            }

            //Get the reference of GridView
            var GridView = row.parentNode;

            //Get all input elements in Gridview
            var inputList = GridView.getElementsByTagName("input");

            for (var i = 0; i < inputList.length; i++) {
                //The First element is the Header Checkbox
                var headerCheckBox = inputList[0];

                //Based on all or none checkboxes
                //are checked check/uncheck Header Checkbox
                var checked = true;
                if (inputList[i].type == "checkbox" && inputList[i] != headerCheckBox) {
                    if (!inputList[i].checked) {
                        checked = false;
                        break;
                    }
                }
            }
            headerCheckBox.checked = checked;
        }





--------------
<asp:GridView ID="grdRequsers" runat="server" AllowPaging="True" AllowSorting="True"
                                   AutoGenerateColumns="False" OnSelectedIndexChanging="grdRequsers_SelectedIndexChanging" DataKeyNames="CID,ipaddress">

<Columns>
                                       <asp:TemplateField HeaderText="" HeaderStyle-Width="5px">
                                           <HeaderTemplate>
                                               <asp:CheckBox Text="" ID="chkSelectAll" runat="server" onclick="checkAll(this);"

                                                   TextAlign="Left" />
                                           </HeaderTemplate>
                                           <ItemTemplate>
                                               <asp:CheckBox ID="chkSelect" runat="server" onclick="Check_Click(this)" />
                                           </ItemTemplate>
                                       </asp:TemplateField>
                                       <asp:TemplateField HeaderText="sd" SortExpression="sd" HeaderStyle-Width="70px"

                                           HeaderStyle-BorderColor="White">
                                           <ItemTemplate>
                                               <asp:Label ID="lblPostCode" runat="server" Text='<%# Eval("sd") %>'></asp:Label>
                                           </ItemTemplate>
                                       </asp:TemplateField>






-------------------------------
protected void grd1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
  {
      var tempid = Convert.ToInt64(grd1.DataKeys[e.NewSelectedIndex].Values[0]);
      Session["ViewID"] = tempid;
      Response.Redirect("NextPage.aspx");
 }

---------------
this is for next page


tempid = Convert.ToInt64(Session["ViewID"]);
SqlCommand cmd = new SqlCommand("SELECT * FROM tblname where id=" + tempid, con);

SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
txtid.Text = Convert.ToString(dr["id"]);
txtname.Text = Convert.ToString(dr["name"]);

}
con.Close();
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900