WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

ASP.NET Web Forms - Server Controls


Server controls are labels that are comprehended by the server.


Limitations in Classic ASP

The posting beneath was replicated from the past chapter:

<html>
<body bgcolor="yellow">
<center>
<h2>Hello welookups!</h2>
<p><%Response.Write(now())%></p>
</center>
</body>
</html>

The code above represents a restriction in Classic ASP: The code square has to be set where you need the yield to show up.

With Classic ASP it is difficult to isolate executable code from the HTML itself. This makes the page hard to peruse, and hard to maintain.


ASP.NET - Server Controls

ASP.NET has illuminated the "spaghetti-code" issue portrayed above with server controls.

Server controls are labels that are comprehended by the server.

There are three sorts of server controls:

  • HTML Server Controls - Traditional HTML tags
  • Web Server Controls - New ASP.NET labels
  • Validation Server Controls - For info approval

ASP.NET - HTML Server Controls

HTML server controls are HTML labels comprehended by the server.

HTML components in ASP.NET documents are, as a matter of course, treated as content. To make these components programmable, include a runat="server" credit to the HTML component. This characteristic demonstrates that the component ought to be treated as a server control. The id ascribe is added to distinguish the server control. The id reference can be utilized to control the server control at run time.

Note: All HTML server controls must be inside a <form> tag with the runat="server" characteristic. The runat="server" trait demonstrates that the structure ought to be handled on the server. It additionally shows that the encased controls can be gotten to by server scripts.

In the accompanying model we proclaim a HtmlAnchor server control in an .aspx record. At that point we control the HRef quality of the HtmlAnchor control in an occasion handler (an occasion handler is a subroutine that executes code for a given occasion). The Page_Load occasion is one of numerous occasions that ASP.NET understands:

<script runat="server">
Sub Page_Load
link1.HRef="http://www.welookups.com"
End Sub
</script>

<html>
<body>

<form runat="server">
<a id="link1" runat="server">Visit welookups!</a>
</form>

</body>
</html>

The executable code itself has been moved outside the HTML.


ASP.NET - Web Server Controls

Web server controls are exceptional ASP.NET labels comprehended by the server.

Like HTML server controls, Web server controls are additionally made on the server what's more, they require a runat="server" credit to work. Notwithstanding, Web server controls don't really guide to any current HTML components and they may speak to increasingly complex elements.

The sentence structure for making a Web server control is:

<asp:control_name id="some_id" runat="server"/>

In the accompanying model we proclaim a Button server control in an .aspx record. At that point we make an occasion handler for the Click occasion which changes the content on the button:

<script runat="server">
Sub submit(Source As Object, e As EventArgs)
button1.Text="You clicked me!"
End Sub
</script>

<html>
<body>

<form runat="server">
<asp:Button id="button1" Text="Click me!"
runat="server" OnClick="submit"/>
</form>

</body>
</html>

ASP.NET - Validation Server Controls

Validation server controls are utilized to approve client input. In the event that the client input does not pass approval, it will show a blunder message to the user.

Each approval control plays out a particular sort of approval (like approving against a particular esteem or a scope of values).

By default, page approval is performed when a Button, ImageButton, or LinkButton control is clicked. You can counteract approval when a catch control is clicked by setting the CausesValidation property to false.

The sentence structure for making a Validation server control is:

<asp:control_name id="some_id" runat="server"/>

In the accompanying model we announce one TextBox control, one Button control, furthermore, one RangeValidator control in an .aspx document. On the off chance that approval falls flat, the content "The esteem must be from 1 to 100!" will be shown in the RangeValidator control:

Example

<html>
<body>

<form runat="server">
<p>Enter a number from 1 to 100:
<asp:TextBox id="tbox1" runat="server"/>
<br/><br/>
<asp:Button Text="Submit" runat="server"/>
</p>

<p>
<asp:RangeValidator
ControlToValidate="tbox1"
MinimumValue="1"
MaximumValue="100"
Type="Integer"
Text="The esteem must be from 1 to 100!"
runat="server"/>
</p>
</form>

</body>
</html>

Show model »