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




No comments:

Post a Comment

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...