ASP .Net Unit 2 (HTML Server Controls & Rich Control)
ASP .Net Unit 2 (HTML Server Controls & Rich Control)
ASP.NET Controls
HtmlInputCheckbox Control
HtmlForm Control
HtmlInputFile Control
HtmlGeneric Control HtmlInputRadioButton Control
HtmlInputText Control
Introduction of HTML Server Controls
The HTML elements are considered as text in ASP.NET file. They cannot be referred as server side code. These
controls can be treated as server control by adding runat=”server” attribute. The id attribute in the element can be
added as reference to the control. All the HTML server controls are written in the <form> tag.
o The HTML server controls follow the HTML centric object model. It is similar to HTML
o The controls can interact with the Client side scripting
o The migration of the code can be made easy by adding runat=”server attribute
o The abstraction of the HTML tag is similar to the HTML server control
o The controls do not possess any mechanism for identifying capabilities of the client browser
Control List
1) HtmlAnchor control:
The <a> Html element allows the user to create a hyperlink. User can navigate to the page defined in the link. The
Target property is used to display the Web page. The values can be _blank, _self, _parent, _top. The properties are
mentioned below:
Property Description
Attributes It returns the attribute name and value pairs of the element
InnerText It sets or returns the text between the opening and closing tags of the
element.
InnerHtml It sets or returns the content between the opening and closing tags of the
element
OnServerClick The function to be executed when the link is clicked by the user
1
<html xmlns=”http://www.w3.org/1999/xhtml” >
2
<head runat=”server”>
3
<title></title>
4
</head>
5 <body>
7 <div>
5 a1.Title=”Welcome to Gmail”;
a1.Target=”_blank”;
6
7
a2.HRef=”http://www.yahoo.com”;
8
a2.Title=”Welcome to yahoo”;
9
a2.Target=”_blank”;
10
}
11
}
12
13
Output:
2) HtmlButton control:
The HtmlButton control is defined using the <button> element. The properties are as mentioned below:
Property Description
Attributes It returns the attribute name and value pairs of the element
InnerHtml It sets or returns the content between the opening and closing tags of the element
InnerText It sets or returns the text between the opening and closing tags of the element
OnServerClick The function to be executed when the button is clicked by the user
Style It is used to set or return the CSS properties applied to the control
<head runat=”server”>
2
<title></title>
3
<script language=”javascript” type=”text/javascript”>
4
//<![ CDATA [
5
function btn1_onclick ()
6
{
7 lbl1.innerHTML =”The button clicked is save”;
8 }
9 funciton btn2_onclick()
10 {
}
12
//]]>
13
</script>
14
</head>
15
<body>
16
<form id=”form1” runat=”server”>
17 <button id=”btn1” runat=”server” onclick =”return btn1_onclick()” >Save
Value </button>
18
<br/>
19
<button id=”btn2” runat=”server” onclick=”return btn2_onclick()” >Display
20 Value</button>
21 <br/>
26
Output:
3) HtmlForm control:
The Htmlform control is used to control a <form> element. All the
Html server controls are written within the Html form control. User can add only one form control for a web page. The
POST method is the default method of the Html form control. The properties are mentioned below:
Property Description
Action It contains a URL defining where to send the data in the form submitted
Attributes It returns the attribute name and value pairs of the element
InnerHtml It sets or returns the content between the opening and closing tags of the element
InnerText It sets or returns the text between the opening and closing tags of the element
Method The form posts the data to the server.
Style It is used to set or return the CSS properties applied to the control
<html xmlns=”http://www.w3.org/1999/xhtml”>
1
<head runat=”server”>
2
<title></title>
3 <script runat=”server”>
5 {
7 }
</script>
8
</head>
9
<body>
10
<form id=”form1” runat=”server”>
11
<div>
12
Name: <input is=”name” runat=”server” type=”text” /><br/>
13 <input id=”button1” runat=”server” type=”submit” value=”submit” onserverclick=”
style=”height:26px” />
14
<label id=”lbl1” runat=”server”></label>
15
</div>
16 </form>
17 </body>
</html>
18
19
20
Output:
4) HtmlGeneric control:
The HtmlGeneric control is used to control elements other than Html server control. The <body>, <div>, <span> and
<p> are used as Html generic control. The properties are as mentioned below:
Property Description
Attributes It returns the attribute name and value pairs of the element
InnerHtml It sets or returns the content between the opening and closing tags of the element
InnerText It sets or returns the text between the opening and closing tags of the element
Style It is used to set or return the CSS properties applied to the control
5) HtmlImage control:
The HtmlImage control is used to control <img> element. The <img> element is used to display an image. The
properties are as follows:
Property Description
Align It is used to align images depending on the surrounding elements. The values that
can be assigned are top, middle, bottom, left, right.
Attributes It returns the attribute name and value pairs of the element
Style It is used to set or return the CSS properties applied to the control
1 <html xmlns=”http:www.w3.org/1999/xhtml”>
<head runat=”server”>
2
<title></title>
3
</head>
4
<body>
5
<form id=”form1” runat=”server”>
6
<div>
7 <img id=”img1” runat=”server” />
8 </div>
9 </form>
</body>
10
</html>
11
12
1
public partial class _Default : System.Web.UI.Page
2
{
3 protected void Page_Load( object sender, EventArgs e)
4 {
5 img1.Src =”Image\\Desert.jpg”;
6 img1.Alt = “Desert”;
7 img1.Height=90;
img1.Width=100;
8
}
9
}
10
Output:
6) HtmlInputButton control:
The HtmlInputButton control is used to control <input type=”button”>, <input type=”reset”> and <input
type=”submit”> elements. The properties are as mentioned below:
Property Description
Attributes It returns the attribute name and value pairs of the element
Style It is used to set or return the CSS properties applied to the control
2 <head runat=”server”>
3 <title></title>
<script runat=”server”>
4
void submit( object sender, EventArgs e)
5
{
6
p1.InnerHtml =”Course entered is:”+t1.Value;
7
}
8
</script>
9 </head>
10 <body>
<br/>
14
<input type=”submit” runat=”server” value=”submit”
15 onserverclick=”submit” /><br/>
17 </div>
18 </form>
</body>
19
</html>
20
21
Output:
7) HtmlInputCheckBox control:
The HtmlInputCheckBox control is used to control as <input type=”checkbox”> element. The properties are as
mentioned below:
Property Description
Attributes It returns the attribute name and value pairs of the element
Style It is used to set or return the CSS properties applied to the control
Output:
8) HtmlInputFile control: The HtmlInputFile control is used to control an <input type=”file”> element. The
properties are as mentioned below:
Property Description
Attributes It returns the attribute name and value pairs of the element
Style It is used to set or return the CSS properties applied to the control
Property Description
Attributes It returns the attribute name and value pairs of the element
runat C
Style It is used to set or return the CSS properties applied to the control
Output:
10) HtmlInputText control: The HtmlInputText control is used to control <input type=”text”> and <input
type=”password”> elements. The properties are stated below:
Property Description
Attributes It returns the attribute name and value pairs of the element