aspnotes
aspnotes
net
It is a web framework designed and developed by Microsoft. It is used to
develop websites, web applications and web services. It provides fantastic
integration of HTML, CSS and JavaScript. It was first released in January
2002. It is built on the Common Language Runtime (CLR) and allows
programmers to write code using any supported .NET language.
Stage Description
• IIS –
IIS is a protocol server which is used to host a website on server. IIS
stands for Internet Information services.
What is global.asax :
Global.asax is an optional file which is used to handling higher level
application events such as Application_Start, Application_End,
Session_Start, Session_End etc. It is also popularly known as ASP.NET
Application File. This file resides in the root directory of an ASP.NET-based
application.
To create label either we can write code or use the drag and drop facility of
visual studio 2017.
This is server side control, asp provides own tag to create label. The example
is given below.
• This control has its own properties that are tabled below.
Property Description
2. TextBox -
This is an input control which is used to take user input. To
create TextBox either we can write code or use the drag and drop facility of
visual studio IDE.
This is server side control, asp provides own tag to create it. The example is
given below.
Server renders it as the HTML control and produces the following code to
the browser.
<input name="TextBox1" id="TextBox1" type="text">
This control has its own properties that are tabled below.
Property Description
3. Button -
This control is used to perform events. It is also used to submit client request
to the server. To create Button either we can write code or use the drag and
drop facility of visual studio IDE.
This is a server side control and asp provides own tag to create it. The
example is given below.
Server renders it as the HTML control and produces the following code to
the browser.
This control has its own properties that are tabled below.
Property Description
4. CheckBox -
It is used to get multiple inputs from the user. It allows user to select choices
from the set of choices.
This is a server side control and ASP.NET provides own tag to create it. The
example is given below.
<asp:CheckBox ID="CheckBox2" runat="server" Text="J2EE"/>
Server renders it as the HTML control and produces the following code to
the browser.
This control has its own properties that are tabled below.
Property Description
5. RadioButton -
It is an input control which is used to takes input from the user. It allows user
to select a choice from the group of choices.
This is a server side control and ASP.NET provides own tag to create it. The
example is given below.
Server renders it as the HTML control and produces the following code to
the browser.
This control has its own properties that are tabled below.
Property Description
We can also set Selected Date property that shows specified date in the
calendar.
To create Calendar we can drag it from the toolbox of visual studio.
This is a server side control and ASP.NET provides own tag to create it. The
example is given below.
This control has its own properties that are tabled below.
Property Description
2) DropDownList -
The DropDownList is a web server control which is used to create an HTML
Select component. It allows us to select an option from the dropdown list. It
can contain any number of items
• Menu Control-
Asp.net Menu Control feature provides a consistent way for your website
users to navigate your website. If you want to use navigation menu control
on your website, then follow these easy steps.
You can add Menu control to your .master page by simply drag and drop
from ToolBox > Navigation > Menu or add the following few lines of code
snippet.
• Master Page –
ASP.NET web page that uses master page for common UI, is called
as content page. Content pages merge with the master pages at
compile time to produce final output. The final page combines the
layout of the master page with the content from the content page.
Before Master pages feature in ASP.NET, developers created
consistent web pages by using custom controls, CSS and JavaScript.
Now developing consistent web pages are very easy with the help of
Master pages. You can dynamically change the common UI part on
master page from content pages.
A master page has the extension .master that can comprise static
text, HTML elements, and server controls. A master page is similar to
an ASP.NET page but it contains @Master directive at the top and
one or more ContentPlaceHolder server controls.
Master page inherits from the MasterPage class.
ContentPlaceHolder is an important control that is related with master
page.
ContentPlaceHolder control is available only on master page. You
can use more than one ContentPlaceHolder control in master page.
ContentPlaceHolder control is used on master page to customize or
add some controls on the content page as follows:
4. Validation controls
ASP.NET Validation
In this chapter, we will discuss about the data validation in the Web Forms. To perform
validation, ASP.NET provides controls that automatically check user input and require no
code. We can also create custom validation for our application.
Validator Description
We can use comparison operators like: less than, equal to, greater than etc.
CompareValidator Properties
Property Description
It allows us to check whether the user input is between a specified upper and lower
boundary. This range can be numbers, alphabetic characters and dates.
RangeValidator Properties
Property Description
It allows us to check and validate predictable sequences of characters like: e-mail address,
telephone number etc.
The ValidationExpression property is used to specify the regular expression, this
expression is used to validate input control.
RegularExpression Properties
Property Description
It is used to mandate form control required and restrict the user to provide data.
Note: It removes extra spaces from the beginning and end of the input value before
validation is performed.
RequiredFieldValidator Properties
Property Description
ErrorMessage It is used to set error message that display when validation fails.
It helps to identify requests from the same browser during a time period
(session). It is used to store value for the particular time session. By default,
ASP.NET session state is enabled for all ASP.NET applications.
if (password.Text=="qwe123")
{
// Storing email to Session variable
Session["email"] = email.Text;
}
// Checking Session variable is not empty
if (Session["email"] != null)
{
// Displaying stored email
Label3.Text = "This email is stored to the session.";
Label4.Text = Session["email"].ToString();
}
• ASP.NET Cookie
ASP.NET Cookie is a small bit of text that is used to store user-specific
information. This information can be read by the web application whenever
user visits the site.
When a user requests for a web page, web server sends not just a page, but
also a cookie containing the date and time. This cookie stores in a folder on
the user's hard disk.
When the user requests for the web page again, browser looks on the hard
drive for the cookie associated with the web page. Browser stores separate
cookie for each different sites user visited.
Note: The Cookie is limited to small size and can be used to store only
4 KB (4096 Bytes) text.
o Cookies collection
o HttpCookie
HttpCookie Example
View State is the method to preserve the Value of the Page and Controls
between round trips. It is a Page-Level State Management technique. View
State is turned on by default and normally serializes the data in every
control on the page regardless of whether it is actually used during a post-
back.
Features Of View State
You can enable and disable View State for a single control as well as at the
page level also. To turn off View State for a single control, set the
EnableViewState property of that control to false.
TextBox1.EnableViewState=false;
To turn off the View State for an entire page, we need to
set EnableViewState to false of the page directive as shown below:
Request.QueryString(variable)[(index).count].
In the second web form we take the one lable for displaying the
values of the first page. For receiving the value from the first page we
write the following code on the page_load of the second page.
This method many browsers supports only 255 character length of url in
query string. The value passed will be visible so some time it cause
security issue.
For send information to other page Response.Redirect() method used and
for retrieve information from url use Request.QueryString() method used.
Query String method we can sent value to only desired page, and value
will be temporarily. So Query string increase the overall performance of
web application.
Points to remember:
?:- Seperator
&:- to seperate two different fileds
----
%, + is used for space management(between the values)
Creating QueryString:-
Responce.Redirect():-
.Redirect("default1.aspx?firstName=Riyaj");
.Redirect("default1.aspx?firstName="+txtUname.text);
.Redirect("default1.aspx?firstName=Riyaj shahu
college&lname=Shaikh");
.Redirect("default1.aspx?firstName="+txtUname.text+"&lname="+txtlname
.text);
string s1 = Request.QueryString["firstName"].toString();
string s2 = Request.QueryString["lname"].toString();
View state can only be visible from Session state value availability is
a single page and not multiple across all pages available in a
pages. user session.
6. Data Controls
➢ The Grid View : Automatically Generating Columns, Defining
Columns-
Instead of letting the Grid View control automatically generate the column
fields, you can manually define the column fields by setting the Auto
Generate Columns property to false and then creating a
custom Columns collection. In addition to bound column fields, you can
also display a button column field, a check box column field, a command
field, a hyperlink column field, an image field, or a column field based on
your own custom-defined template.
Code:-
ASPX Coding –
ASPX.CS coding
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
SqlConnection conn;
SqlCommand cmd;
SqlDataAdapter da
; DataTable dt;
conn = new
SqlConnection(@"DataSource=DESKTOP24CMRE4\SQLEXPRESS;
conn.Close();
Response.Write("Redocrd Saved....");
FillDataGrid();
conn.Open();
dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
conn.Close();
if (dt != null)
GridView1.DataSource = sortData;
GridView1.DataBind();
string newSortType;
if (sortAcsDec == SortDirection.Ascending)
{
newSortType = "ASC";
else
newSortType = "DESC";
return newSortType;
a. What is ADO.NET?
All the ADO.NET classes are located into System.Data.dll and integrated
with XML classes located into System.Xml.dll.
ADO.NET has two main components that are used for accessing and
manipulating data are the .NET Framework data provider and the DataSet.
b. SqlConnection , SqlCommand
Code :
1. using System;
2. using System.Data;
3. using System.Data.SqlClient;
4.
5. namespace sqlconnectionANDsqlcommand
6. {
7. public partial class Form1 : Form
8. {
9. public Form1()
10. {
11. InitializeComponent();
12. }
13. private void btnclick_Click(object sender,
EventArgs e)
14. {
15. SqlConnection conn = new SqlConnection(
"Database=student;Server=.;user=sa;password=aaaaaaa"
);
16. conn.Open(); // Open the connection
17. // body
18. // body
19. conn.Close(); // Close the connection
20. }
21. }
22. }
SqlCommand Class
SqlCommand Properties
SqlCommand Method
1. using System;
2. using System.Collections.Generic;
3. using System.ComponentModel;
4. using System.Data;
5. using System.Drawing;
6. using System.Linq;
7. using System.Text;
8. using System.Windows.Forms;
9. using System.Data.SqlClient;
10.
11. namespace sqlconnectionANDsqlcommand
12. {
13. public partial class Form1 : Form
14. {
15. public Form1()
16. {
17. InitializeComponent();
18. }
19. private void Form1_Load(object sender, Even
tArgs e)
20. {
21. }
22.
23. private void btnclick_Click(object sender,
EventArgs e)
24. {
25. // Creating instance of SqlConnection
26. SqlConnection conn = new SqlConnection(
"Database=student;Server=.;user=sa;password=aaaaaaa"
);
27. conn.Open();// open the database connec
tion
28. SqlCommand cmd = new SqlCommand();// Cr
eating instance of SqlCommand
29. cmd.Connection = conn; // set the conne
ction to instance of SqlCommand
30. cmd.CommandText = "insert into student_
detail values (" + txtrollno.Text + ",'" + txtname.T
ext + "','" + txtcourse.Text + "')"; // set
31. //the sql command ( Statement )
32. cmd.ExecuteNonQuery();
33. MessageBox.Show("Record Saved"); // sho
wing messagebox for confirmation message for user
34. conn.Close();// Close the connection
35. }
36.
37. }
38. }
DataSet
DataAdapter
• Lets you close the connection as soon it's done loading data,
and may even close it for you automatically
• All of the results are available in memory
• You can iterate over it as many times as you need, or even
look up a specific record by index
• Has some built-in faculties for updating back to the database.
Simple Application using ADO.NET
<td>
<asp:Label ID="Label5" runat="server"></asp:Label></t>
</tr>
<tr>
<td class="auto-style3">
<asp:Label ID="Label3" runat="server"></asp:Label></t>
<td>
<asp:Label ID="Label6" runat="server"></asp:Label></t>
</tr>
<tr>
<td class="auto-style3">
<asp:Label ID="Label4" runat="server"></asp:Label></t>
<td>
<asp:Label ID="Label7" runat="server"></asp:Label></t>
</tr>
</table>
</body>
</html>
CodeBehind
// WebFormAdoNet.aspx.cs
using System;
using System.Data.SqlClient;
namespace ado.netWebFormExample
{
public partial class WebFormAdoNet : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void ButtonId_Click(object sender, EventArgs e)
{
SqlConnection con = null;
try
{
// Creating Connection
con = new SqlConnection("data source=.; database=student; integrat
ed security=SSPI");
// Writing insert query
string query = "insert into student(name,email,contact)values('"+Use
rnameId.Text+ "',
'" + EmailId.Text + "','" + ContactId.Text + "')";
SqlCommand sc = new SqlCommand(query,con);
// Opening connection
con.Open();
// Executing query
int status = sc.ExecuteNonQuery();
Label1.Text = "Your record has been saved with the followin
g details!";
// ----------------------- Retrieving Data ------------------ //
SqlCommand cm = new SqlCommand("select top 1 * from
student", con);
// Executing the SQL query
SqlDataReader sdr = cm.ExecuteReader();
sdr.Read();
Label2.Text = "User Name"; Label5.Text = sdr["name"].To
String();
Label3.Text = "Email ID"; Label6.Text = sdr["email"].ToStr
ing();
Label4.Text = "Contact"; Label7.Text = sdr["contact"].To
String();
}
catch (Exception ex)
{
Console.WriteLine("OOPs, something went wrong." + ex);
}
// Closing the connection
finally
{
con.Close();
}
}
}
}