How to Add Check Box Select,Select All, Unselect and Unselect All Functionality in Gridview Column


First Add Check Box column in Gridview as Follows
 <asp:GridView runat="server" ID="grv" AutoGenerateColumns="false" GridLines="Vertical"  
     BorderWidth="0" AllowPaging="true" PagerStyle-HorizontalAlign="Center" OnRowCreated="grvClassCaption_RowCreated">  
     <Columns>  
       <asp:TemplateField HeaderStyle-HorizontalAlign="Center" HeaderText="Select All" ItemStyle-HorizontalAlign="Center"  
         HeaderStyle-CssClass="GridHeader" ItemStyle-Width="200px">  
         <HeaderTemplate>  
           <asp:CheckBox ID="chkHeader" runat="server" Text="Select All" Onclick="SetCheckBoxes(this)" />  
         </HeaderTemplate>  
         <ItemTemplate>  
           <asp:CheckBox ID="chkBox" runat="server" Text="Select" Onclick="ChangeHeader()" />  
         </ItemTemplate>  
       </asp:TemplateField>  
       <asp:TemplateField HeaderStyle-HorizontalAlign="Center" HeaderText="Name" ItemStyle-HorizontalAlign="Center"  
         HeaderStyle-CssClass="GridHeader" ItemStyle-VerticalAlign="Top" ItemStyle-CssClass="GridColumn"  
         ItemStyle-Width="200px">  
         <ItemTemplate>  
           <asp:Label ID="lblName" runat="server" Text='<%# Eval("Name")%>'></asp:Label>  
         </ItemTemplate>  
       </asp:TemplateField>  
     </Columns>  
   </asp:GridView>  
   <div>  
     <asp:Button ID="Button1" runat="server" Text="SAVE" OnClick="BtnSave_Click" />  
   </div>  

And ADD Following Script to the header of page
 <script type="text/javascript">  
     function ChangeHeader()  
     {  
       var Parent = document.getElementById("<%=grv.ClientID %>");  
       var chkList = Parent.getElementsByTagName('input');  
       chkList[0].checked = true;  
       for (i = 1; i < chkList.length; i++) {  
         if (!chkList[i].checked) {  
           chkList[0].checked = false;  
         }  
       }  
     }  
     function SetCheckBoxes(chkHeader)   
     {  
       var Parent = document.getElementById("<%=grv.ClientID %>");  
       var chkList = Parent.getElementsByTagName('input');  
       if (chkHeader.checked) {  
         for (i = 0; i < chkList.length; i++) {  
           chkList[i].checked = true;  
         }  
       }  
       else {  
         for (i = 0; i < chkList.length; i++) {  
           chkList[i].checked = false;  
         }  
       }  
     }  
 </script>  

On SAVE button click Check the check box Condition
  protected void BtnSave_Click(object sender, EventArgs e)  
     {  
         int count = 0;  
         for (int i = 0; i < grv.Rows.Count; i++)  
         {  
           GridViewRow row = grv.Rows[i];  
           bool isChecked = ((CheckBox)row.FindControl("chkBox")).Checked;  
           if (isChecked)  
           {  
             count++;  
           }  
         }  
         if (count != 0)  
         {  
           SaveData(); //save the data to database  
         }  
 }  
Read More »

How to Apply IE Specific CSS in ASP.net Application

First add the following meta tags on master page or content page ,where we are using particular CSS class.

 <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />

 <!--[if IE 8 ]> <html class="ie8"> <![endif]-->

For eg. Class abc 

And in CSS file write class as follows
And write browser specific properties in this class.

.ie8 .abc
{

//class style here

}
Read More »

Function To Convert Object of List Type into Datatable Object


public DataTable ConvertToDataTable<T>(IList<T> data)
        {
            PropertyDescriptorCollection properties =
               TypeDescriptor.GetProperties(typeof(T));
          
            foreach (PropertyDescriptor prop in properties)
            {
              
                table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
              
            }
            foreach (T item in data)
            {
                DataRow row = table.NewRow();
                foreach (PropertyDescriptor prop in properties)
                    row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
                table.Rows.Add(row);
            }
            return table;
        }
Read More »

Create And Export Excel File in Asp.net


 /* IT EXPORT DATA FROM DATA TABLE */


 protected void btnExportfromDt_Click(object sender, EventArgs e)
        {
            string strFilename = "StudDetails.xls";
            Export(LoadData(), strFilename);
        }
        protected void Export(DataTable dtEmp,string filename)
        {
            string attachment = "attachment; filename="+filename;
            Response.ClearContent();
            Response.AddHeader("content-disposition", attachment);
            Response.ContentType = "application/vnd.ms-excel";
            string tab = string.Empty;
            foreach (DataColumn dtcol in dtEmp.Columns)
            {
                Response.Write(tab + dtcol.ColumnName);
                tab = "\t";
            }
            Response.Write("\n");
            foreach (DataRow dr in dtEmp.Rows)
            {
                tab = "";
                for (int j = 0; j < dtEmp.Columns.Count; j++)
                {
                    Response.Write(tab + Convert.ToString(dr[j]));
                    tab = "\t";
                }
                Response.Write("\n");
            }
            Response.End();
        }

Note :For Visual Studio 10 use Trigger Tag For Update Panel To Download The File;
Read More »