Monday 2 May 2016

Export grid Data into excel and word

Here we will learn How to export grid data into Excel  And Word .
Add an aspx page and write the following code on .aspx Page.
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1">
<title>Export Gridview with Images in Asp.net</title>
<style type="text/css">
.GridviewDesign {font-size: 100%; font-family: 'Trebuchet MS', 'Lucida Sans Unicode', 'Lucida Grande', 'Lucida Sans', Arial, sans-serif color: #303933;}
.headerstyleForGrid
{
color:#FFFFFF;border-right-color:#abb079;border-bottom-color:#abb079;background-color: #df5015;padding:0.5em 0.5em 0.5em 0.5em;text-align:center;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div class="GridviewDesign">
<asp:GridView ID="gvDetailsForExport" CssClass="Gridview" runat="server" AutoGenerateColumns="False">
<HeaderStyle CssClass="headerstyleForGrid" />
<Columns>
<asp:BoundField HeaderText="User Id" DataField="UserId" />
<asp:BoundField HeaderText="User Name" DataField="UserName" />
<asp:BoundField HeaderText="Gender" DataField="Gender" />
<asp:ImageField DataImageUrlField="Imagepath" HeaderText="Image" ItemStyle-Height="25px" ItemStyle-Width="25px" />
</Columns>
</asp:GridView>
<br />
<asp:Button ID="btnExportToExcel" runat="server" Text="Export Data To Excel"
onclick="btnExport_Click" />
&nbsp;
<asp:Button ID="btnExportToWord" runat="server" Text="Export Data To Word"
onclick="btnExportToWord_Click" />
</div>
</form>
</body>
</html>

After completion code for .aspx page write the following code on .aspx.cs page
    protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                gvDetailsForExport.DataSource = BindData();
                gvDetailsForExport.DataBind();
            }
        }
      
    
        protected DataTable BindData()
        {
            DataTable dtGrid = new DataTable();
            dtGrid.Columns.Add("UserId", typeof(Int32));
            dtGrid.Columns.Add("UserName", typeof(string));
            dtGrid.Columns.Add("Gender", typeof(string));
            dtGrid.Columns.Add("Imagepath", typeof(string));

            dtGrid.Rows.Add(1,"Munesh","Male","Images/uploads/Sign1.jpg");
            dtGrid.Rows.Add(2,"Rahul","Male","Images/uploads/Sign2.jpg");
            dtGrid.Rows.Add(3,"Reet","Female","Images/uploads/Sign3.jpg");
            dtGrid.Rows.Add(4,"Anshuman","Male","Images/uploads/Sign4.jpg");     
            return dtGrid;
        }
        protected void btnExport_Click(object sender, EventArgs e)
        {
            Response.ClearContent();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Employees.xls"));
            Response.ContentType = "application/ms-excel";
            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            gvDetailsForExport.AllowPaging = false;
            gvDetailsForExport.DataSource = BindData();
            gvDetailsForExport.DataBind();
            gvDetailsForExport.HeaderRow.Style.Add("background-color", "#FFFFFF");
            for (int i = 0; i < gvDetailsForExport.HeaderRow.Cells.Count; i++)
            {
                gvDetailsForExport.HeaderRow.Cells[i].Style.Add("background-color", "#df5015");
            }
            gvDetailsForExport.RenderControl(htw);
            Response.Write(sw.ToString());
            Response.End();
        }

        protected void btnExportToWord_Click(object sender, EventArgs e)
        {
            Response.ClearContent();
            Response.Buffer = true;        

            Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Employees.doc"));
            Response.ContentType = "application/ms-word";

            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            gvDetailsForExport.AllowPaging = false;
            gvDetailsForExport.DataSource = BindData();
            gvDetailsForExport.DataBind();
            //Change the Header Row back to white color
            gvDetailsForExport.HeaderRow.Style.Add("background-color", "#FFFFFF");
            //Applying stlye to gridview header cells
            for (int i = 0; i < gvDetailsForExport.HeaderRow.Cells.Count; i++)
            {
                gvDetailsForExport.HeaderRow.Cells[i].Style.Add("background-color", "#df5015");
            }
            gvDetailsForExport.RenderControl(htw);
            Response.Write(sw.ToString());
            Response.End();
        }

Now  run your application and see the output and perform operation  On Gridview.


Click Here to Download Semple code Download

Monday 29 February 2016

Code-Behind for Web Application Projects

In this tutorial we will learn the code-behind model and the project structure of pages built For Web Application Projects.
ASP.NET Pages in web project have associated with two files  one is a .aspx file that contains the html and declarative server control mark-up, and the other page is a .cs "code-behind" file that contains the UI logic for the page
Control mark-up declarations are defined within the .aspx file itself.  For example:
And corresponding protected field declarations are added in the .cs code-behind class that match the name and type of controls defined within the .aspx file. For example:
Here we want to show date in the label when we select date from the calendar,so for that see the following code.

You can then set a breakpoint (press the F9 key on the line to set it on), and then hit F5 to compile, run and debug the page:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace FirstWebApplication
{
    public partial class FirstWebPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!Page.IsPostBack)
            {
                Calendar1.SelectedDate = DateTime.Now;
                Label1.Text = "please select a date from calendar";
            }
        }
    }
}

Now if you run your application your result will be

Handling server events from controls in our .aspx page
To handle a server event from a control on your page, you can either manually add an event-handler to the control yourself (by overridng the OnInit method in your code-behind class and adding the event delegate there), or by using the WYSIWYG designer to generate an event handler.
You can double-click on any of the events to automatically add a default named event handler.  Alternatively, you can type the name of the event handler you wish to generate.

You can then add whatever code you want within the code-behind file:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace FirstWebApplication
{
    public partial class FirstWebPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!Page.IsPostBack)
            {
                Calendar1.SelectedDate = DateTime.Now;
                Label1.Text = "please select a date from calendar";
            }
        }

        protected void Calendar1_SelectionChanged(object sender, EventArgs e)
        {
            Label1.Text = "Your selected date from calendar is '" + Calendar1.SelectedDate.ToShortDateString() +"'";
        }
    }
}
Now run your application and select a date from calendar then o/p will be




C# program Selection Sorting

Selection sort is a straightforward sorting algorithm. This algorithm search for the smallest number in the elements array and then swap i...