Page Directives Page directives specify optional settings used by the page compiler when processing ASP.NET files. The Web Forms page framework supports the following directives: · @ Page · @ Control · @ Import · @ Register · @ Assembly · @ OutputCache The @ Page Directive The @ Page directive defines page-specific attributes used by the ASP.NET page parser and compiler. <%@ page Trace="true" %> The @ Control Directive In addition to HTML and Web server controls, you also can create your own custom, reusable controls by using the same techniques you have learned to develop Web Forms pages. These controls are called user controls. The @ Import Directive The @ Import directive explicitly imports a namespace into a page, making all classes and interfaces of the imported namespace available to the page. The @ Register Directive The @ Register directive associates aliases with namespaces and class names for short description in custom server control syntax.
1
The @ Assembly Directive The @ Assembly directive declaratively links an assembly to the current page, making all of the assembly’s classes and interfaces available for use on the page. <%@ Assembly Name="myassembly.dll" %> The @ OutputCache Directive The @ OutputCache directive declaratively controls the output caching policies of a page. <%@ OutputCache Duration="10" %> USE OF APPLICATION OBJECTS
private void Button1_Click(object sender, System.EventArgs e) { Response.Write(Application["test"].ToString()); Response.Write("
"+"Hello friends"); 2
}
TextBox1.Text="Hello friends";
private void Button2_Click(object sender, System.EventArgs e) { Response.Redirect("Webform2.aspx"); } private void Button3_Click(object sender, System.EventArgs e) { ListBox1.Items.Clear(); for(Application["test2"]=1;(Int32)Application["test2"]<=10;) { ListBox1.Items.Add(Application["test2"].ToString()); Application["test2"]=(Int32)Application["test2"]+1; } string[] arr2 = new string[3]; arr2 = (string[])Application["arr"]; for(int k=0;k<arr2.Length;k++) { ListBox2.Items.Add(arr2[k].ToString()); } } private void Button4_Click(object sender, System.EventArgs e) { TextBox2.Text=Application["test2"].ToString(); }
3
USE OF VALIDATION CONTROLS
USE OF CUSTOM CONTROL
4
<script language="JavaScript"> function check(objSource, objArgs) { var num=objArgs.Value var valid=true if(num % 2 == 0) { valid=true } else { valid=false } 5
objArgs.IsValid = valid; //return objArgs; } SERVER OBJECTS
private void RadioButtonList1_SelectedIndexChanged(object sender, System.EventArgs e) { if (RadioButtonList1.SelectedIndex==0) { Label1.Text=Server.MachineName.ToString(); } else if(RadioButtonList1.SelectedIndex==1) { 6
Label1.Text=Server.UrlDecode("http://localhost/chaitanyaweb/WebF orm3.aspx"); } else if(RadioButtonList1.SelectedIndex==2) { Label1.Text=Server.UrlEncode("http://localhost/chaitanyaweb/WebF orm3.aspx"); } else if(RadioButtonList1.SelectedIndex==3) { Label1.Text=Server.HtmlEncode(""+"hi..friends"+"
"); } else if(RadioButtonList1.SelectedIndex==4) { Label1.Text=Server.HtmlDecode(""+"hi..friends"+"
"); } else if(RadioButtonList1.SelectedIndex==5) { Server.Transfer("WebForm1.aspx"); } else if(RadioButtonList1.SelectedIndex==6) { Label1.Text="Server.Execute works in page load event"; } } private void Button1_Click(object sender, System.EventArgs e) { string str="images//flower45.jpg"; Image1.ImageUrl=Server.MapPath(str); }
7
COOKIES
private void Page_Load(object sender, System.EventArgs e) { HttpCookie ck = new HttpCookie("userinfo"); ck.Values.Add("uid",TextBox1.Text); ck.Values.Add("pwd",TextBox2.Text); DateTime dt = DateTime.Now; TimeSpan ts = new TimeSpan(0,0,2,0,0); ck.Expires = dt.Add(ts); Response.Cookies.Add(ck); Session["first"]=TextBox1.Text.ToString(); } private void Button2_Click(object sender,System.EventArgs e) { 8
}
Response.Redirect("WebForm5.aspx");
private void Page_Load(object sender, System.EventArgs e) { Response.Write(""+Session.SessionID.ToString()+"
"); Label1.Text="Hi.. " + Session["first"].ToString(); Response.Write(""+"COOKIES DETAILS"+"
"); HttpCookie ck1 = Request.Cookies["userinfo"]; Response.Write("User id is: " + ck1.Values["uid"].ToString()); Response.Write("Password is: " + ck1.Values["pwd"].ToString()); } SESSION PROPERTIES
9
private void RadioButtonList1_SelectedIndexChanged(object sender, System.EventArgs e) { if(RadioButtonList1.SelectedIndex==0) { Label1.Text=Session.SessionID.ToString(); } else if(RadioButtonList1.SelectedIndex==1) { Label1.Text=Session.Timeout.ToString(); } else if(RadioButtonList1.SelectedIndex==2) { Label1.Text=Session.LCID.ToString(); } else if(RadioButtonList1.SelectedIndex==3) { 10
Label1.Text=Session.IsNewSession.ToString(); } else if(RadioButtonList1.SelectedIndex==5) { Label1.Text=Session.Count.ToString(); } } DATABASE PROGRAMS USING ADO.NET
USING SQLDATAADAPTER
private void Page_Load(object sender, System.EventArgs e) {
11
SqlConnection cn = new SqlConnection("server=localhost;uid=sa;database=muser"); cn.Open(); SqlDataAdapter adp = new SqlDataAdapter("select * from uaccount",cn); DataSet ds = new DataSet(); adp.Fill(ds,"uaccount"); DataGrid1.DataSource=ds.Tables["uaccount"]; DataGrid1.DataBind(); } USING OLEDB
private void Page_Load(object sender, System.EventArgs e) { OleDbConnection cn = new OleDbConnection("provider=sqloledb;uid=sa;initial catalog=muser"); cn.Open(); OleDbCommand cmd = new OleDbCommand("select * from uaccount",cn); //DataGrid1.DataSource=cmd.ExecuteReader(); OleDbDataReader dr; dr = cmd.ExecuteReader(); DataGrid1.DataSource=dr; DataGrid1.DataBind(); } USING DATAADAPTER,DATASET AND COMMAND
private void Page_Load(object sender, System.EventArgs e) { OleDbConnection cn = new OleDbConnection("provider=sqloledb;uid=sa;database=muser"); cn.Open(); string str="select * from uaccount"; OleDbCommand cmd = new OleDbCommand(); cmd.CommandText=str; cmd.Connection=cn; OleDbDataAdapter adp = new OleDbDataAdapter(); 12
adp.SelectCommand=cmd; DataSet ds = new DataSet(); adp.Fill(ds); DataGrid1.DataSource=ds.Tables[0]; DataGrid1.DataBind(); }
USING DATAVIEWS i.e. rowfilter and sort
private void Page_Load(object sender, System.EventArgs e) { OleDbConnection cn = new OleDbConnection("provider=microsoft.jet.oledb.4.0;data source="+Server.MapPath("MUSER.mdb")); cn.Open(); OleDbDataAdapter adp = new OleDbDataAdapter("select * from uaccount",cn); DataSet ds = new DataSet(); adp.Fill(ds,"uaccount"); DataView dv; dv = new DataView(ds.Tables[0]); dv.Sort="name"; dv.RowFilter="slno > 3 and slno <=7"; DataGrid1.DataSource=dv; DataGrid1.DataBind(); } SEARCHING RECORDS
13
OleDbConnection cn = new OleDbConnection("provider=sqloledb;uid=sa;database=muser"); private void Page_Load(object sender, System.EventArgs e) { if(!IsPostBack) { OleDbDataAdapter adp = new OleDbDataAdapter("select * from uaccount",cn); DataSet ds = new DataSet(); adp.Fill(ds,"uaccount"); DataGrid1.DataSource=ds.Tables[0]; DataGrid1.DataBind(); DropDownList1.DataTextField="slno"; DropDownList1.DataSource=ds.Tables[0]; DropDownList1.DataBind(); } } private void Button1_Click(object sender, System.EventArgs e) 14
{ OleDbCommand cmd = new OleDbCommand(); cmd.CommandText="select * from uaccount where slno = " + DropDownList1.SelectedItem; cmd.Connection=cn; cn.Open(); DataGrid1.DataSource=cmd.ExecuteReader(); DataGrid1.DataBind(); cn.Close(); } INSERTING, UPDATING, DELETING AND SEARCHING USING ADO.NET
OdbcConnection cn = new OdbcConnection("dsn=test;uid=sa"); private void Page_Load(object sender, System.EventArgs e) { if (!IsPostBack) { 15
OdbcDataAdapter adp = new OdbcDataAdapter("select * from dept",cn); OdbcDataAdapter adp1 = new OdbcDataAdapter("select * from emp",cn); DataSet ds = new DataSet(); DataSet ds1 = new DataSet(); adp.Fill(ds); adp1.Fill(ds1); DropDownList1.DataTextField="deptno"; DropDownList1.DataSource=ds.Tables[0]; DropDownList1.DataBind(); DropDownList2.DataTextField="ENO"; DropDownList2.DataSource=ds1.Tables[0]; DropDownList2.DataBind(); display(); } } protected void display() { OdbcDataAdapter adp = new OdbcDataAdapter("select * from emp",cn); DataSet ds = new DataSet(); adp.Fill(ds); DataGrid1.DataSource=ds.Tables[0]; DataGrid1.DataBind(); } private void Button1_Click(object sender, System.EventArgs e) { OdbcCommand cmd = new OdbcCommand("insert into emp values('" + TextBox1.Text + "', '" + TextBox2.Text + "','" + TextBox3.Text + "','" + TextBox4.Text + "','" + DropDownList1.SelectedItem.ToString() + "','" + TextBox5.Text + "','" + TextBox6.Text + "')",cn); cn.Open(); cmd.ExecuteNonQuery(); 16
cn.Close(); display(); } private void Button4_Click(object sender, System.EventArgs e) { OdbcCommand cmd = new OdbcCommand ("select * from emp where eno = '" + DropDownList2.SelectedItem + "' ",cn); OdbcDataReader dr; cn.Open(); dr=cmd.ExecuteReader(); dr.Read(); TextBox1.Text=dr[0].ToString(); TextBox2.Text=dr[1].ToString(); TextBox3.Text=dr[2].ToString(); TextBox4.Text=dr[3].ToString(); DropDownList1.SelectedValue=dr[4].ToString(); TextBox5.Text=dr[5].ToString(); TextBox6.Text=dr[6].ToString(); cn.Close(); } private void Button2_Click(object sender, System.EventArgs e) { string str; str="update emp set ename = '" + TextBox2.Text +"',addr = '" + TextBox3.Text +"',desig='" + TextBox4.Text +"',deptno = '"+ DropDownList1.SelectedItem +"',exp= " + TextBox5.Text +",salary =" + TextBox6.Text +" where eno = '"+ TextBox1.Text + "' "; OdbcCommand cmd = new OdbcCommand(); cmd.CommandText=str; cmd.Connection=cn; cn.Open(); cmd.ExecuteNonQuery(); cn.Close(); 17
display(); } private void Button3_Click(object sender, System.EventArgs e) { OdbcCommand cmd = new OdbcCommand(); cmd.CommandText="delete from emp where eno = '" + DropDownList2.SelectedItem.ToString() + "' "; cmd.Connection=cn; cn.Open(); cmd.ExecuteNonQuery(); cn.Close(); display(); } PROGRAM TO DISPLAY SUM OF MARKS AND TOTAL MARKS IN A DATAGRID
18
<%@ Page CodeBehind="WebForm22.aspx.cs" Language="c#" AutoEventWireup="false" Inherits="Webtrial.WebForm22" %> <%@ Register Tagprefix="uc1" tagname="myctrl" src="WebUserControl1.ascx" %>
SqlConnection cn = new SqlConnection("server=localhost;uid=sa;database=student_det ails"); double sum; private void Page_Load(object sender, System.EventArgs e) { cn.Open(); databind(); 19
} void databind() { SqlDataAdapter adp = new SqlDataAdapter("select * from marks1",cn); DataSet ds = new DataSet(); adp.Fill(ds,"marks"); DataColumn dc; dc = new DataColumn("Total", Type.GetType("System.Double")); dc.Expression="math+sci+sst"; ds.Tables[0].Columns.Add(dc); DataGrid1.DataSource=ds; DataGrid1.DataBind(); } protected void gtot(double s) { sum += s; } private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e) { if ((e.Item.ItemType == ListItemType.Item) || ( e.Item.ItemType == ListItemType.AlternatingItem)) { gtot(Double.Parse( e.Item.Cells[5].Text )); } else if (e.Item.ItemType ==ListItemType.Footer ) { e.Item.Cells [4].Text =" Total "; e.Item.Cells[5].Text =sum.ToString(); } } USE OF DATA REPEATERS
20
<%@ Import Namespace="System.Data.SqlClient" %> <%@ Import Namespace="System.Data" %> <script language="C#" runat="server"> private void Page_Load(object sender, System.EventArgs e) { SqlConnection cn = new SqlConnection("server=localhost;uid=sa;database=railway"); SqlDataAdapter adp = new SqlDataAdapter("select * from emp",cn); DataSet ds = new DataSet(); adp.Fill(ds); Repeater1.DataSource=ds.Tables[0]; Repeater1.DataBind(); }
USE OF DATA LIST CONTROL
23
<%@ Import Namespace="System.Data" %> <%@ Import Namespace="System.Data.SqlClient" %> <script language="C#" runat="server"> private void Page_Load(object sender, System.EventArgs e) { SqlConnection cn = new SqlConnection("server=localhost;uid=sa;database=student_details"); SqlDataAdapter adp = new SqlDataAdapter("select * from student",cn); DataSet ds = new DataSet(); adp.Fill(ds); DataList1.DataSource=ds.Tables[0]; DataList1.DataBind(); } 24