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




asp.net first web application


In this tutorial we will learn how to create, build and run your first asp.net web app using C# and the ASP.NET.
Creating a New Project

Select File->New Project within the Visual Studio 2013. This will open the New Project dialog screen. Click on the “Visual C#” node in the tree-view on the left hand side of the dialog box and choose the "ASP.NET Web Application" icon:





Choose location where you want this project to be created on system Then give the name of this project and click ok.

Visual Studio will then create and open a new web project within the solution explorer. By default it will create a single page (Default.aspx), an AssemblyInfo.cs file in properties , as well as a web.config file.

Visual stdio 2005 screen-


Visual stdio -2013


In VS-2005 this default.aspx will be empty page but in VS-2013 there will be created page means some text are there by default created by the Microsoft.
So for understanding purpose we will add a new item in this project name will be “FirstWebPage.aspx”.
So for that right click on your application and click on add new item and select a empty web Form give the for this and click Add.




After click on Add new item following screen will open



After click on Add button this “FirstWebPage.aspx” webform will add to the project.
Now click on “FirstWebPage.aspx” webform it will open as HTML format(Source file) we can see it below of that page. We can go in design mode from there.
We can made changes in screen from Source mode and design mode.

Here we are drag and drop a calendar and a label from toolbox .we can change view of calendar using calendar properties.



Html Code will be

<body>
    <form id="form1" runat="server">
    <div>
   
        Hello your first asp.net Application<br />
        <br />
        <asp:Calendar ID="Calendar1" runat="server" BackColor="White" BorderColor="Black" BorderStyle="Solid" CellSpacing="1" Font-Names="Verdana" Font-Size="9pt" ForeColor="Black" Height="250px" NextPrevFormat="ShortMonth" Width="330px">
            <DayHeaderStyle Font-Bold="True" Font-Size="8pt" ForeColor="#333333" Height="8pt" />
            <DayStyle BackColor="#CCCCCC" />
            <NextPrevStyle Font-Bold="True" Font-Size="8pt" ForeColor="White" />
            <OtherMonthDayStyle ForeColor="#999999" />
            <SelectedDayStyle BackColor="#333399" ForeColor="White" />
            <TitleStyle BackColor="#333399" BorderStyle="Solid" Font-Bold="True" Font-Size="12pt" ForeColor="White" Height="12pt" />
            <TodayDayStyle BackColor="#999999" ForeColor="White" />
        </asp:Calendar>
        <br />
        <br />
        <asp:Label ID="Label1" runat="server" Height="27px" Text="Label" Width="200px"></asp:Label>
   
    </div>
    </form>
</body>


When you will run this application you will see this page on browser ,but before this we have to set “Startup project “ for that right click on “FirstWebPage.aspx” and set it as startup project  and run this application or you can see this page using open this browser With.






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