@ "C#" "True" "Default - Aspx.Cs" " - Default": Language Autoeventwireup Codefile Inherits

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 15

What is ASP.NET? Active Server Pages for .

NET is a web technology from Microsoft which can use different languages under .NET like VB, C#, J# etc. ASP.NET provides two development models 1. Single Page Model 2. Two Page Model In case of single page model, presentation (tag) and logic (program code) get placed in same file with .aspx extension In case of two page mode, presentation and logic get placed in two different files as .aspx and .aspx.cs or .aspx.vb etc. To merge the presentation and logic files using an attribute of Page directive. For Website
<%@ Page Language="C#" AutoEventWireup="true" Inherits="_Default" %> CodeFile="Default.aspx.cs"

For Web Application


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="batch06_2604._Default" %>

Types of Web Project ASP.NET provides two kinds of web projects 1. ASP.NET Website a. No Project file or Solution file 2. ASP.NET Web Application a. Has Project file and solution file Creating an ASP.NET Website File New Website ASP.NET Website Creating a web application File New Project ASP.NET Web Application

Requirements 1. Client a. Browser 2. Web Server a. ASP.NET Web Development Server i. Free with Visual Studio for Testing purpose only b. Internet Information Server (IIS) i. Free with MS Windows Components of an ASP.NET Page 1. HTML Tags 2. Server Side Tags or ASP.NET Tags a. Must have runat=server attribute b. Starts with asp: 3. Intrinsic Objects or Built-in Object a. Request b. Response c. Session d. Server e. Application f. ViewState g. Trace 4. Directives a. Provides instructions to the compiler i. Use <%@ and %> to add a directive ii. Example 1. Page 2. Include 3. Register 4. etc. iii. <%@ Page Language=C# %> 1. Default language is VB 5. Scriptlet a. A section inside the page to merge a language code along with the tags b. Use <% and %> delimiters c. Example
<% int n = 6; for (int i = 1; i <= 10; i++) Response.Write(i * n + "<br>");

%>

6. Expressions a. To show value of some variable or expression b. Use <%= and %> delimiters
<% int n = 6; for (int i = 1; i <= 10; i++) {

%>

<%=n%> X <%=i%> = <%=n * i%><br /> <% %> }

What is Web Form? A web form is a class inherited from Page class of System.Web.UI namespace. It allows to use OOPs concepts in ASP.NET Passing data a web form 1. Using QueryString or Get Method a. http://localhost:1839/querystringtest.aspx?num=12 2. Using Form or Post method
protected void Page_Load(object sender, EventArgs e) { if (Request["num"] != null) { int num = int.Parse(Request["num"]); for (int i = 1; i <= 10; i++) Response.Write(i * num + "<br>"); } }

28.04.2011 Working with Controls Label control Text TextBox control Text MaxLength ReadOnly TextMode Singleline/password/multiline Button control ImageButton control LinkButton control Hyperlink Image ImageMap to use different hotspots for different hyperlink Hotspots can be of three types a. Circular b. Rectangle c. Polygon to select only one item from list of items To add items use Items collection Use SelectedItem and SelectedValue properties Each item is an object of ListItem class a. ListItem(string text, string value) SelectedItem To show an image ImageUrl To navigate to given URL Text ImageUrl NavigateUrl Target To create hyperlink type button Text To use an image as button ImageUrl To create a push button Text

DropdownList -

a. Text b. Value Note: Use IsPostBack property of Page class to perform an action on Form Load, only for once
public partial class WebForm1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { DropDownList1.Items.Add(new ListItem("Production", "4")); DropDownList1.Items.Add(new ListItem("Human Resource", "5")); } } protected void Button1_Click(object sender, EventArgs e) { Response.Write(DropDownList1.SelectedItem.Text); } }

