Escolar Documentos
Profissional Documentos
Cultura Documentos
Client-side technologies
Server-side technologies
Button code
...
Button
List
List code
...
Text
Text code
...
Browser
ASP.NET
Event handlers
Initialize
Restore Control State
Load Page
Control Events
1. Change Events
Page_Init
Page_Load
Textbox1_Changed
2. Action Events
Button1_Click
Page_Unload
Code at Button or
Page_load event
Label1.Text = TextBox1.Text;
Label1.Text =Welcome in
asp.net page;
TextBox control is
used to enter data
event
Label1.Text = TextBox1.Text;
TextBox1.Text =Welcome in
asp.net page;
TextBox1.Text
=Welcome in asp.net
page;
Coding At Property
<asp:HyperLink
ID="HyperLink1"
runat="server"
NavigateUrl="http://
www.google.co.in">
Google</asp:HyperLi
nk>
Coding At DropDownSeleted
Index Event
If(DropDownList1.SeletedIndex
==0)
{
Label1.Text=Welcome;
Else
Statement1;
}
Code is same as
dropdownlist
Coding At
listboxSeleted Index
Event
If(Listbox1.SeletedInd
ex==0)
{
Label1.Text=Welcome
;
Else
Statement1;
}
Coding At Checkbox
CheckBox1_CheckedChange
d 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.SeletedInde
x==0)
{
Label1.Text=Welcome;
Else
Statement1;
}
Code at Radiobutton
Event
RadioButton web server control is
used for select or deselect an item
when all these RadioButtons will be in
a same group.
If(Radiobutton1.Checked)
{
Label1.Text=Male;
}
Must set property
autopostback true
And 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
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
<div>
<asp:ImageMap ID="ImageMap1"
runat="server"
ImageUrl="~/images/dog-cat.jpg"
HotSpotMode="PostBack" >
<asp:CircleHotSpot
AlternateText="Dog" Radius="50"
X="120" Y="42"
PostBackValue="Dog"/>
<asp:CircleHotSpot
AlternateText="Cat" Radius="50"
X="70" Y="140"
PostBackValue="Cat"/>
</asp:ImageMap>
</div>
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.
FileUpload1.SaveAs(Server.MapPath
(path));
Image1.ImageUrl = path;
Code for upload the file in drive
if (FileUpload1.HasFile)
{
FileUpload1.SaveAs(@"D:\temp\"
+FileUpload1.FileName);
Label1.Text = "File Uploaded: " +
FileUpload1.FileName +"
"+System.DateTime.Now.ToLongDat
eString();
} else
{
Label1.Text = "No File
Uploaded.";
}
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
</asp:MultiView>
Coding at page_load
if (!Page.IsPostBack)
{
MultiView1.ActiveViewIndex = 0;
}
Coding at button
MultiView1.ActiveViewIndex += 1;
YoucanusethePanelco
ntrolasacontainerforot
hercontrols.Thisispartic
ularlyusefulwhenyouar
ecreatingcontentprogra
mmaticallyandyouneed
awaytoinserttheconte
ntintothepage.
<asp:PanelID="Panel1"runat="se
rver"ScrollBars="Auto"Width="
450"Height="200"Wrap="true"
BackColor="Aqua"Direction="Le
ftToRight">
<p> Write here Text</p>
</panel>
<asp:PlaceHolder
ID="PlaceHolder1"
runat="server"></asp:PlaceHo
lder>
Coding at load event
GridView Control
Details View
Datalist View
Form view
List view
Repeater Control
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. .
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate >
<h2>Employee Detail</h2>
<table border ="2" bgcolor="silver" bordercolor="red">
<tr>
<th>Emp Name is :</th>
<th>Emp no. is :</th>
<th>Salary is :</th>
</tr>
</HeaderTemplate>
<ItemTemplate >
<tr><td bgcolor="">
<%#Eval ("ename") %></td>
<td><%#Eval ("empno") %> </td>
<td><%#Eval ("sal") %>
</td>
</tr>
</ItemTemplate>
<FooterTemplate >
</table><h2>By Abc...</h2></FooterTemplate>
</asp:Repeater>
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 are
RequiredFieldValidator
CompareValidator
RegularExpressionValidator
RangeValidator
CustomValidator
ValidationSummary
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.
With the help of this control, you can compare the value of one control with the value of
another control.
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
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 on
Custom 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.
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
DataTable dt = new DataTable();
SqlConnection objcon = new
SqlConnection(System.Configuration.ConfigurationManager.ConnectionS
trings["conn"].ToString());
SqlDataAdapter objda = new SqlDataAdapter("select * from emp",
objcon);
objda.Fill(dt);
if (dt.Rows.Count > 0)
{
GridView1.DataSource = dt;
GridView1.DataBind();
}
string q;
SqlCommand cmd;
SqlConnection con;
q = "insert into student(roll,name,marks)
values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "')";
con = new SqlConnection("data source=.\\SQLEXPRESS;initial
catalog=dbemp;integrated security=true");
con.Open();
cmd = new SqlCommand(q, con);
cmd.ExecuteNonQuery();
//Label1.Text = "Save data Successfully...";
Response.Write("<script language='javascript'>alert('Saved data in
table:');</script>");
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())
protected void TextBox1_TextChanged(object sender,
{
EventArgs e)
Label3.Visible = true;
{
Label4.Visible = true;
Label2.Visible = false;
TextBox2.Visible = true;
Label3.Visible = false;
TextBox3.Visible = true;
Label4.Visible = false;
TextBox2.Text = dr[0].ToString();
TextBox2.Visible = false;
TextBox3.Visible = false;
TextBox3.Text = dr[1].ToString();
}
}
else
{
Label2.Visible = true;
Label2.Text = "No Such Record Exists";
}
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
{
Coding at Textbox Changed Event
Label2.Visible = true;
Label2.Visible = false;
GridView1.Visible = false;
GridView1.Visible = false;
Label2.Text = "No Such Record Exists";
}
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(); }
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();
cn.Open();
ds = new DataSet();
ad = new SqlDataAdapter(q, cn);
ad.Fill(ds, "police");
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();