Pages

Monday, 9 July 2012

How to Check All check Boxes In grid view and retrieving selected records


protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if ((e.Row.RowType ==DataControlRowType.Header))
        {
            ((CheckBox)e.Row.FindControl("cbSelectAll")).Attributes.Add("onclick","javascript:SelectAll('" + ((CheckBox)e.Row.FindControl("cbSelectAll")).ClientID +"')");
        }
    }




<script type="text/javascript">
    function SelectAll(id)
     {
      var frm = document.forms[0];
         
           for (i=0;i<frm.elements.length;i++)
           {
               if (frm.elements[i].type =="checkbox")
               {
          frm.elements[i].checked =document.getElementById(id).checked;
               }
           }
       }
    </script>




protected void Button2_Click(object sender, EventArgs e)
    {
   
     
        string s = "";
        foreach (GridViewRow grv in GridView1.Rows)
        {
            Label l = ((Label)grv.FindControl("lbl"));
            CheckBox cb = ((CheckBox)grv.FindControl("cbSelProduct"));
            if (cb.Checked == true)
            {
                if (s == "")
                {
                    s = l.Text;
                }
                else
                {
                    s = s + "," + l.Text;
                }
            }
            txtEmails.Text = s;

        }

    }

Bind the data first to Gridview....................


No comments:

Post a Comment