Note: To perform some action when an item get selected in drop down list use AutoPostBack Property of DropDownList Note: To avoid flickering when a post back occurs use AJAX controls 1. ScriptManager 2. UpdatePanel If using the ScriptManager, Response.Write() will not work
<body> <form id="form1" runat="server"> <asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptManager> <asp:UpdatePanel ID="UpdatePanel1" runat="server"> <ContentTemplate> <div> <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" onselectedindexchanged="DropDownList1_SelectedIndexChanged"> <asp:ListItem Value="1">Sales</asp:ListItem> <asp:ListItem Value="2">Marketing</asp:ListItem> <asp:ListItem Value="3">IT</asp:ListItem> </asp:DropDownList> <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" /> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> </div> </ContentTemplate> </asp:UpdatePanel>

</form> </body>

Providing fix location to the controls Select the control then Format Position Absolute Now drag and drop the control to any location 30.04.2011 Using Data Source Controls ASP.NET provides advanced controls for database operations 1. SqlDataSource a. All databases 2. AccessDataSource a. Only for MS Access 3. XmlDataSource a. Only for XML Files 4. ObjectDataSource a. Only for collections 5. SitemapDataSource a. For .sitemap files 6. LinqDataSource a. For LINQ (Language Integrated Query) Using GridView control Most advance control to work with database a. DataSourceId

Case 1: Using MS Access as database Create a database and add into App_Data folders Add the AccessDataSource control to select the database and tables then define the different statements 1. 2. 3. 4. InsertQuery UpdateQuery DeleteQuery SelectQuery

5. Insert() 6. Update() 7. Delete() Example Create a database as batch06.mdb and define a table as Student rollno, name and course Create a web form to insert, update and delete records Show the current records in the table. First select your database and tables fields. It makes a SelectQuery To create an Insert Query select InsertQuery property and create the query using QueryBuilder. Map the parameters with the control names or some other information.

Setting properties of GridView Select the GridView control and Columns property Set the HeaderText property for headings

To select a records and pass it back to controls add the Select button

Double Click on Gridview control to get an event SelectedIndexChanged Get data from current row and pass it to the controls
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" DataKeyNames="rollno" DataSourceID="AccessDataSource1" ForeColor="#333333" GridLines="None" OnSelectedIndexChanged="GridView1_SelectedIndexChanged"> <RowStyle BackColor="#EFF3FB" /> <Columns> <asp:BoundField DataField="rollno" HeaderText="Roll No" ReadOnly="True" SortExpression="rollno" /> <asp:BoundField DataField="name" HeaderText="Name" SortExpression="name" /> <asp:BoundField DataField="course" HeaderText="Course" SortExpression="course" /> <asp:CommandField ShowSelectButton="True" /> </Columns> <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" /> <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />

<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> <EditRowStyle BackColor="#2461BF" /> <AlternatingRowStyle BackColor="White" /> </asp:GridView> protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) { TextBox1.Text = GridView1.SelectedRow.Cells[0].Text; TextBox2.Text = GridView1.SelectedRow.Cells[1].Text; for (int i = 0; i < DropDownList1.Items.Count; i++) { if (GridView1.SelectedRow.Cells[2].Text == DropDownList1.Items[i].Text) { DropDownList1.SelectedIndex = i; break; } } }

Using HTML page with .ASPX Page


protected void Page_Load(object sender, EventArgs e) { int rollno = int.Parse(Request.Form["txtRollNo"]); string name = Request.Form["txtName"]; string course = Request.Form["txtCourse"]; OleDbConnection cn = new OleDbConnection(); cn.ConnectionString=@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\Batches2011\06\batch06.mdb"; cn.Open(); OleDbCommand cmd = new OleDbCommand(); cmd.CommandText = "INSERT INTO student VALUES(?,?,?)"; cmd.Connection = cn; cmd.Parameters.AddWithValue("rollno", rollno); cmd.Parameters.AddWithValue("name", name); cmd.Parameters.AddWithValue("course", course); cmd.ExecuteNonQuery(); cn.Close(); Response.Write("Record Saved"); }

You might also like