UNIT 2 - AspNet
UNIT 2 - AspNet
net and c#
UNIT 2
Form Validation
Validation is important part of any web application. User's input must always be validated
before sending across different layers of the application.
1. Client Side
2. Serve Side
Client side validation is good but we have to be dependent on browser and scripting
language support.
3ibca 501 Web Programming with Asp.Net and C# Asst. Prof. Pradnya J. Nehete
WEB Programming with asp.net and c#
Client side validation is considered convenient for users as they get instant feedback. The
main advantage is that it prevents a page from being postback to the server until the client
validation is executed successfully.
For developer point of view serve side is preferable because it will not fail, it is not
dependent on browser and scripting language.
You can use ASP.NET validation, which will ensure client, and server validation. It work on
both end; first it will work on client validation and than on server validation. At any cost
server validation will work always whether client validation is executed or not. So you have
a safety of validation check.
For client script .NET used JavaScript. WebUIValidation.js file is used for client validation
by .NET
We can use comparison operators like: less than, equal to, greater than etc.
CompareValidator Properties
3ibca 501 Web Programming with Asp.Net and C# Asst. Prof. Pradnya J. Nehete
WEB Programming with asp.net and c#
Example
Here, in the following example, we are validating user input by using CompareValidator
controller. Source code of the example is given below.
// compare_validator_demo.aspx
3ibca 501 Web Programming with Asp.Net and C# Asst. Prof. Pradnya J. Nehete
WEB Programming with asp.net and c#
7. <style type="text/css">
8. .auto-style1 {
9. width: 100%;
10. }
11. .auto-style2 {
12. height: 26px;
13. }
14. .auto-style3 {
15. height: 26px;
16. width: 93px;
17. }
18. .auto-style4 {
19. width: 93px;
20. }
21. </style>
22. </head>
23. <body>
24. <form id="form1" runat="server">
25. <table class="auto-style1">
26. <tr>
27. <td class="auto-style3">
28. First value</td>
29. <td class="auto-style2">
30. <asp:TextBox ID="firstval" runat="server" required="true"></asp:TextBox>
31. </td>
32. </tr>
33. <tr>
34. <td class="auto-style4">
35. Second value</td>
36. <td>
37. <asp:TextBox ID="secondval" runat="server"></asp:TextBox>
38. It should be greater than first value</td>
39. </tr>
40. <tr>
41. <td class="auto-style4"></td>
42. <td>
43. <asp:Button ID="Button1" runat="server" Text="save"/>
44. </td>
45. </tr>
46. </table>
47. < asp:CompareValidator ID="CompareValidator1" runat="server" ControlToCompare="s
econdval"
48. ControlToValidate="firstval" Display="Dynamic" ErrorMessage="Enter valid value" ForeCo
lor="Red"
49. Operator="LessThan" Type="Integer"></asp:CompareValidator>
50. </form>
3ibca 501 Web Programming with Asp.Net and C# Asst. Prof. Pradnya J. Nehete
WEB Programming with asp.net and c#
51. </body>
52. </html>
Output:
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.
3ibca 501 Web Programming with Asp.Net and C# Asst. Prof. Pradnya J. Nehete
WEB Programming with asp.net and c#
RangeValidator Properties
Property Description
3ibca 501 Web Programming with Asp.Net and C# Asst. Prof. Pradnya J. Nehete
WEB Programming with asp.net and c#
Example
In the following example, we are using RangeValidator to validate user input in specified
range.
// RangeValidator.aspx
3ibca 501 Web Programming with Asp.Net and C# Asst. Prof. Pradnya J. Nehete
WEB Programming with asp.net and c#
Output:
3ibca 501 Web Programming with Asp.Net and C# Asst. Prof. Pradnya J. Nehete
WEB Programming with asp.net and c#
It allows us to check and validate predictable sequences of characters like: e-mail address,
telephone number etc.
RegularExpression Properties
Property Description
3ibca 501 Web Programming with Asp.Net and C# Asst. Prof. Pradnya J. Nehete
WEB Programming with asp.net and c#
Example
// RegularExpressionDemo.aspx
3ibca 501 Web Programming with Asp.Net and C# Asst. Prof. Pradnya J. Nehete
WEB Programming with asp.net and c#
14. <td>
15. <asp:TextBox ID="username" runat="server"></asp:TextBox>
16. <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"Con
trolToValidate="username"
17. ErrorMessage="Please enter valid email" ForeColor="Red"ValidationExpression="\w+([-
+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">
18. </asp:RegularExpressionValidator>
19. </td>
20. </tr>
21. <tr>
22. <td class="auto-style2"></td>
23. <td>
24. <br/>
25. <asp:Button ID="Button1" runat="server" Text="Save"/>
26. </td>
27. </tr>
28. </table>
29. </div>
30. </form>
31. </body>
32. </html>
Output:
It will validate email format as we specified in regular expression. If validation fails, it throws
an error message.
3ibca 501 Web Programming with Asp.Net and C# Asst. Prof. Pradnya J. Nehete
WEB Programming with asp.net and c#
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
3ibca 501 Web Programming with Asp.Net and C# Asst. Prof. Pradnya J. Nehete
WEB Programming with asp.net and c#
ErrorMessage It is used to set error message that display when validation fails.
Example
// RequiredFieldValidator.aspx
3ibca 501 Web Programming with Asp.Net and C# Asst. Prof. Pradnya J. Nehete
WEB Programming with asp.net and c#
18. <div>
19. </div>
20. <table class="auto-style1">
21. <tr>
22. <td class="auto-style2">User Name</td>
23. <td>
24. <asp:TextBox ID="username" runat="server"></asp:TextBox>
25. <asp:RequiredFieldValidatorIDasp:RequiredFieldValidatorID="user" runat="server" Con
trolToValidate="username"
26. ErrorMessage="Please enter a user name" ForeColor="Red"></asp:RequiredFieldValidat
or>
27. </td>
28. </tr>
29. <tr>
30. <td class="auto-style2">Password</td>
31. <td>
32. <asp:TextBox ID="password" runat="server"></asp:TextBox>
33. <asp:RequiredFieldValidator ID="pass" runat="server" ControlToValidate="password" E
rrorMessage="Please enter a password"
34. ForeColor="Red"></asp:RequiredFieldValidator>
35. </td>
36. </tr>
37. <tr>
38. <td class="auto-style2"> </td>
39. <td>
40. <br/>
41. <asp:Button ID="Button1" runat="server" Text="login"/>
42. </td>
43. </tr>
44. </table>
45. </form>
46. </body>
47. </html>
Output:
3ibca 501 Web Programming with Asp.Net and C# Asst. Prof. Pradnya J. Nehete
WEB Programming with asp.net and c#
We can set DisplayMode property to display error messages as a list, bullet list or single
paragraph.
ValidationSummary Properties
3ibca 501 Web Programming with Asp.Net and C# Asst. Prof. Pradnya J. Nehete
WEB Programming with asp.net and c#
Property Description
Example
The following example explains how to use ValidationSummery control in the application.
// ValidationSummeryDemo.aspx
3ibca 501 Web Programming with Asp.Net and C# Asst. Prof. Pradnya J. Nehete
WEB Programming with asp.net and c#
2. Inherits="asp.netexample.ValidationSummeryDemo" %>
3. <!DOCTYPE html>
4. <html xmlns="http://www.w3.org/1999/xhtml">
5. <head runat="server">
6. <title></title>
7. </head>
8. <body>
9. <form id="form1" runat="server">
10. <div>
11. </div>
12. <table class="auto-style1">
13. <tr>
14. <td class="auto-style2">User Name</td>
15. <td>
16. <asp:TextBox ID="username" runat="server"></asp:TextBox>
17. <asp:RequiredFieldValidator ID="user" runat="server" ControlToValidate="username"
18. ErrorMessage="Please enter a user name" ForeColor="Red">*</asp:RequiredFieldValida
tor>
19. </td>
20. </tr>
21. <tr>
22. <td class="auto-style2">Password</td>
23. <td>
24. <asp:TextBox ID="password" runat="server"></asp:TextBox>
25. <asp:RequiredFieldValidator ID="pass" runat="server" ControlToValidate="password"
26. ErrorMessage="Please enter a password" ForeColor="Red">*</asp:RequiredFieldValida
tor>
27. </td>
28. </tr>
29. <tr>
30. <td class="auto-style2">
31. <br/>
32. <asp:Button ID="Button1" runat="server"Text="login"/>
33. </td>
34. <td>
35. <asp:ValidationSummary ID="ValidationSummary1" runat="server" ForeColor="Red"/>
36. <br/>
37. </td>
38. </tr>
39. </table>
40. </form>
41. </body>
42. </html>
Output:
3ibca 501 Web Programming with Asp.Net and C# Asst. Prof. Pradnya J. Nehete
WEB Programming with asp.net and c#
We can also set Selected Date property that shows specified date in the calendar.
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.
3ibca 501 Web Programming with Asp.Net and C# Asst. Prof. Pradnya J. Nehete
WEB Programming with asp.net and c#
This control has its own properties that are tabled below.
Property Description
NextMonth Text It is used to set text for the next month button.
3ibca 501 Web Programming with Asp.Net and C# Asst. Prof. Pradnya J. Nehete
WEB Programming with asp.net and c#
Example
3ibca 501 Web Programming with Asp.Net and C# Asst. Prof. Pradnya J. Nehete
WEB Programming with asp.net and c#
In this example, we are implementing calendar and displaying user selected date to the web
page.
// WebControls.aspx
// WebControls.aspx.cs
1. using System;
2. using System.Collections.Generic;
3. using System.Linq;
4. using System.Web;
5. using System.Web.UI;
6. using System.Web.UI.WebControls;
7. namespace WebFormsControlls
3ibca 501 Web Programming with Asp.Net and C# Asst. Prof. Pradnya J. Nehete
WEB Programming with asp.net and c#
8. {
9. public partial class WebControls : System.Web.UI.Page
10. {
11. public void Calendar1_SelectionChanged(object sender, EventArgs e)
12. {
13. ShowDate.Text = "You Selected: "+Calendar1.SelectedDate.ToString("D");
14. }
15. }
16. }
Output:
It shows date selected by the user at the web page. A screenshot is attached below.
3ibca 501 Web Programming with Asp.Net and C# Asst. Prof. Pradnya J. Nehete
WEB Programming with asp.net and c#
AdRotator Control
The AdRotator control randomly selects banner graphics from a list, which is specified in
an external XML schedule file. This external XML schedule file is called the advertisement
file.
3ibca 501 Web Programming with Asp.Net and C# Asst. Prof. Pradnya J. Nehete
WEB Programming with asp.net and c#
The AdRotator control allows you to specify the advertisement file and the type of window
that the link should follow in the AdvertisementFile and the Target property respectively.
The basic syntax of adding an AdRotator is as follows:
The advertisement file is an XML file, which contains the information about the
advertisements to be displayed.
Extensible Markup Language (XML) is a W3C standard for text document markup. It is a
text-based markup language that enables you to store data in a structured format by using
meaningful tags. The term 'extensible' implies that you can extend your ability to describe
a document by defining meaningful tags for the application.
XML is not a language in itself, like HTML, but a set of rules for creating new markup
languages. It is a meta-markup language. It allows developers to create custom tag sets for
special uses. It structures, stores, and transports the information.
Following is an example of XML file:
<BOOK>
<NAME> Learn XML </NAME>
<AUTHOR> Samuel Peterson </AUTHOR>
<PUBLISHER> NSS Publications </PUBLISHER>
<PRICE> $30.00</PRICE>
</BOOK>
Like all XML files, the advertisement file needs to be a structured text file with well-defined
tags delineating the data. There are the following standard XML elements that are
commonly used in the advertisement file:
Element Description
3ibca 501 Web Programming with Asp.Net and C# Asst. Prof. Pradnya J. Nehete
WEB Programming with asp.net and c#
NavigateUrl The link that will be followed when the user clicks the ad.
AlternateText The text that will be displayed instead of the picture if it cannot be
displayed.
Apart from these tags, customs tags with custom attributes could also be included. The
following code illustrates an advertisement file ads.xml:
<Advertisements>
<Ad>
<ImageUrl>rose1.jpg</ImageUrl>
<NavigateUrl>http://www.1800flowers.com</NavigateUrl>
<AlternateText>
Order flowers, roses, gifts and more
</AlternateText>
<Impressions>20</Impressions>
<Keyword>flowers</Keyword>
</Ad>
<Ad>
<ImageUrl>rose2.jpg</ImageUrl>
<NavigateUrl>http://www.babybouquets.com.au</NavigateUrl>
<AlternateText>Order roses and flowers</AlternateText>
<Impressions>20</Impressions>
<Keyword>gifts</Keyword>
</Ad>
<Ad>
<ImageUrl>rose3.jpg</ImageUrl>
<NavigateUrl>http://www.flowers2moscow.com</NavigateUrl>
3ibca 501 Web Programming with Asp.Net and C# Asst. Prof. Pradnya J. Nehete
WEB Programming with asp.net and c#
<Ad>
<ImageUrl>rose4.jpg</ImageUrl>
<NavigateUrl>http://www.edibleblooms.com</NavigateUrl>
<AlternateText>Edible Blooms</AlternateText>
<Impressions>20</Impressions>
<Keyword>gifts</Keyword>
</Ad>
</Advertisements>
The AdRotator class is derived from the WebControl class and inherits its properties. Apart
from those, the AdRotator class has the following properties:
Properties Description
AlternateTextFeild The element name of the field where alternate text is provided. The
default value is AlternateText.
3ibca 501 Web Programming with Asp.Net and C# Asst. Prof. Pradnya J. Nehete
WEB Programming with asp.net and c#
ImageUrlField The element name of the field where the URL for the image is
provided. The default value is ImageUrl.
NavigateUrlField The element name of the field where the URL to navigate to is
provided. The default value is NavigateUrl.
Target The browser window or frame that displays the content of the page
linked.
Events Description
AdCreated It is raised once per round trip to the server after creation of the
control, but before the page is rendered
Disposed Occurs when a server control is released from memory, which is the
last stage of the server control lifecycle when an ASP.NET page is
requested
Init Occurs when the server control is initialized, which is the first step
in its lifecycle.
3ibca 501 Web Programming with Asp.Net and C# Asst. Prof. Pradnya J. Nehete
WEB Programming with asp.net and c#
Load Occurs when the server control is loaded into the Page object.
PreRender Occurs after the Control object is loaded but prior to rendering.
State Management :
State Outline
As I said in the beginning, HTTP is a stateless protocol. It just cleans up or we can say
removes all the resources/references that were serving a specific request in the past. These
resources can be:
Objects
Allocated Memory
Sessions ID's
Some URL info
and so on.
3ibca 501 Web Programming with Asp.Net and C# Asst. Prof. Pradnya J. Nehete
WEB Programming with asp.net and c#
Whenever we use Client-Side State Management, the state related information will directly
get stored on the client-side. That specific information will travel back and communicate
with every request generated by the user then afterwards provides responses after server-
side communication.
Server-Side State Management is different from Client-Side State Management but the
operations and working is somewhat the same in functionality. In Server-Side State
Management all the information is stored in the user memory. Due to this functionality
there is more secure domains at the server side in comparison to Client-Side State
Management.
3ibca 501 Web Programming with Asp.Net and C# Asst. Prof. Pradnya J. Nehete
WEB Programming with asp.net and c#
It will be a little difficult to directly evaluate what will be better for our application. We
cannot directly say that we will use client-side or server-side architecture of State
Management.
State Management techniques are based on client side and server side. Their functionality
differs depending on the change in state, so here is the hierarchy:
Client-side | Techniques
3ibca 501 Web Programming with Asp.Net and C# Asst. Prof. Pradnya J. Nehete
WEB Programming with asp.net and c#
View State
Hidden field
Cookies
Control State
Query Strings
Server-side | Technique
Session State
Application State
Now I am defining each and every technique in detail with their reference example.
View State
In general we can say it is used for storing user data in ASP.NET, sometimes in ASP.NET
applications the user wants to maintain or store their data temporarily after a post-back..
In this case VIEW STATE is the most used and preferred way of doing that.
This property is enabled by default but we can make changes depending on our
functionality, what we need to do is just change the EnableViewState value to either TRUE
for enabling it or FALSE for the opposite operation.
3ibca 501 Web Programming with Asp.Net and C# Asst. Prof. Pradnya J. Nehete
WEB Programming with asp.net and c#
6. if (ViewState["count"] != null)
7. {
8. int ViewstateVal = Convert.ToInt32(ViewState["count"]) + 1;
9. View.Text = ViewstateVal.ToString();
10. ViewState["count"]=ViewstateVal.ToString();
11. }
12. else
13. {
14. ViewState["count"] = "1";
15. }
16. }
17. }
18.
19. // Click Event
20. protected void Submit(object sender, EventArgs e)
21. {
22. View.Text=ViewState["count"].ToString();
23. }
Points to Remember
Session Management :
Session is a State Management Technique. A Session can store the value on the Server. It
can support any type of object to be stored along with our own custom objects. A session is
one of the best techniques for State Management because it stores the data as client-based,
in other words, the data is stored for every user separately and the data is secured also
because it is on the server.
3ibca 501 Web Programming with Asp.Net and C# Asst. Prof. Pradnya J. Nehete
WEB Programming with asp.net and c#
Step 2: Then Click on "New Project" -> "WEB" -> "ASP.NET Empty Web Application".
3ibca 501 Web Programming with Asp.Net and C# Asst. Prof. Pradnya J. Nehete
WEB Programming with asp.net and c#
Step 4: Now right-click on the "Add" -> "New Item" -> "Web Form" and add the name of the
web form and I had added 2 Web Form1.aspx and Web Form2.aspx.
Step 5: After adding the web form the following code is added to Web Form1.aspx.
3ibca 501 Web Programming with Asp.Net and C# Asst. Prof. Pradnya J. Nehete
WEB Programming with asp.net and c#
3ibca 501 Web Programming with Asp.Net and C# Asst. Prof. Pradnya J. Nehete
WEB Programming with asp.net and c#
Now to set the session we need to use a config file. We can set the session on one of the
following 2 types of configuration files:
1. <system.web>
2. <sessionState mode="SQLServer" sqlConnectionString="Server=DIVS\SQLEXPRESS;
Integrated Security=true"></sessionState>
3. <compilation debug="true" targetFramework="4.0" />
4. </system.web>
3ibca 501 Web Programming with Asp.Net and C# Asst. Prof. Pradnya J. Nehete
WEB Programming with asp.net and c#
Output
3ibca 501 Web Programming with Asp.Net and C# Asst. Prof. Pradnya J. Nehete
WEB Programming with asp.net and c#
Step 2: Then click on "New Project" > "Web" > "ASP.NET Empty Web Application" .
3ibca 501 Web Programming with Asp.Net and C# Asst. Prof. Pradnya J. Nehete
WEB Programming with asp.net and c#
Step 4: Now right-click on "Add" > "New Item" > "Web Form" and add the name of the web
form.
Step 5: Now add the Global.asax file. Again go to Solution Explorer and "Add" > "New Item"
> "Global Application Class".
3ibca 501 Web Programming with Asp.Net and C# Asst. Prof. Pradnya J. Nehete
WEB Programming with asp.net and c#
Step 6: Now to configure the session we need to use the web.config file as in the following:
Step 7: Now to count the number of users online we need to use the global.asax file as in
the following:
3ibca 501 Web Programming with Asp.Net and C# Asst. Prof. Pradnya J. Nehete
WEB Programming with asp.net and c#
16. Application.Lock();
17. Application["user"] = (int)Application["user"] - 1;
18. Application.UnLock();
19. }
Step 8: Now to show the online users we need to use a web form as in the following:
Output
3ibca 501 Web Programming with Asp.Net and C# Asst. Prof. Pradnya J. Nehete