+ All Categories
Home > Documents > ASP.net Tutorials

ASP.net Tutorials

Date post: 14-Sep-2015
Category:
Upload: akshay-kalra
View: 26 times
Download: 0 times
Share this document with a friend
Description:
ASP.net Tutorials
58
Transcript
  • ASP.NET is a Microsoft Technology.ASP stands for Active Server PagesASP.NET is a program that runs inside IIS.IIS (Internet Information Services) is Microsoft's Internet server.IIS comes as a free component with Windows servers.IIS is also a part of Windows 2000 and XP Professional.

  • Client-side technologiesHTML, DHTML, JavaScriptServer-side technologiesASP (Active Server Pages)ASP.NET is the next generation of ASP

  • ASP.NET is the next generation ASP, but it's not an upgraded version of ASP. ASP.NET is an entirely new technology for server-side scripting. ASP.NET is a part of the Microsoft .NET framework, and a powerful tool for creating dynamic and interactive web pages. ASP.NET is a server side scripting technology that enables scripts (embedded in web pages) to be executed by an Internet server

  • An ASP.NET file is just the same as an HTML fileAn ASP.NET file can contain HTML, XML, and scriptsScripts in an ASP.NET file are executed on the serverAn ASP.NET file has the file extension ".aspx"

  • BrowserASP.NETButton code...List code...Text code...Event handlers

  • Page_LoadPage_UnloadTextbox1_ChangedButton1_ClickInitializeRestore Control StateSave Control StateRender1. Change Events2. Action EventsPage_InitControl EventsLoad PageUnload Page

  • HTML Controls - Traditional HTML tagsWeb Server Controls - New ASP.NET tagsValidation Server Controls - For input validationData Control Database bindingNavigation Control Page NavigateAnd many more.

  • Code at Button or Page_load event

    Label1.Text = TextBox1.Text;

    Label1.Text =Welcome in asp.net page;

    Ideally Label control is used to place a static, non clickable (can't fire onclick event) piece of text on the page..

  • Code at Button or Page_load event

    Label1.Text = TextBox1.Text;

    TextBox1.Text =Welcome in asp.net page;

    TextBox control is used to enter data into the form that can be sent to the web server by posting the form. And also convert it into multiline or password fields.

  • Code at Button event

    TextBox1.Text =Welcome in asp.net page;

    Button control is generally used to post the form or fire an event either client side or server side. When it is rendered on the page, it is generally implemented through

  • Process: Right Click on LinkButton ->>properties->Postback Url->>set page->OKOrDouble click on Link btn->>code the followingsCode at link button Response.Redirect(test.aspx");

    The LinkButton control in ASP.NET is a HyperLink style Button. The LinkButton looks like a HyperLink control but it is a Button which works as a HyperLink.

  • Coding at Image Button

    Int a,b,c;A=10;B=34;C=a+b;Label1.text=Sum=+c;ImageButton control in ASP.NET is just like a Button control. The ImageButton control is used to display a clickable image. Events of ImageButton are the same as Button control. You will provide the image using ImageUrl property of the ImageButton control.

  • Coding At Property Google

    Hyperlink control in ASP.NET is used to link another webpage when the control is clicked. In this example we use two Hyperlink. When we click the Hyperlink it will redirect another page. The path of the web page will be given in NavigateUrl field.

  • The Dropdown List Web server control enables users to select from a single-selection drop-down list box. The Dropdown List control is similar to the List Box Web server control.

    Coding At DropDownSeleted Index Event

    If(DropDownList1.SeletedIndex==0){Label1.Text=Welcome;ElseStatement1;}

  • The ListBox control in ASP.NET is used to display a list of items with scroller. The user can click on items. A ListBox control provides single or multiple selections of item using SelectionMode property.

    Code is same as dropdownlistCoding At listboxSeleted Index Event

    If(Listbox1.SeletedIndex==0){Label1.Text=Welcome;ElseStatement1;}

  • The Checkbox Web server controls provide a way for users to switch between true-false or yes-no options.Coding At CheckboxCheckBox1_CheckedChanged Event

    If (Checkbox1.Checked){Label1.Text=You are Good;}

    Must set property autopostback true

  • CheckBoxList control in ASP.NET is used to select or deselect the item. ChechBoxList is just like a RadioButtonList control in HTML. CheckBoxList control contains CheckBoxes in a group.

    Coding At CheckBoxListSeleted Index Event

    If(CheckBoxList1.SeletedIndex==0){Label1.Text=Welcome;ElseStatement1;}

    Must set property autopostback true

  • RadioButton web server control is used for select or deselect an item when all these RadioButtons will be in a same group. Code at Radiobutton Event

    If(Radiobutton1.Checked){Label1.Text=Male;}

    Must set property autopostback trueAnd make a group for radiobutton

  • RadioButtonList control in ASP.NET is used to select a RadioButton. You can make any number of RadioButton in RadioButtonList

    Coding is Same as Checkbox list control

  • Image control in ASP.NET is used for designing the webpage and many other purpose. You can use image at design time or at run time(programmatically

    You can choose Image Control from the toolbar ->>and Set the path of image using imgurl Property->ok

  • Image Map control. This control enables you to create an image that has individual regions that users can click; these regions are referred to as hot spots. Each of these hot spots can be a separate hyperlink or can raise a post back event.

  • Coding for AdRotator in XML file

    image1.gif http://www.google.com After than rightclick on adrotator->>Choose file ->>select them->ok

    AdRotator is a Rich Web Control in ASP.NET. AdRotator control is used to make rotating advertisement in ASP.NET.It displays the different different images in web page when you refresh the web page. You can provide the information about the images which is displayed in AdRotator using XML file or database.

  • Code for upload the imagestring path = "~/Upload/" + FileUpload1.FileName; FileUpload1.SaveAs(Server.MapPath(path)); Image1.ImageUrl = path;

    Code for upload the file in driveif (FileUpload1.HasFile) { FileUpload1.SaveAs(@"D:\temp\" +FileUpload1.FileName); Label1.Text = "File Uploaded: " + FileUpload1.FileName +" "+System.DateTime.Now.ToLongDateString(); } else { Label1.Text = "No File Uploaded."; }

    The FileUpLoad control enables you to upload file to the server. It displays a text box control and a browse button that allow users to select a file to upload to the server.

  • Coding at page_loadif (!Page.IsPostBack) { MultiView1.ActiveViewIndex = 0; } Coding at buttonMultiView1.ActiveViewIndex += 1;

    MultiView control in ASP.NET acts as a container for group of view controls. You can see the all view in a single MultiView control in a single web page

  • Write here Text

    YoucanusethePanelcontrolasacontainerforothercontrols.Thisisparticularlyusefulwhenyouarecreatingcontentprogrammaticallyandyouneedawaytoinsertthecontentintothepage.

  • Coding at load event

    HyperLink hyperlink1 = new HyperLink(); hyperlink1.Text = google"; hyperlink1.NavigateUrl = "http://www.google.co.in/"; PlaceHolder1.Controls.Add(hyperlink1); PlaceHolder Web server control in ASP.NET is used to added the controls at run time.

  • A data source control interacts with the data-bound controls and hides the complex data binding processes. These are the tools that provide data to the data bound controls and support execution of operations like insertions, deletions, sorting and updates.

  • GridView ControlDetails ViewDatalist ViewForm viewList viewRepeater Control

  • The ASP.NET GridView control adding the ability to take advantage of specific capabilities of ASP.NET data source controls. to handle simple operations such as paging, sorting, editing or deleting data, the GridView control can automatically handle these operations provided its bound data source control supports these capabilities.

  • DetailsView Control:->Details View is a data-bound user interface control that renders a single record at a time from its associated data source, optionally providing paging buttons to navigate between records.

  • The DataList control displays data items in a repeating list, and optionally supports selecting and editing the items. The content and layout of list items in DataList is defined using templates.

  • Repeater Control is used to display repeated list of items that are bound to the control and its same as gridview and datagridview. Repeater control is lightweight and faster to display data when compared with gridview and datagrid. By using this control we can display data in custom format but its not possible in gridview or datagridview and it doesnt support for paging and sorting. .

    Employee Detail Emp Name is : Emp no. is : Salary is : By Abc...

  • One of the main tasks for every web developer is to check for errors while developing web pages. According to the server side terminology, this concept is called as Validation. Prior to ASP.NET, this task was achieved by using JavaScript and VBScript. JavaScript was commonly used by many developers and the main disadvantage of this is that developers had to write lot of code. ASP.NET simplified this task by providing us with built-in WebForm controls.There are six controls which are available for performing validation. They areRequiredFieldValidatorCompareValidatorRegularExpressionValidatorRangeValidatorCustomValidatorValidationSummary

  • This is one of the important controls used in majority of web applications. If a user fails to enter the required information on the form then the message given on this validation control will appear thereby forcing the user to fill up the required information. How to use RequiredFieldValidatator:Right click on Textbox->>Choose properties->>click Control to Validate->>ok

  • With the help of this control, you can compare the value of one control with the value of another control.How to use Compare Validator:Right click on Textbox->>Choose properties->>click Control to Validate->> and Compare Validator->>choose the textbox->>select type of valie->> and operator->>ok

  • his control is used to verify the accuracy of strings using special characters. It will check for the specific pattern which should match with the code. You have to specify a regular expression

    How to use Regular Expression Validator:Right click on Textbox->>Choose properties->>click on validation expression->>select any pre-defined expression->>ok

  • With the help of this control, you can check whether users have entered a value within a specific range as specified. You can specify the range values using MinimumValue and MaximumValue properties.How to use Range validator:Right click on Textbox->>Choose properties->>click Control to Validate->> and give Minimumvalue and maximumvalue ->>choose type of function ->>ok

  • The Validation controls that you have seen above are built-in within the .NET Framework. You have to apply them as such by utilizing its properties in your ASP.NET application. With the help of CustomValidator control, you can write your own validation rules and apply them on the appropriate WebForm control.Process: Right click on CustomValidator->>choose control to validate->>double click onCustom Validator->> type the followings colde:

  • This control collects all the values from the ErrorMessage property and displays them on ASP.NET page. The data will be presented either by directly on the page or inside a MessageBox.Right click on Validation->>choose Validation summary ->>true orMessagebox->>true

  • ADO.NET provides consistent access to data sources such as SQL Server and XML, and to data sources exposed through OLE DB and ODBC. Data-sharing consumer applications can use ADO.NET to connect to these data sources and retrieve, handle, and update the data that they contain. Some common ADO.NET PROVIDERADO.NET data providers:.NET Framework Data Provider for SQL Server (System.Data.SqlClient).NET Framework Data Provider for OLE DB (System.Data.OleDb).NET Framework Data Provider for ODBC (System.Data.Odbc).NET Framework Data Provider for Oracle (System.Data.OracleClient)

  • using System.Data.SqlClient;//namespace for sqlserverusing System.Data;string q;SqlConnection con;SqlDataAdapter adp;DataSet ds;

    q = "select * from emp"; con = new SqlConnection("data source=.\\sqlexpress;initial catalog=dbemp;integrated security=true"); con.Open(); ds = new DataSet(); adp = new SqlDataAdapter(q, con); adp.Fill(ds, "emp"); GridView1.DataSource =ds; GridView1.DataBind();

  • using System.Data.SqlClient;//namespace for sqlserverusing System.Data;Using System.Configuration;string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;

    void BindGridView() { SqlDataAdapter da = new SqlDataAdapter("select *from country", constr); DataSet ds = new DataSet(); da.Fill(ds); GridViewcountry.DataSource =ds; GridViewcountry.DataBind(); }

  • using System.Data.SqlClient;using System.Data;using System.Configuration;

    DataTable dt = new DataTable(); SqlConnection objcon = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["conn"].ToString()); SqlDataAdapter objda = new SqlDataAdapter("select * from emp", objcon); objda.Fill(dt); if (dt.Rows.Count > 0) {

    GridView1.DataSource = dt; GridView1.DataBind(); }

  • SqlConnection conn = new SqlConnection ("Data Source=.\\sqlexpress;Initial Catalog=dbemp;Integrated Security=True"); SqlCommand cmd = new SqlCommand ();

    cmd.Connection = conn; string strQuery = "Insert into st1 (id,name,marks) values (@roll,@name,@marks)" ;

    cmd.CommandText = strQuery;

    cmd.CommandType = CommandType .Text;

    //Adding Parameters to the Command Object. cmd.Parameters.AddWithValue( "@roll", Int32.Parse(TextBox1.Text)); cmd.Parameters.AddWithValue("@name", TextBox2.Text); cmd.Parameters.AddWithValue("@marks", Int32.Parse(TextBox3.Text)); //Connection Object Open conn.Open(); cmd.ExecuteNonQuery(); Response.Write("alert('Saved data in table:');");

  • q = "select username,password from login where username='" + TextBox1.Text + "'and password='" + TextBox2.Text + "'"; con = new SqlConnection("data source=.\\sqlexpress;initial catalog=user;integrated security=true"); con.Open(); cmd = new SqlCommand(q, con); SqlDataReader dr = cmd.ExecuteReader(); if (dr.Read()) { Response.Redirect("Welcome.aspx"); } else { //Label1.text="plz check username and passowrd"; Response.Redirect("Error.aspx"); }

  • q = "select ename ,job from emp where empno = " + TextBox1.Text + ""; cn = new SqlConnection("data source = .\\sqlexpress; integrated security = true; initial catalog = dbemp;"); cn.Open(); cmd = new SqlCommand(q, cn); SqlDataReader dr = cmd.ExecuteReader(); if (dr.Read()) { Label3.Visible = true; Label4.Visible = true; TextBox2.Visible = true; TextBox3.Visible = true; TextBox2.Text = dr[0].ToString(); TextBox3.Text = dr[1].ToString(); } else { Label2.Visible = true; Label2.Text = "No Such Record Exists"; }protected void TextBox1_TextChanged(object sender, EventArgs e) { Label2.Visible = false; Label3.Visible = false; Label4.Visible = false; TextBox2.Visible = false; TextBox3.Visible = false; }

  • Coding at button :q = "select * from emp where empno = " + TextBox1.Text + ""; cn = new SqlConnection("data source = .\\sqlexpress; integrated security = true; initial catalog = dbemp;"); cn.Open(); cmd = new SqlCommand(q, cn); SqlDataReader dr = cmd.ExecuteReader(); if (dr.HasRows) { GridView1.Visible = true; GridView1.DataSource = dr; GridView1.DataBind(); } else { Label2.Visible = true; GridView1.Visible = false; Label2.Text = "No Such Record Exists"; }Coding at Textbox Changed EventLabel2.Visible = false;GridView1.Visible = false;

  • Private void bindgridview(){

    Coding for binding the gridview with help of method

    }Call it page_load event

    if (!IsPostBack) { bindgridview(); }Coding at Gridview_PageIndexchangin

    GridView1.PageIndex = e.NewPageIndex; bindgridview();

  • Coding at button :private void showdata() { String ConnString = ConfigurationManager.ConnectionStrings["conString"].ConnectionString; SqlConnection con = new SqlConnection(ConnString); SqlCommand cmd = new SqlCommand(); // DataTable dt = new DataTable(); DataSet dt = new DataSet(); SqlDataAdapter adp = new SqlDataAdapter(cmd); cmd.CommandType = CommandType.StoredProcedure; //cmd.CommandText = "select * from emp"; cmd.CommandText = "show";//show is stored p name cmd.Connection = con; con.Open(); adp.Fill(dt); GridView1.DataSource = dt; GridView1.DataBind(); }Cal it in page_load eventif (!IsPostBack) { showdata(); }

  • Coding at button ://save data using stored proc String ConnString = ConfigurationManager.ConnectionStrings["conString"].ConnectionString; SqlConnection con = new SqlConnection(ConnString); SqlCommand cmd = new SqlCommand(); cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = "ins10";//name of stored p cmd.Parameters.Add("@roll", SqlDbType.Int).Value = TextBox1.Text; // cmd.Parameters.AddWithValue("@id", TextBox1.Text); cmd.Parameters.Add("@name", SqlDbType.VarChar).Value = TextBox2.Text.Trim(); cmd.Parameters.Add("@marks", SqlDbType.Int).Value = TextBox3.Text.Trim(); cmd.Connection = con; con.Open(); cmd.ExecuteNonQuery(); Label5.Text = "Record inserted successfully"; con.Close(); con.Dispose();

  • Coding at button ://save data using stored proc String ConnString = ConfigurationManager.ConnectionStrings["conString"].ConnectionString; SqlConnection con = new SqlConnection(ConnString); SqlCommand cmd = new SqlCommand(); cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = update";//name of stored p cmd.Parameters.Add("@roll", SqlDbType.Int).Value = TextBox1.Text; // cmd.Parameters.AddWithValue("@id", TextBox1.Text); cmd.Parameters.Add("@name", SqlDbType.VarChar).Value = TextBox2.Text.Trim(); cmd.Parameters.Add("@marks", SqlDbType.Int).Value = TextBox3.Text.Trim(); cmd.Connection = con; con.Open(); cmd.ExecuteNonQuery(); Label5.Text = "Record updated successfully"; con.Close(); con.Dispose();

  • Coding at button ://save data using stored proc String ConnString = ConfigurationManager.ConnectionStrings["conString"].ConnectionString; SqlConnection con = new SqlConnection(ConnString); SqlCommand cmd = new SqlCommand(); cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = deleteprd";//name of stored p cmd.Parameters.Add("@roll", SqlDbType.Int).Value = TextBox1.Text; // cmd.Parameters.AddWithValue("@id", TextBox1.Text); cmd.Connection = con; con.Open(); cmd.ExecuteNonQuery(); Label5.Text = "Record deleted successfully"; con.Close(); con.Dispose();

  • private void bindddl() { q = "select empno from emp"; cn = new SqlConnection("data source = .\\sqlexpress; integrated security = true; initial catalog = dbemp;"); cn.Open(); ds = new DataSet(); ad = new SqlDataAdapter(q, cn); ad.Fill(ds, "emp"); DropDownList1.DataSource = ds; // DropDownList1.DataTextField = "ename";//string value DropDownList1.DataValueField = "empno";//int value DropDownList1.DataBind();}

  • private void bind() {q = "select distinct city from police";cn = new SqlConnection("data source = .\\sqlexpress; integrated security = true; initial catalog =dbemp;");cn.Open();ds = new DataSet();ad = new SqlDataAdapter(q, cn);ad.Fill(ds, "police");DropDownList1.DataSource = ds.Tables[0];DropDownList1.DataTextField = "city";//DropDownList1.DataValueField = "empno";DropDownList1.DataBind(); }Coding at Button eventq = "select * from police where city = '"+DropDownList1 .SelectedItem .Text +"'"; cn = new SqlConnection("data source = .\\sqlexpress; integrated security = true; initial catalog = dbemp;"); cn.Open(); ds = new DataSet(); ad = new SqlDataAdapter(q, cn); ad.Fill(ds, "police"); GridView1.DataSource = ds.Tables[0]; GridView1.DataBind();

  • private void bind() {q = "select distinct city from police";cn = new SqlConnection("data source = .\\sqlexpress; integrated security = true; initial catalog =dbemp;");cn.Open();ds = new DataSet();ad = new SqlDataAdapter(q, cn);ad.Fill(ds, "police");DropDownList1.DataSource = ds.Tables[0];DropDownList1.DataTextField = "city";//DropDownList1.DataValueField = "empno";DropDownList1.DataBind();DropDownList1.Items.Insert(0, "");//add the Column in Dropdownlist }Coding at Dropdownlist event q = "select * from police where city = '"+DropDownList1 .SelectedItem .Text +"'"; cn = new SqlConnection("data source = .\\sqlexpress; integrated security = true; initial catalog = dbemp;"); cn.Open(); ds = new DataSet(); ad = new SqlDataAdapter(q, cn); ad.Fill(ds, "police"); GridView1.DataSource = ds.Tables[0]; GridView1.DataBind();

    **


Recommended