KUDVENKAT
KUDVENKAT
KUDVENKAT
NET - Part 1
3. On the server side, the Web application runs under Microsoft Internet Information
Services (IIS)
When the client enters the URL of the web application in the browser, and submits the request.
The web server which hosts the web application, receives the request. The request is then
processed by the application. The application generates, the HTML and hands it over to the IIS
(web server). Finally, IIS sends the generated HTML to the client, who made the initial request.
The client browser will the interpret the HTML and displays the user interface. All this
communication, happens over the internet using HTTP protocol. HTTP stands for Hyper Text
Transfer Protocol. A protocol is a set of rules that govern how two or more items communicate.
Creating ASP.NET website - Part 2
In this video session
1. We will learn about using visual studio
2. Creating your first ASP.NET web application
3. Learn about different windows in visual studio
If you have closed the start page, and later, if you want to see it again, select START
PAGE from the VIEW menu.
Solution Explorer: To view the solution explorer window, from the VIEW menu,
select SOLUTION EXPLORER. Or you can also use keyboard short cut, CTRL + W, S. On the
solution explorer, use the AUTO-HIDE push pin button, to either show or hide solution explorer.
Visual Studio organizes applications into projects and solutions. A solution is a collection of
projects. In our example, we have WebApplication2 solution. This solution has only one project
- WebApplication2. If you want to add another project to the solution, simply right click the
solution, and Select Add => New Project. For example, to add a class library project,
select CLASS LIBRARY from the New Project dialog box and click OK.
The solution file will have a .sln extension and the project file will have .csproj (if c# is the
programming language) or .vbproj (if visual basic is the programming language)
Tool Box: To view the TOOL BOX, Select TOOL BOX from the VIEW menu, or use the
keyboard short cut, CTRL + W, X. Just like, solution explorer, tool box can be auto hidden using
the AUTO-HIDE PUSH PIN. Toolbox displays the controls and components that can be used on
a web form.
Web Forms: WebForms has the extension of .aspx. A web form also has a code
behind and designer files. Code behind files has the extension of .aspx.cs (if c# is the
programming language) or .aspx.vb (if vb is the programming language). Designer files
contains the extension of .aspx.designer.cs (if c# is the programming language)
or .aspx.designer.vb (if visual basic is the programming language). Code behind files contain
the code that user writes, where as the designer file contains the auto generated code.
You shouldn't change the code in the designer file, because that code might later be modified
by Visual Studio and your changes could be overwritten. A Web form is associated with its code
file using the @Page directive found in the Web form’s HTML.
<%@ Page Title="Home
Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeBe
hind="Default.aspx.cs" Inherits="WebApplication2._Default" %>
A webform's HTML can be edited either in Source or Design mode. You can also
choose SPLIT mode, which shows both the DESIGN and the SOURCE at the same time.
Web Applications work on HTTP protocol. HTTP protocol is a stateless protocol, meaning
it does not retain state between user requests. Let's understand the stateless nature of the
HTTP protocol with an example.
Create a new asp.net web application project. Drag and drop a TextBox and a Button control
onto the webform. Change the Text property of the Button control to Click Me.
At this point, double click the button control, which should generate the event handler in the
code behind file. Modify the code behind file, so the code in WebForm1 class looks as shown
below.
1. In the scope of WebForm1 class, we are creating an integer variable ClicksCount which is
initialized to ZERO.
2. On the Page_Load() event handler, we are setting the Text property of TextBox1 to ZERO.
We do this initialization, only, when the request is an initial GET request.
3. In the Button1_Click() event, we are incrementing the value of the ClicksCount by 1, and
then assigning the value to the Text property of TextBox1.
public partial class WebForm1 : System.Web.UI.Page
{
int ClicksCount = 0;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
TextBox1.Text = "0";
}
}
With this code in place, run the application, and click the Button. We expect the count to
be increased every time we click the button. When you click it the first time, it gets incremented
to 1. After that, no matter how many times you click it, the value stays at 1. This is because of
the stateless nature of the web applications that work on HTTP protocol.
So what actually happens when you make a GET request for this WebForm1?
When we compile this project an assembly is generated. Since the name of the project
is ViewStateDemo, the name of the assembly will be ViewStateDemo.dll. So when a request
is made for WebForm1, The application's assembly(ViewStateDemo.dll) creates an instance
(object), of WebForm1, initializes ClicksCount to ZERO, and set's the TextBox1.Text to
ZERO. As this is the initial GET request, the Button1_Click() event will not be executed. At this
point the web server, generates the HTML to respond to the request, and posts that response
back to the browser. It then immediately destroys the instance of the WebForm1.
The browser receives the HTML, and we should now see textbox set to ZERO.
When you click the button for the second time, the webform gets posted back again. A new
instance of WebForm1 is created. ClicksCount initialized to ZERO. In the Button1_Click() event,
the value gets incremented to 1 and assigned to TextBox1. HTML gets generated and sends it
to client and destroys the webform.
So, no matter how many times you click the Button, the value of the TextBox, will not move
beyond 1.
Now, let's see, how to preserve the state between requests using ViewState
variables. Re-write the code in WebForm1, as shown below.
public partial class WebForm1 : System.Web.UI.Page
{
int ClicksCount = 1;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
TextBox1.Text = "0";
}
}
Click the Button now, and the value gets incremented every time we click. So how is this
possible now. It's possible because, we are using the ViewState variable Clicks to preserve the
data between requests. The ViewState data, travels with every
request and response between the client and the web server.
Now, let's try to achieve the same behaviour, without explicitly storing data in a
ViewState variable. Modify the WebForm1 code as shown below.
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
TextBox1.Text = "0";
}
}
Upon clicking the Button, the value gets incremented correctly as expected. This is possible
because, TextBox1 is an asp.net server control, that uses viewstate internally, to preserve
data across postbacks.
Because Web forms have very short lifetimes, ASP.NET takes special steps to preserve the
data entered in the controls on a Web form. Data entered in controls is sent with each request
and restored to controls in Page_Init. The data in these controls is then available in the
Page_Load(), Button_Click(), and many more events, that occur after Page_Init() event. We will
discuss about, all the events in the life cycle of a webform and the order in which they occur in a
later video session.
On the other hand the HTML controls, do not retain state across post backs. Only
ASP.NET server controls retains state. To prove this
1. Add a new webform to the web application project
2. Drag and Drop Input(Text) control from the HTML tab, in the ToolBox
3. Drag and Drop TextBox control from the Standard tab, in the ToolBox
4. Finally drag and drop a button
5. Set the newly added webform as the start page by right clicking on it, in the solution explorer
6. Run the project, by pressing CTRL + F5
7. Type "TEST" into both the controls (ASP.NET TextBox and the HTML TextBox), and press
the button
8. You should see that, the value in the ASP.NET TextBox is preserved across postback, but
not the value in the standard HTML textbox
Now, if you type TEST and click the button, both controls now retain state across
postback.
ViewState data is serialized into base64-encoded strings, and is stored in Hidden input
field __ViewState. To view this hidden input field, right click on the browser and select "View
Page Source" for google chrome. In internet explorer, right click and select "View Source"
Events in the life cycle of a web application - Part 4
In a web application, events can occur at 3 levels
1. At the Application Level(Example: Application Start)
2. At the Page Level(Example: Page Load)
3. At the Control Level (Example: Button Click)
In this video, we will learn about Application Level events. Before understanding
Application level events, lets talk about Session State and Application State variables. In Part
3 of this video series we have learnt about ViewState. ViewState variables are used to preserve
data across page post back. By default, ViewState of one webform is not available in another
webform.
If you want to make your data available on multiple web forms, there are several
techniques in ASP.NET, as listed below.
1. Query Strings
2. Cookies
3. Session State
4. Application State
Session state variables are available across all pages, but only for a given single session.
Session variables are like single-user global data. Only the current session has access to its
Session state.
Application State variables are available across all pages and across all sessions. Application
State variables are like multi-user global data. All sessions can read and write Application State
variables.
In an ASP.NET web application, Global.asax file conatins the application level events.
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
}
In general, Application events are used to initialize data that needs to be available to all the
current sessions of the application. Where as Session events are used to initialize data that
needs to be available only for a given individual session, but not between multiple sessions.
Now, let's write a simple application, using session and application level events. Create a new
asp.net web application, and copy paste the following code in Global.asax file.
1. Application_Start() event gets fired, when a first request is made, and if the application is
not already running.
2. Session_Start() event is fired every time a new browser instance, with a different session-id,
visits the application.
3. Session_End() event is fired when the user session times out. The default is 20 minutes.
This can be configured in the web.config file.
void Application_Start(object sender, EventArgs e)
{
// Create Application state variables
Application["TotalApplications"] = 0;
Application["TotalUserSessions"] = 0;
// Increment TotalApplications by 1
Application["TotalApplications"] = (int)Application["TotalApplications"] + 1;
}
void Session_Start(object sender, EventArgs e)
{
// Increment TotalUserSessions by 1
Application["TotalUserSessions"] = (int)Application["TotalUserSessions"] + 1;
}
void Session_End(object sender, EventArgs e)
{
// Decrement TotalUserSessions by 1
Application["TotalUserSessions"] = (int)Application["TotalUserSessions"] - 1;
}
Now, when you run the application, you get the following output:
Number of Applications: 1
Number of Users Online: 1
Copy the URL and open a new instance of the browser. Paste the URL and press enter. In the
new instance of the browser, we still see the same output.
We expected the Number of Users Online to be 2. The new instance of the browser, is treated
as part of the same session, because, by default the browser uses cookies to store session id.
The session id is read from the same cookie when you opened the new browser window.
Hence, Number of Users Online is not incremented.
How to get a new session-id and force the Session_Start() event to execute?
1. Close the browser: Close the existing browser window, which automatically deletes the
session cookie. Now, open a new brwoser instance. Since, the existing session cookie
associated with the previous browser instance is deleted. The new instance of the browser, will
get a new session-id and a session cookie.Now, if you navigate to WebForm1.aspx,
Session_Start() event gets fired and Number of Users Online is incremented to 2.
2. Open a new instance of a different browser: For example, if you first visited the application
with Google Chrome, now try accessing the same page with internet explorer, Session_Start()
event gets fired and Number of Users Online is incremented to 2.
3. Use Cookie-less Sessions: To use cookie-less sessions set the cookieless attribute to true
in web.config as shown below.
<sessionState mode="InProc" cookieless="false"></sessionState>
Let's understand the differences, with an example. Create a new asp.net web application.
ViewState:
Add a new WebForm, to the project and name it ViewState1.aspx. Drag and drop a button and
a text box control onto the webform. Double click the button control on the webform. This
automatically generates the event handler, for the button control.
Now add another webform, to the project, with name ViewState2.aspx. Just like you have done
for ViewState1.aspx, drag and drop a TextBox and a Button control onto this webform as
well.
Now, copy and paste the following code in ViewState1.aspx.cs and ViewState2.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (ViewState["Clicks"] == null)
{
ViewState["Clicks"] = 0;
}
TextBox1.Text = ViewState["Clicks"].ToString();
}
}
Now, run the application, and navigate to ViewState1.aspx. Click the button control.
Everytime, you click the button, the clicks count get incremented and is displayed in the
TextBox, as expected.
Now, navigate to ViewState2.aspx. Click the button, on this page. Notice, that the value starts
from ZERO, indicating that, each page has it's own ViewState["Clicks"].
So, the conclusion is that, ViewState of a webform is available only with in that webform,
by default.
So, where does this viewstate, gets stored - On the client or on the server? ViewState is
stored on the page using a hidden field called _ViewState. So, ViewState travels along with the
page, between the client and the server, with each request and response.
ASP.NET uses viewstate, to retain the values a user types into controls on the webform, across
postbacks.
SessionState:
Add a new webform with name SessionState1.aspx. Drag and drop a button and a text box
control onto SessionState1.aspx. Do the same thing by adding a page with
name SessionState2.aspx.
Add the following sessionstate element to your web.config file, under system.web. This
setting, specifies the web application to use cookieless sessions.
<sessionState mode="InProc" cookieless="true"></sessionState>
Run the application and navigate to SessionState1.aspx. Click the button 3 times, and notice
that, the value 3 is displayed in the TextBox. Now, navigate to SessionState2.aspx. The
value 3 is displayed in the TextBox on SessionState2.aspx. Now, click twice, the value is
incremented to 5. Now, navigate back to SessionState1.aspx, and you should see the value
5. This proves that a session state variable is accessible across all pages in a web
application.
Now, open a new browser window and navigate to SessionState1.aspx (Make sure you have
a different session-id). Notice that, the value in the textbox is ZERO. So, this proves that,
Session state variables are available across all pages, but only for a given single session.
Session variables are like single-user global data. Only the current session has access to its
Session state.
Application State:
Add a new webform with name ApplicationState1.aspx. Drag and drop a button and a text box
control onto ApplicationState1.aspx. Do the same thing by adding a page with
name ApplicationState2.aspx.
Run the application and navigate to ApplicationState1.aspx. Click the button 3 times, and
notice that, the value 3 is displayed in the TextBox. Now, navigate to ApplicationState2.aspx.
The value 3 is displayed in the TextBox on ApplicationState2.aspx. Now, click twice, the value is
incremented to 5. Now, navigate back to ApplicationState1.aspx, and you should see the value
5. This proves that an application state variable is accessible across all pages in a web
application.
Now, open a new browser window and navigate to ApplicationState1.aspx. Notice that, the
value in the textbox is still 5. So, this proves that, Application State variables are
available across all pages and across all sessions. Application State variables are like multi-
user global data. All sessions can read and write Application State variables.
3. ViewState is used by all asp.net controls to retain their state across postback
Session State:
1. Session state variables are available across all pages, but only for a given single session.
Session variables are like single-user global data.
2. Session state variables are stored on the web server.
3. SessionState variables are cleared, when the user session times out. The default is 20
minutes. This is configurable in web.config
Application State:
1. Application State variables are available across all pages and across all sessions. Application
State variables are like multi-user global data.
2. Application State variables are stored on the web server.
3. Application State variables are cleared, when the process hosting the application is restarted.
ASP.NET Page Life Cycle Events - Part 6
Suggested videos before continuing with this session
Part 3 - Understanding ViewState
Part 4 - Events in the life cycle of a web application
Part 5 - Difference between ViewState, SessionState and ApplicationState
In Part 4, of this video series, we have discussed that, events can occur at 3 levels in an
asp.net web application.
1. At the application level. (Example - Session_Start event in global.asax)
2. At the Page or web form level (Example - Page_Load)
3. At the control level(Example - Selected Index changed event of a dropdownlist)
In this video, we will discuss about events at the page level. From the previous,parts of this
video series, we have learnt that, web applications work on a stateless protocol. Every time a
request is made for a webform, the following sequence of events occur.
1. Web Application creates an instance of the requested webform.
2. Processes the events of the webform.
3. Generates the HTML, and sends the HTML back to the requested client.
4. The webform gets destroyed and removed from the memory.
The following are some of the commonly used events in the life cycle of an asp.net
webform. These events are shown in order of occurrence, except for, Error event, which
occurs only if there is an unhandled exception.
PreInit - As the name suggests, this event happens just before page initialization event
starts. IsPostBack, IsCallback and IsCrossPagePostBack properties are set at this stage. This
event allows us to set the master page and theme of a web application dynamically. PreInit is
extensively used when working with dynamic controls.
Init - Page Init, event occurs after the Init event, of all the individual controls on the webform.
Use this event to read or initialize control properties. The server controls are loaded and
initialized from the Web form’s view state.
InitComplete - As the name says, this event gets raised immediately after page initialization.
Load - Page Load event, occurs before the load event of all the individual controls on that
webform.
Control Events - After the Page load event, the control events like button's click, dropdownlist's
selected index changed events are raised.
Load Complete - This event is raised after the control events are handled.
PreRender - This event is raised just before the rendering stage of the page.
To see the events execution order, create a new asp.net web project, and copy the
following code.
protected void Page_PreInit(object sender, EventArgs e)
{ Response.Write("Page_PreInit" + "<br/>"); }
Run the project and you should see the following output.
Page_PreInit
Page_Init
Page_InitComplete
Page_PreLoad
Page_LoadComplete
Page_PreRender
Page_PreRenderComplete
In the previous parts of this video series, we have discussed that events can occur at
application, page and control levels. In Part 4, we discussed application level events, and in Part
6, we discussed about Page Level events.
In this session, we will discuss about control level events. ASP.NET server controls, such
as TextBox, Button, and DropDownList has their own events. For example, Button has
a click event. TextBox has TextChanged event,
and DropDownList has SelectedIndexChanged event. We have a set of asp.net validation
controls, that has validation events. The events that all these controls expose, can be broadly
divided into 3 categories.
Postback events - These events submit the Web page, immediately to the server for
processing. Click event of a button control is an example for PostBack event.
Cached events - These events are saved in the page’s view state to be processed when a
postback event occurs. TextChanged event of TextBox control,
and SelectedIndexChanged event of a DropDownList control are examples of cached events.
Cached events can be converted into postback events, by setting the AutoPostBack property
of the control to true.
Validation events - These events occur on the client, before the page is posted back to the
server. All validation controls use these type of events.
In Part 6, of this video series, we have understood that control events are processed after the
PageLoad event. The picture below depicts the same. Among the control events, Cached
events happen before PostBack events.
To understand the order in which Page and Server control events execute, add a Web
form with a TextBox, RequiredFieldValidator, and a Button control. You can
find RequiredFieldValidator under Validation tab, in the ToolBox. Double click the TextBox
control on the WebForm, and the event handler TextBox1_TextChanged() will be automatically
generated. Along the same lines, double click the Button control, Button1_Click() event handler
will be generated. Right Click the RequiredFieldValidator control on the webform and select
Properties. From the Properties window, Set ControlToValidate property to TextBox1. At this
stage copy and paste the following code in WebForm1.aspx.cs.
protected void Page_PreInit(object sender, EventArgs e)
{
Response.Write("Page_PreInit" + "<br/>");
}
protected void Page_Init(object sender, EventArgs e)
{
Response.Write("Page_Init" + "<br/>");
}
protected void Page_InitComplete(object sender, EventArgs e)
{
Response.Write("Page_InitComplete" + "<br/>");
}
protected void Page_PreLoad(object sender, EventArgs e)
{
Response.Write("Page_PreLoad" + "<br/>");
}
protected void Page_LoadComplete(object sender, EventArgs e)
{
Response.Write("Page_LoadComplete" + "<br/>");
}
protected void Page_PreRender(object sender, EventArgs e)
{
Response.Write("Page_PreRender" + "<br/>");
}
protected void Page_PreRenderComplete(object sender, EventArgs e)
{
Response.Write("Page_PreRenderComplete" + "<br/>");
}
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
Response.Write("Text Changed Event"+ "<br/>");
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write("Button Click"+ "<br/>");
}
Now, run the project, and when the webform renders, the page level events occur in the
following order. Notice that, TextChanged and ButtonClick events are not fired.
Page_PreInit
Page_Init
Page_InitComplete
Page_PreLoad
Page_LoadComplete
Page_PreRender
Page_PreRenderComplete
Don't type anything in the TextBox, and click the button control. The RequiredFieldValidator
message is displayed. No other events get processed and the page is not posted back to the
server.
Now, enter some text, into the TextBox and Click the button. Notice that, Text Changed
Event and Button Click, execute after Page Load and Before Page Load Complete events.
Among the control events, TextChanged event is fired before the click event. The execution
order is shown below.
Page_PreInit
Page_Init
Page_InitComplete
Page_PreLoad
Text Changed Event
Button Click
Page_LoadComplete
Page_PreRender
Page_PreRenderComplete
IsPostBack in asp.net - Part 8
Suggested Videos
Part 3 - ViewState in ASP.NET
Part 6 - ASP.NET Page Life Cycle Events
Part 7 - ASP.NET Server Control Events
IsPostBack is a Page level property, that can be used to determine whether the page is being
loaded in response to a client postback, or if it is being loaded and accessed for the first time.
In real time there are many situations where IsPostBack property is used. For example,
consider the webform used to register employee details. A sample form that we will use for this
example is shown below. The form has First Name, Last Name and City fields.
If you want to follow along with me, copy and paste the following HTML in a web form.
<table style="font-family: Arial">
<tr>
<td colspan = "2"><b>Employee Details Form</b></td>
</tr>
<tr>
<td>First Name: </td>
<td> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> </td>
</tr>
<tr>
<td>Last Name: </td>
<td> <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> </td>
</tr>
<tr>
<td>City:</td>
<td>
<asp:DropDownList ID="ddlCity" runat="server">
</asp:DropDownList>
</td>
</tr>
<tr>
<td></td>
<td>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click"
Text="Register Employee" />
</td>
</tr>
</table>
Copy and Paste the following code in the code behind file of the web form.
protected void Page_Load(object sender, EventArgs e)
{
LoadCityDropDownList();
}
public void LoadCityDropDownList()
{
ListItem li1 = new ListItem("London");
ddlCity.Items.Add(li1);
Now run the application. Look at the City DropDownList. The cities, (London, Sydney and
Mumbai) are correctly shown as expected. Just click the button once. Notice, that the city
names in the DropDownList are duplicated. So, every time you click the button, the city
names are again added to the DropDownList.
We know that all ASP.NET server controls retain their state across postback. These controls
make use of ViewState. So, the first time, when the webform load. the cities get correctly added
to the DropDownList and sent back to the client.
Now, when the client clicks the button control, and the webform is posted back to the server for
processing. During the Page initialization, ViewState restoration happens. During this stage, the
city names are retrieved from the viewstate and added to the DropDownList. PageLoad event
happens later in the life cycle of the webform. During page load we are again adding another set
of cities. Hence, the duplication.
Another way to solve, this problem is to simply disable the ViewState of the DropDownlist
control. To disable the viewstate, right click the DropDownList control, and set EnableViewState
property to false. Now run the project, and the cities duplication issue is gone.
But the problem, with this approach is that, the DropDownList list, does not remember your
selecttion across postback. That is, Select "Mumabi" as the city, and submit the form. When the
page rerenders, observer that selection is set back to "London". Another problem with, disabling
the viewstate is that, the DropDownList events may not work correctly as expected.
Another way to solve this, is to clear all the DropDownList items, before calling
LoadCityDropDownList() method. But this not efficient from a performance perspective. The
modified code is shown below.
Create a new asp.net web application and run it by pressing CTRL + F5. Notice the URL of the
page in the browser. A random port number is used. On my machine it was using port number
16041.
http://localhost:16041/WebForm1.aspx
To confirm, if this is the built-in visual studio development server, check the notifications
area on the task bar, and you should see ASP.NET Development Server running. Please refer
to the image below.
You can also create the virtual directory from visual studio, on the project properties
window.
1. Select Use Local IIS Web Server
2. Project URL will be populated automatically. You can change the name of the virtual directory
if you wish to do so.
3. Click Create Virtual Directory button.
4. After a few seconds the virtual directory was successfully created message will appear.
5. Click OK
ASP.NET TextBox Control - Part 10
The TextBox control is used to get the input from the user of the web application. An
asp.net textbox has several properties, that we need to be aware of as a developer.
2. Text - Use this property to set or get the Text from the TextBox.
4. ReadOnly - Set this property to true if you don't want the user to change the text in the
TextBox.
5. ToolTip - The tooltip is displayed when the mouse is over the control.
6. Columns - Use this property to specify the width of the TextBox in characters
Events of TextBox:
TextChanged - This event is fired, when the text is changed.
Methods of a TextBox:
Focus - Set input focus onto the control.
To view the properties of the TextBox, Right click on the control, and select Properties. In the
properties window, you can also find the events supported by the control.
All these properties can be set at the design time, or at runtime using code.
ASP.NET Radio Button Control - Part 11
Radio Button control is used, when you want the user to select only one option from the
available choices. For example, the gender of a person. A person can be Male or Female. He
cannot be both. So, if the user has first selected Male, and if tries to select Female, the initial
Male selection he made should automatically get de-selected. Another example, would be when
you want the user to select his or her favourite colour.
In short, if you want to provide the user with mutually exclusive options, then choose a
Radio Button Control.
Text - This is string property used to get or set the text associated with the radio button control
TextAlign - right or left. On which side of the radio button the text should appear
AutoPostBack - Set this property to true, if you want the webform to be posted immediately
when the checked status of the radio button changes.
Group Name - By default, the individual radio button selections, are not mutually exclusive. If
you have a group of radio buttons, and if you want the selections among the group to be
mutually exclusive, then use the same group name for all the radio button controls.
Events:
CheckedChanged - This event is fired when the checked status of the radio button control is
changed.
In this video we will learn about the properties, methods and events of an asp.net CheckBox
control
CheckBox Control is used, when you want the user to select more than one option from
the available choices. For example, the education of a person. A person can have a graduate
degree, post graduate degree and a doctrate. In this case the user selects all the 3 checkboxes.
Where as a person, may just have a graduate degree, in which case he only selects, the
graduate checkbox.
Another example, would be when you want the user to select the days of his availability.
In short, if you want to provide the user with more than one option to select from, then
choose a check box Control.
Methods:
Focus() - Just like TextBox, checkbox also supports, Focus() method. If you want to set the
input focus, to a specific checkbox, Call this method for that check box control.
Events:
CheckedChanged - This event is fired when the checked status of the check button control is
changed.
The ASP.NET Hyperlink control is used to create a link to another Web page.
Properties:
Text - The link text that will be shown to the user
Navigate URL - The URL of the page to which the user will be sent
ImageURL - The URL of the image, that will be displayed for the link. If you specify both the
Text and ImageUrl, the image will be displayed instead of the text. If for some reason, the image
is not unavailable, the text will be displayed.
Target - If target is not specified, the web page to which the hyperlink is linked, will be displayed
in the same window. If you set the Target to _blank, the web page will be opened in a new
window.
Methods:
Focus() - Call this method to Set the input focus when the page loads.
Events:
No HyperLink control specific events
The Button, LinkButton and ImageButton controls in ASP.NET are used to post a page to
the server.
1. Button - The Button control is used to display a push button. Use the Text property to change
the Text on the Button control.
2. LinkButton - LinkButton displays the button like a HyperLink. Use the Text property to
change the LinkText.
3. ImageButton - ImageButton provides the flexibility of associating an Image with the button,
using the ImageURL property.
All the 3 button controls support CommandName and CommandArgument properties. We will
talk about these properties in the next video session. These 3 button controls also
support CuasesValidation and ValidationGroup properties. We will discuss about these
properties, when we talk about validation controls in asp.net. We will discuss
about PostBackURL property, when we talk about cross page post back.
All the 3 button controls, exposes client side click event and server side click event. You can
associate the javascript, that you want to run in response to the click event on the client side
using OnClientClick property as shown below.
<asp:Button ID="Button1" runat="server"
OnClientClick="alert('You are about to submit this page')"
Text="Button" />
When you click this button, you will get a popup as shown below. Once you click OK, the
webform will be submitted to the server for processing server side click event.
In the above example we are using javascript, alert() function. The client side alert message
box, can be used to communicate important information to the user. For example messages like
1. You are about to place an order
2. You are about to leave this website
Sometimes, we may accidentally click on a delete button, which deletes the record
permanently. So, whenever, we do things like this, we want to be double sure, if the user really
wants to delete the record. The javascript confirm(), function can be used for this purpose.
<asp:Button ID="Button1" runat="server"
OnClientClick="return confirm('Are you sure you want to delete this record?')"
Text="Button" />
When you click the button now, the user will be shown a confirmation box, as shown
below.
If you click cancel, the confirm() function returns false and the webform will not be submitted. If
you click OK, the confirm() function returns true, and the webform will be posted to the server.
So, far we have associated the javascript, to the client click event of a button control at design
time. It is also, possible, to do the same at runtime using the Button controls attribute collection
as shown below.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Button1.Attributes.Add("onclick", "return confirm('Do you want to delete the record?');");
}
}
ASP.NET button control exposes 2 events - Click and Command events. In Part 14, we
have discussed about the click event. In this session we will discuss about the Command event.
When the Button is clicked, both the events are raised. Click event happens before the
Command event.
To prove this drag and drop a button control onto the webform
1. Double click the Button control. This will automatically generate the click event handler in the
code behind file
2. To generate the command event handler, right click the button control and select properties.
Click the events icon, in the properties window. Double click on the command event. The event
handler for the command event should now be generated.
If you are following along with me. At this stage the HTML for the button control in the
aspx page, should look as shown below.
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click"
CommandName="Button1" oncommand="Button1_Command" />
Please copy and paste the following code in the code behind file.
When you click the Button now, you should see the following output. This proves that when a
button is clicked, first the Click event and then the Command event is fired.
Button1 Click event handled
Button1 Command event handled
The click event handler and the command event handlers, are attached to the
respective Click and Command events in the HTML using onclick and oncommand attributes.
The event handlers can also be attached programatically as shown below.
protected void Page_Load(object sender, EventArgs e)
{
Button1.Click += new EventHandler(Button1_Click);
Button1.Command += new CommandEventHandler(Button1_Command);
}
If you have multiple button controls on a webform, and if you want to programmatically
determine which Button control is clicked, we can make use of Command event, along
with CommandName and CommandArgument properties. Command event, makes it possible
to have a single event handler method responding to the click event of multiple buttons. The
command event, CommandName and CommandArgument properties are extremely useful
when working with data-bound controls like Repeater, GridView, DataList. We will discuss about
Repeater, GridView, and DataList in a later video session.
Let's understand this with an example. Consider the HTML below. Here we have 4 buttons.
Notice that all the button controls have the same command event handler method
- oncommand="CommandButton_Click". Also, notice
the CommandName and CommandArgument properties. We will later use these properties, in
the code behind to determine which button is clicked.
<asp:Button ID="PrintButton" runat="server" Text="Print" oncommand="CommandButton_Click"
CommandName="Print"/>
Copy and Paste the following code in your code behind file. The CommandEventArgs
object, has the CommandName and CommandArgument properties, that are used to
programatically determine which button the user has clicked.
protected void CommandButton_Click(object sender, CommandEventArgs e)
{
switch (e.CommandName)
{
case "Print":
OutputLabel.Text = "You clicked Print Button";
break;
case "Delete":
OutputLabel.Text = "You clicked Delete Button";
break;
case "Show":
if (e.CommandArgument.ToString() == "Top10")
{
OutputLabel.Text = "You clicked Show Top 10 Employees Button";
}
else
{
OutputLabel.Text = "You clicked Show Bottom 10 Employees Button";
}
break;
default:
OutputLabel.Text = "We don't know which button you clicked";
break;
}
}
Note: All the 3 button controls - Button, LinkButton and ImageButton, expose Command event,
the CommandName and CommandArgument properties.
Now switch the webform to source mode. Notice that in the HTML it has added ListItem
object, as shown below.
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Value="1">Male</asp:ListItem>
<asp:ListItem Value="1">Female</asp:ListItem>
</asp:DropDownList>
If you run the web application now, you should see that Male and Female items shown in the
DropDownList.
If you want a specific listitem to be selected in the dropdownlist, when the page loads,
then set the Selected property of the ListItem object to true. This can be done in 2 ways.
1. Using the ListItem Collection Editor window or
2. In the HTML of the webform
DropDownList1.Items.Add(maleListItem);
DropDownList1.Items.Add(femaleListItem);
}
}
If you are adding listitem objects, to the DropDownList in the Page_Load event, make sure
you do only when the page is loaded for the first time. Otherwise, every time, you post the page
back, by clicking a button, the list items will be added again causing duplication.
A DropDownList is a collection of ListItem objects. Along the same lines, the following
controls are also a collection of ListItem objects. So, adding items to these controls is also very
similar to DropDownList.
1. CheckBoxList
2. RadioButtonList
3. BulletedList
4. ListBox
In the next video session, we will discuss about binding the dropdownlist to the data from a
database and an XML file.
Data bind asp.net dropdownlist with data from the database - Part 17
Suggested Videos
Part 14 - Button, LinkButton and ImageButton Controls
Part 15 - Command Event of an asp.net button control
Part 16 - DropDownList in asp.net
In this video, we will learn about, binding data from a database table, to a dropdownlist. We will
be using tblCity table for this demo. Please find the script below, to create and populate the
table.
Create an ASP.NET web application. Drag and drop a DropDownList control onto the
webform. Copy and paste the following code in the code behind page.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using (SqlConnection con = new SqlConnection(CS))
{
SqlCommand cmd = new SqlCommand("Select CityId, CityName, Country from tblCity",
con);
con.Open();
SqlDataReader rdr = cmd.ExecuteReader();
DropDownList1.DataSource = rdr;
DropDownList1.DataBind();
}
}
}
Run the application now. The city names are displayed as expected. But make sure to set
the properties(DataTextField, DataValueField) before calling DataBind() method. Also, note that,
these properties can be set in the HTML of the aspx page as well.
<asp:DropDownList ID="DropDownList1" DataTextField="CityName"
DataValueField="CityId" runat="server">
</asp:DropDownList>
In Part 17 of this video series we have discussed about binding a DropDownList to data that
is retrieved from a database. In this part, we will learn about binding the DropDownList to
Data from an XML file.
Drag and drop a DropDownList on the webform. Copy and paste the following code in the code
behind page.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//Create a new DataSet
DataSet DS = new DataSet();
//Read the xml data from the XML file using ReadXml() method
DS.ReadXml(Server.MapPath("Countries.xml"));
DropDownList1.DataTextField = "CountryName";
DropDownList1.DataValueField = "CountryId";
DropDownList1.DataSource = DS;
DropDownList1.DataBind();
ListItem li = new ListItem("Select", "-1");
DropDownList1.Items.Insert(0, li);
}
}
The important thing to notice here is that, we are using ReadXml() method of the DataSet
object, to read the data from the Countries.xml file into a DataSet. Server.MapPath() method
returns the physical path of the file from the provided virtual path. We will discuss about this
method in a later video session.
To insert a ListItem at a specific location use the Insert() method specifying the index of the
location where you want to insert, and the listitem object.
In this video we will discuss about Server.MapPath() method. This method returns the
physical path for a given virtual path. This method can be used in several different ways,
depending on the characters that we use in the virtual path. Let's understand this with an
example.
1. Create an asp.net web application in C:\ and name it SampleWeb.
2. Right click on the SampleWeb project in solution explorer and add a new webform and name
it PageInRootDirectory.aspx
3. Add a new folder to the project and name it Categories.
4. Right click on the Categories folder, and add another folder. name it Electronics
5. Add a webform to the Electronics folder and name it PageInElectronicsFolder.aspx
6. At this point, right click on the web application project and add a new folder. Name it Data.
7. Add a sub folder to Data, and name it Countries
8. Right click on the Countries folder and add an XML file. Name it Countries.xml.
9. Copy and paste the following in Countries.xml file.
<?xml version="1.0" encoding="utf-8" ?>
<Countries>
<Country>
<CountryId>101</CountryId>
<CountryName>India</CountryName>
</Country>
<Country>
<CountryId>102</CountryId>
<CountryName>US</CountryName>
</Country>
<Country>
<CountryId>103</CountryId>
<CountryName>Australia</CountryName>
</Country>
<Country>
<CountryId>104</CountryId>
<CountryName>UK</CountryName>
</Country>
</Countries>
If you are following along with me, at this stage, the solution explorer should look as
shown below.
Copy and paste the following code in PageInElectronicsFolder.aspx.cs
Response.Write(". returns " + Server.MapPath(".") + "<br/>");
Response.Write(".. returns " + Server.MapPath("..") + "<br/>");
Response.Write("~ returns " + Server.MapPath("~") + "<br/>");
C:\SampleWeb\SampleWeb is the root directory for the sample asp.net web application that
we used in the Demo. To get to this root directory we are passing ../../ to the Server.MapPath()
method as shown below.
DS.ReadXml(Server.MapPath("../../Data/Countries/Countries.xml"));
The number of double dots, that you use, depends on how deep you are in the folder
hierarchy. To avoid confusion, if any time you want to navigate to the root directory of the
application, it is better to use ~(tilde) character as shown below.
DS.ReadXml(Server.MapPath("~/Data/Countries/Countries.xml"));
Tilde(~) symbol resolves to the root application directory, no matter how deep you are in the
folder hierarchy. This is the advantage of using ~(tilde), over ..(2 Dots). The following code
would work from any folder in our application.
DS.ReadXml(Server.MapPath("~/Data/Countries/Countries.xml"));
Where as, the following code will only work from folders, that are 2 levels deeper relative
to the root directory of the application.
DS.ReadXml(Server.MapPath("../../Data/Countries/Countries.xml"));
Copy and paste the following code in the code behind file.
protected void Button1_Click(object sender, EventArgs e)
{
if (DropDownList1.SelectedValue != "-1")
{
Response.Write("Selected Item Text = " + DropDownList1.SelectedItem.Text + "<br/>");
Response.Write("Selected Item Value = " + DropDownList1.SelectedItem.Value + "<br/>");
Response.Write("Selected Item Index = " + DropDownList1.SelectedIndex.ToString());
}
else
{
Response.Write("Please select the continent");
}
}
The SelectedIndex and SelectedValue properties of the DropDownList can also be used to
have a list item selected in the DropDownList. For example, the following code would default the
selection to Asia when the page loads for the first time.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DropDownList1.SelectedValue = "1";
}
}
The same thing can also be achieved using the SelectedIndex property as shown below.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DropDownList1.SelectedIndex = 1;
}
}
In this video we will discuss about cascading dropdownlists. First create the required
tables and populate them, with some sample data using the SQL script below.
Let's understand cascading dropdownlists with an example. The following are the 3
dropsownlist controls, that we will have in our asp.net web application.
1. Continents DropDownList
2. Countries DropDownList
3. Cities DropDownList
When the webform first loads, only the continents dropdownlist should be
populated. Countries and Cities dropdownlist should be disabled and should not allow the user
to select anything from these 2 dropdownlists. Once, the user makes a selection in the
continents dropdownlist, then Countries dropdownlist should be enabled and populated with the
countries that belong to the selected continent. The same logic applies for the
cities dropdownlist.
To achieve this drag and drop 3 dropdownlist controls onto the webform. The HTML of
the Webform should be as shown below.
Copy and paste the following code in the code behind page
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
PopulateContinentsDropDownList();
}
}
ddlCountries.Enabled = false;
ddlCities.Enabled = false;
}
ddlCities.SelectedIndex = 0;
ddlCities.Enabled = false;
}
}
In this video we will learn about asp.net checkboxlist control. Just like DropDownList
1. CheckBoxList is collection of ListItem objects.
2. Items can be added to the CheckBoxList in the HTML source or in the code behind file
3. CheckBoxList can be bound to a database table or an xml file
DropDownList is generally used, when you want to present the user with multiple choices,
from which you want him to select only one option. Where as if you want the user to select more
than one option, then a CheckBoxList control can be used.
Create an asp.net web application. Copy and paste the following HTML
<asp:CheckBoxList ID="checkboxListEducation" runat="server"
RepeatDirection="Horizontal">
<asp:ListItem Text="Diploma" Value="1"></asp:ListItem>
<asp:ListItem Text="Graduate" Value="2"></asp:ListItem>
<asp:ListItem Text="Post Graduate" Value="3"></asp:ListItem>
Copy and paste the following code in Button1_Click event. The sample code prints the
Text, Value and Index of the selected list item object.
// Loop thru each list item in the checkboxlist
foreach (ListItem li in checkboxListEducation.Items)
{
// If the list item is selected
if (li.Selected)
{
// Retrieve the text of the selected list item
Response.Write("Text = " + li.Text + ", ");
// Retrieve the value of the selected list item
Response.Write("Value = " + li.Value + ", ");
// Retrieve the index of the selected list item
Response.Write("Index = " + checkboxListEducation.Items.IndexOf(li).ToString());
Response.Write("<br/>");
}
}
By default, the ListItem objects are laid out in vertical direction. If you want to change the
direction, use RepeatDirection property
<asp:CheckBoxList ID="checkkboxListEducation" runat="server" RepeatDirection="Hori
zontal">
RepeatColumns property specifies the number of columns used to lay out the items.
Set the Enabled property of the ListItem object to false, to disable the selection, in the
CheckBoxList control.
SelectedIndex property of the CheckBoxList control can also be used to get the index of the
selected item in the checkboxlist. But this property, returns only one selected item, and that too,
the item with the lowest index. SelectedIndex property returns -1, if nothing is selected.
SelectedValue property returns the selected Item's value, but only for one selected item. If no
item is selected this property returns empty string.
To retrieve the Text of the selected item, SelectedItem.Text property can be used.
SelectedItem will be NULL, if nothing is selected, and hence, calling Text and Value properties
may cause NullReferenceException. Hence, it is important to check for null, when using
SelectedItem property of a CheckBoxList control.
if (checkboxListEducation.SelectedItem != null)
{
Response.Write(checkboxListEducation.SelectedItem.Text);
}
To have a ListItem pre-selected, when the page renders, we can do that in the HTML by setting
the Selected property to True as shown below.
<asp:ListItem Text="Diploma" Selected="True" Value="1"></asp:ListItem>
This can be programmatically done, using the SelectedValue property as shown below.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
CheckBoxList1.SelectedValue = "1";
}
}
Copy and paste the following HTML and Code in the ASPX and the code behind page
<asp:Button ID="buttonSelectAll" runat="server" Text="Select All"
onclick="buttonSelectAll_Click" />
<asp:Button ID="buttonDeselectAll" runat="server" Text="De-Select All"
onclick="buttonDeselectAll_Click" />
<br /><br />
<asp:CheckBoxList ID="CheckBoxList1" runat="server"
RepeatDirection="Horizontal">
<asp:ListItem Text="Diploma" Value="1"></asp:ListItem>
<asp:ListItem Text="Graduate" Value="2"></asp:ListItem>
<asp:ListItem Text="Post Graduate" Value="3"></asp:ListItem>
<asp:ListItem Text="Doctrate" Value="4"></asp:ListItem>
</asp:CheckBoxList>
protected void buttonSelectAll_Click(object sender, EventArgs e)
{
foreach (ListItem li in CheckBoxList1.Items)
{
li.Selected = true;
}
}
Properties
Rows : The number of visible rows in the Listbox. A scrollbar is automatically generated, if the
total number of item are greater than the number of visible rows in the listbox.
SelectionMode : SelectionMode can be Single or Multimple. By default, this property value is
Single, meaning when the listbox renders, the user can select only one item from the listbox.
Set this property to Multimple, to enable multiple item selections. To select, multiple items from
the listbox, hold-down the CTRL key, while the listitem's are selected.
Please note that, it is not possible to set the Selected property of more than one ListItem object
to true, if the SelectionMode of the listbox is to Single.
Retrieving the selected item's Text, Value and Index is similar to DropDownList and
CheckBoxList
ASPX Page code:
<asp:ListBox ID="ListBox1" runat="server" SelectionMode="Multiple">
<asp:ListItem Text="Diploma" Value="1"></asp:ListItem>
<asp:ListItem Text="Graduate" Value="2"></asp:ListItem>
<asp:ListItem Text="Post Graduate" Value="3"></asp:ListItem>
<asp:ListItem Text="Doctrate" Value="4"></asp:ListItem>
</asp:ListBox>
<br />
<br />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
In this video we will discuss about a simple real time example using asp.net checkboxlist
and listbox.
Copy and Paste the following code in the code behind page
protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
{
// Everytime the selection changes, clear the items in the listbox
ListBox1.Items.Clear();
// Loop thru each litemitem in the checkboxlist
foreach (ListItem li in CheckBoxList1.Items)
{
// If the listitem is selected
if (li.Selected)
{
// Add the listitem text to the listbox
ListBox1.Items.Add(li.Text);
In this video we will learn about asp.net RadioButtonList control. Just like every other list
control
1. RadioButtonList is also a collection of ListItem objects.
2. Items can be added to the RadioButtonList in the HTML source or in the code behind file
3. RadioButtonList like any other list control supports databinding. For example, RadioButtonList
can be bound to a database table or an xml file
CheckBoxList is generally used, when you want to present the user with multiple choices,
from which you want him to select one or more options. Where as if you want the user to select
only one option, then a RadioButtonList control can be used, i.e RadioButtonList is commonly
used to present mutually exclusive choices.
Create an asp.net web application. Copy and paste the following HTML
<asp:RadioButtonList ID="ColorRadioButtonList" runat="server"
RepeatDirection="Horizontal">
<asp:ListItem Text="Red" Value="1"></asp:ListItem>
<asp:ListItem Text="Green" Value="2"></asp:ListItem>
<asp:ListItem Text="Blue" Value="3"></asp:ListItem>
<asp:ListItem Text="Orange" Value="4"></asp:ListItem>
</asp:RadioButtonList>
<br />
<asp:Button ID="btnSubmit" runat="server" Text="Submit"
onclick="btnSubmit_Click"/>
<asp:Button ID="btnClearSelection" runat="server" Text="Clear Selection"
onclick="btnClearSelection_Click"/>
Copy and paste the following code in your code-behind page
protected void btnSubmit_Click(object sender, EventArgs e)
{
// If the user has made a choice
if (ColorRadioButtonList.SelectedIndex != -1)
{
Response.Write("Text = " + ColorRadioButtonList.SelectedItem.Text + "<br/>");
Response.Write("Value = " + ColorRadioButtonList.SelectedItem.Value + "<br/>");
Response.Write("Index = " + ColorRadioButtonList.SelectedIndex.ToString());
}
// If the user has not selected anything
else
{
Response.Write("Please select your favourite color");
}
}
RadioButtonList Example
By default, the ListItem objects are laid out in vertical direction. If you want to change the
direction, use RepeatDirection property
<asp:RadioButtonList ID="ColorRadioButtonList" runat="server" RepeatDirection="Horiz
ontal">
RepeatColumns property specifies the number of columns used to lay out the items.
RepeatLayout property, specifies the layout to be used by each list item. The following are
the values allowed by RepeatLayout property
1. Table
2. Flow
3. OrderedList
4. UnorderedList
Please note that the, OrderedList and UnorderedList layouts are only supported, if the
RepeatDirection is vertical.
Set the Enabled property of the ListItem object to false, to disable the selection, in the
RadioButtonList control.
To retrieve the Text of the selected item, SelectedItem.Text property can be used.
SelectedItem will be NULL, if nothing is selected, and hence, calling Text and Value properties
may cause NullReferenceException. Hence, it is important to check for null, when using
SelectedItem property of a RadioButtonList control.
if (ColorRadioButtonList.SelectedItem != null)
{
Response.Write(ColorRadioButtonList.SelectedItem.Text);
}
Properties of BulletedList
BulletStyle - This property, is used to customize the bullets style. If CustomImage is specified
as the BulletStyle, then BulletImageURL, also needs to be specified.
The following HTML, sets the DisplayMode="HyperLink". By default, the target page is
displayed in the same browser window. Set the Target property of the BulletedList to _blank, to
open the target page in it's own window.
<asp:BulletedList ID="CountriesBulletedList" runat="server"
BulletStyle="Numbered" DisplayMode="HyperLink">
<asp:ListItem Text="Google" Value="http://google.com"></asp:ListItem>
<asp:ListItem Text="Youtube" Value="http://Youtube.com"></asp:ListItem>
<asp:ListItem Text="Blogger" Value="http://Blooger.com"></asp:ListItem>
<asp:ListItem Text="Gmail" Value="http://Gmail.com"></asp:ListItem>
</asp:BulletedList>
The only difference here is the tag name, otherwise adding ListItems is very identical.
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Text="Item1" Value="1"></asp:ListItem>
<asp:ListItem Text="Item2" Value="2"></asp:ListItem>
<asp:ListItem Text="Item3" Value="3"></asp:ListItem>
</asp:DropDownList>
<br />
<asp:CheckBoxList ID="CheckBoxList1" runat="server">
<asp:ListItem Text="Item1" Value="1"></asp:ListItem>
<asp:ListItem Text="Item2" Value="2"></asp:ListItem>
<asp:ListItem Text="Item3" Value="3"></asp:ListItem>
</asp:CheckBoxList>
<br />
<asp:RadioButtonList ID="RadioButtonList1" runat="server">
<asp:ListItem Text="Item1" Value="1"></asp:ListItem>
<asp:ListItem Text="Item2" Value="2"></asp:ListItem>
<asp:ListItem Text="Item3" Value="3"></asp:ListItem>
</asp:RadioButtonList>
<br />
<asp:ListBox ID="ListBox1" runat="server">
<asp:ListItem Text="Item1" Value="1"></asp:ListItem>
<asp:ListItem Text="Item2" Value="2"></asp:ListItem>
<asp:ListItem Text="Item3" Value="3"></asp:ListItem>
</asp:ListBox>
<br />
<asp:BulletedList ID="BulletedList1" runat="server">
<asp:ListItem Text="Item1" Value="1"></asp:ListItem>
<asp:ListItem Text="Item2" Value="2"></asp:ListItem>
<asp:ListItem Text="Item3" Value="3"></asp:ListItem>
</asp:BulletedList>
Adding ListItems using code. Since all the list controls inherit from ListControl class,
AddListItems() method can be used to add listitems to any list control. A parent class reference
variable can point to a derived class object.
This fact allows us to pass any list control into the AddListItems() method as a parameter. We
have discussed about inheritance in Part 21 and polymorphism in Part 23 of C# Video
Tutorial Series.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
AddListItems(DropDownList1);
AddListItems(CheckBoxList1);
AddListItems(RadioButtonList1);
AddListItems(ListBox1);
AddListItems(BulletedList1);
}
}
private void AddListItems(ListControl listControl)
{
ListItem li1 = new ListItem("Item1", "1");
ListItem li2 = new ListItem("Item2", "2");
ListItem li3 = new ListItem("Item3", "3");
listControl.Items.Add(li1);
listControl.Items.Add(li2);
listControl.Items.Add(li3);
}
ListBox (If SelectionMode=Multiple) and CheckBoxList allows user to select multiple items.
So, to retrieve all the selected listitem's Text, Value and Index use a foreach loop.
Reusable method that can be used with any control that derives from ListControl class,
but works best with controls that allows multiple selections.
private void RetrieveMultipleSelections(ListControl listControl)
{
foreach (ListItem li in listControl.Items)
{
if (li.Selected)
{
Response.Write("Text = " + li.Text + ", Value = " + li.Value +
", Index = " + listControl.Items.IndexOf(li).ToString() + "<br/>");
}
}
}
In this video we will discuss about fileupload control. FileUpload control is a combination of
a text box and a browse button that enable users to select a file to upload to the server.
Create an asp.net web application project. Drag and drop the FileUpload control on the
webform.
The fileUpload control only allows the user to select the file. To upload the seleceted file,
drag and drop a button control. Change the ID of the button to btnUpload and the Text to
Upload File. Also drag and drop a label control, and change the ID of the label to lblMessage. At
this stage the HTML of the webform should be as shown below.
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="btnUpload" runat="server" Text="Upload File"
onclick="btnUpload_Click" />
<br />
<asp:Label ID="lblMessage" Font-Bold="true" runat="server">
</asp:Label>
Right click on the web application project and add a folder with name Uploads. This folder
is going to store all the uploaded files.
In this video we will learn about using the asp.net adrotator control. Adrotator control is
used to display random ads. The ads information can be stored in an xml file or in a database
table. In this video we will discuss about using an XML file.
Create an asp.net web application project, and add an XML file. Name the XML file as
AdsData.xml. Copy and paste the following in the XML file.
<?xml version="1.0" encoding="utf-8" ?>
<Advertisements>
<Ad>
<ImageUrl>~/Images/Google.png</ImageUrl>
<NavigateUrl>http://google.com</NavigateUrl>
<AlternateText>Please visit http://www.Google.com</AlternateText>
<Impressions>10</Impressions>
</Ad>
<Ad>
<ImageUrl>~/Images/Pragim.png</ImageUrl>
<NavigateUrl>http://pragimtech.com</NavigateUrl>
<AlternateText>Please visit http://www.pragimtech.com</AlternateText>
<Impressions>20</Impressions>
</Ad>
<Ad>
<ImageUrl>~/Images/Youtube.png</ImageUrl>
<NavigateUrl>http://Youtube.com</NavigateUrl>
<AlternateText>Please visit http://www.Youtube.com</AlternateText>
<Impressions>40</Impressions>
</Ad>
</Advertisements>
To open the target web page in a separate browser window, set Target="_blank"
In this video we will learn about the asp.net calendar control. Any time you want your users
of the application, to provide a date, it is better to provide a calendar control from which they can
select the date. In this session we will see how to do it.
Drag and drop a TextBox, ImageButton and a Calendar control on the webform. Create an
Image folder and add the following Calendar.png to the Images folder.
Output:
Output
If the SelectionMode is set to Day, then the user can select only one day. In this case to
retrieve the selected date, we use SelectedDate property of the calendar as shown below.
Response.Write(Calendar1.SelectedDate.ToShortDateString());
Events:
SelectionChanged - Fires when the date,week or Month selection is changed, by the user.
protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{
foreach (DateTime selectedDate in Calendar1.SelectedDates)
{
Response.Write(selectedDate.ToShortDateString() + "<br/>");
}
}
SQL Script
Create Table tblEmployees
(
Id int Primary Key,
Name nvarchar(50),
Gender nvarchar(10),
DeptName nvarchar(10)
)
cmd.Parameters.AddWithValue("@Name", txtName.Text);
cmd.Parameters.AddWithValue("@Gender", txtGender.Text);
cmd.Parameters.AddWithValue("@DeptName", txtDept.Text);
cmd.Parameters.AddWithValue("@Id", HiddenField1.Value);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
txtName.Text = "";
txtDept.Text = "";
txtGender.Text = "";
}
}
HiddenField:
1. Value property of the HiddenFiled is used to Get or set the value.
2. The value is stored as string
3. ViewState uses HiddenFiled to maintain state across postback
4. HiddenField is rendered as an <input type= "hidden"/> element
Advantages of HiddenField:
HiddenFiled data is lost when you navigate away from the page. Doesn't require any explicit
cleanup task.
HiddenField is accessible to client-side scripts
<script type="text/javascript">
function GetHiddenFieldValue()
{
alert(document.getElementById('HiddenField1').value);
}
</script>
Disadvantage of HiddenField:
Hidden field data can be seen, by viewing the page source. Never, use HiddenFiled to store
confidential data
In this video we will discuss about the Multiview and View controls in asp.net. As the
name states, a multiview is made up of multiple view controls, and each view control inturn can
have controls inside it.
The HTML below shows a multiview, with 3 views
<asp:MultiView ID="MultiView1" runat="server">
<asp:View ID="View1" runat="server">
</asp:View>
<asp:View ID="View2" runat="server">
</asp:View>
<asp:View ID="View3" runat="server">
</asp:View>
</asp:MultiView>
Let's create a simple example, where we want to capture employee information on a step by
step basis.
1. First capture Employee Personal details
2. Next capture Employee contact details
3. Show summary for confirmation. Upon confirmation, save the data to a database table
Code-Behind Page:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
multiViewEmployee.ActiveViewIndex = 0;
}
}
lblEmail.Text = txtEmail.Text;
lblMobile.Text = txtMobile.Text;
multiViewEmployee.ActiveViewIndex = 2;
}
ActiveViewIndex property of the Multiview control is used to determine, the view that is visible
or active.
This can also be achieved using the wizard control. We will discuss about wizard control in the
next video session.
Another way to achieve this, is by creating multiple webforms and passing data between
webforms using
1. Cookies
2. Query Strings
3. Session variables
We will discuss about these different techniques and their advantages and disadvantages in a
later video session.
In this video we will discuss about Wizard control. Wizard control enables creation of multi-
step user interface. Wizard control provides with built-in previous/next functionality.
Let's create a simple example, where we want to capture employee information on a step by
step basis.
1. First capture Employee Personal details
2. Next capture Employee contact details
3. Show summary for confirmation. Upon confirmation, save the data to a database table
We have already discussed how to achieve this using MultiView control in Part 35 of this video
series.
lblEmail.Text = txtEmail.Text;
lblMobile.Text = txtMobile.Text;
}
}
Properties:
ActiveStepIndex - Used to set or get the ActiveStepIndex of the wizard control
DisplayCancelButton - Determines the visibility of the cancel button in the wizard control
CancelButtonImageUrl - If the cancel button type is set to Image button. Then set this property
to specify the image.
CancelButtonStyle - The style properties to customize the cancel button
CancelButtonText - If the cancel button type is set to Link or Button, then set this property to
specify the Text of the button.
CancelButtonType - Use to Specify the type of cancel button. This can be Button, Image or
Link.
CancelDestinationPageUrl - The destination page to redirect to, when the cancel button is
clicked. This can be a page with in the application or an external website.
Note: WizardSteps can be added in the HTML source or using the WizardSteps collection
editor.
Drag and drop a wizard control on the webform. Right click on the wizard control and select
the properties. Click on the events icon. This displays all the events of the wizard control.
To generate the event handler method for ActiveStepChanged event, double click on the
textbox next to the event. Follow the same process to generate the event handler methods for
the rest of the events of the wizard control.
In this video we will discuss about the UseSubmitBehavior property of the asp.net Button
control. Let us understand the use of this property with an example.
Design a webform with a TextBox, Label and 2 Button controls as shown in the image
below.
For your convinience, I have included the HTML of the aspx page.
<div style="font-family: Arial">
<strong>Name : </strong>
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<br />
<br />
<asp:Button ID="btnClear" runat="server" onclick="btnClear_Click" Text="Clear"/>
<asp:Button ID="btnSubmit" runat="server" onclick="btnSubmit_Click" Text="Submit" />
<br />
<br />
<asp:Label ID="lblMessage" runat="server" Font-Bold="True"
ForeColor="#009933"></asp:Label>
</div>
Dobule click the Clear and Submit buttons, to generate the event handlers, and paste the
following code in the code behind page.
protected void btnSubmit_Click(object sender, EventArgs e)
{
lblMessage.Text = "You enetered: " + txtName.Text;
}
Now, run the web application project. Enter a name in the textbox and hit the enter key on
the keyboard. Notice that the cancel button has the submit behaviour.
In the HTML of the aspx page, set UseSubmitBehavior="false" for the clear button.
<asp:Button ID="btnClear" UseSubmitBehavior="false" runat="server"
onclick="btnClear_Click" Text="Clear"/>
Now, run the web application project again. Enter a name in the textbox and hit the enter key on
the keyboard. Notice that the submit button has the submit behaviour, as expected.
The UseSubmitBehavior property specifies if the Button control uses the browser's built-in
submit function or the ASP.NET postback mechanism.
This property is TRUE by default. When set to FALSE, ASP.NET adds a client-side script to
post the form. To view the client side script added by the ASP.NET, right click on the broswer
and view source.
In Part 36, of this video series we have discussed about an example that makes use of the
wizard control. In this video, we will look at the advanced features of the wizard control like
1. Set focus to the first control in the wizard step when the page loads, so that the user can start
typing into the textbox directly.
2. Attach javascript to the Next, Previous and Finish buttons, to display a confirmation dialog
box before the user moves to the next step.
3. Setting UseSubmitBehavior="true" for the Previous button in the wizard control.
In the HTML below, we have a wizard control with 3 steps. Each step has a TextBox
control.
<asp:Wizard ID="Wizard1" runat="server">
<WizardSteps>
<asp:WizardStep ID="WizardStep1" runat="server" Title="Step 1">
<asp:TextBox ID="Step1TextBox" runat="server"></asp:TextBox>
</asp:WizardStep>
<asp:WizardStep ID="WizardStep2" runat="server" Title="Step 2">
<asp:TextBox ID="Step2TextBox" runat="server"></asp:TextBox>
</asp:WizardStep>
<asp:WizardStep ID="WizardStep3" runat="server" Title="Step 3">
<asp:TextBox ID="Step3TextBox" runat="server"></asp:TextBox>
</asp:WizardStep>
</WizardSteps>
</asp:Wizard>
The following code, sets the focus to the correct textbox, based on the ActiveStepIndex.
Make sure you have this code in the Page_PreRender event. Copying this code in
the Page_Load() event will not work correctly. This is because the ActiveStepIndex is changed
on the Button click event which happens after the Page_Load() event. As
the Page_PreRender() events occurs after the Button_Click event, the code works correctly
as expected.
protected void Page_PreRender(object sender, EventArgs e)
{
if (Wizard1.ActiveStepIndex == 0)
{
Step1TextBox.Focus();
}
else if (Wizard1.ActiveStepIndex == 1)
{
Step2TextBox.Focus();
}
else if (Wizard1.ActiveStepIndex == 2)
{
Step3TextBox.Focus();
}
}
To attach, javascript to the buttons in the navigation bar(next, previous, Finish), we need
to use Navigation Templates. By default, the wizard control generates these buttons
automatically. To make the wizard control use navigation templates and attach javascript
1. Right click on the wizard control and select "Show smart tag"
2. Click on "Convert To Start Navigation Template".
3. Now in the HTML source, specify the javascript that needs to be executed in response to the
OnClientClick event.
<asp:Wizard ID="Wizard1" runat="server">
<StartNavigationTemplate>
<asp:Button ID="StartNextButton" runat="server" CommandName="MoveNext"
Text="Next"
OnClientClick="return confirm('Are you sure you want to go to next step');" />
</StartNavigationTemplate>
<WizardSteps>
<asp:WizardStep ID="WizardStep1" runat="server" Title="Step 1">
<asp:TextBox ID="Step1TextBox" runat="server"></asp:TextBox>
</asp:WizardStep>
<asp:WizardStep ID="WizardStep2" runat="server" Title="Step 2">
<asp:TextBox ID="Step2TextBox" runat="server"></asp:TextBox>
</asp:WizardStep>
<asp:WizardStep ID="WizardStep3" runat="server" Title="Step 3">
<asp:TextBox ID="Step3TextBox" runat="server"></asp:TextBox>
</asp:WizardStep>
</WizardSteps>
</asp:Wizard>
Use StepNavigationTemplate, to attach javascript to the Next and Previous buttons on all
wizard steps, where StepType="Step"
Use FinishNavigationTemplate, to attach javascript to the Finish button on the last step of the
wizard control.
It is also possible to add javascript in code. To add the javascript using code for the Next button,
in a wizard step, where StepType="Step"
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Button btnNext =
(Button)Wizard1.FindControl("StepNavigationTemplateContainerID").FindControl("StepNextButt
on");
btnNext.Attributes.Add("onclick", "return confirm('Are you sure you want to move to the
next step');");
}
}
To make the next button of the wizard control the default button,
set UseSubmitBehavior=False for the Previous button. So that, when the user hits the Enter
key, after entering the required data, the user moves to the next step and not the previous step.
In this video we will discuss about the Literal control. In my opinion this is the least
understood control in asp.net.
Topics Discussed
a) Literal control in asp.net
b) Difference between a literal control and label control
1. In many ways a Literal control is similar to a Label control. Both of these controls are used to
display Text on a webform. The Text property can be set in the HTML or in the code-behind.
2. Label control wraps the text in a span tag when rendered. Any style that is applied to the
Label control, will be rendered using the style property of the span tag.
Will be rendered as
<span id="Label1" style="color:Red;font-weight:bold;">Lable Text</span>
3. A literal control, doesn't output any surrounding tags. The Text is displayed as is.
For example, the following HTML
<asp:Literal ID="Literal1" runat="server"
Text="Literal Control Text"></asp:Literal>
will be rendered as
Literal Control Text
4. If you want to apply any styles to a literal control, include them in the Text of the literal
control. For example, the following HTML sets the font to red color and bold.
<asp:Literal ID="Literal1" runat="server"
Text="<b><font color='Red'>Literal Control Text</font></b>">
</asp:Literal>
5. So, if you just want the text to be displayed without any styles, then use Literal control, else
use Label control.
6. By default, both the Label and Literal Control's does not encode the text they display. For
example, the following HTML displays a javascript alert
<asp:Label ID="Label" runat="server"
Text="<script>alert('Lable Text');</script>">
</asp:Label>
<br />
<asp:Literal ID="Literal1" runat="server"
Text="<script>alert('Literal Text');</script>">
</asp:Literal>
7. To HTML encode the Label Text, Server.HtmlEncode() method can be used, and for Literal
control, Mode property can be used.
<asp:Label ID="Label1" runat="server">
<%=Server.HtmlEncode("<script>alert('Lable Text');</script>")%>
</asp:Label>
<br />
<asp:Literal ID="Literal1" runat="server"
Text="<script>alert('Literal Text');</script>"
Mode="Encode"></asp:Literal>
8. Literal control is a light weight control, when compared with the Label control.
9. The inheritance hierarchy for Literal control class is (Object => Control => Literal), where as
for the Lable control, the hierarchy is (Object => Control => WebControl=> Label)
Asp.net panel control - Part 42
Suggested Videos
Part 39 - UseSubmitBehavior property of the Button control
Part 40 - Asp.net wizard control templates
Part 41 - Literal control in asp.net
The panel control is used as a container for other controls. A panel control is very handy,
when you want to group controls, and then show or hide, all the controls in the group. Panel
control, is also very useful, when adding controls to the webform dynamically. We will discuss
about, adding controls dynamically using panel control in the next video session.
In this video, we will discuss about, using the Panel control to group controls, and then toggle
their visibility, using the Panel control's Visible property.
The following webform is used by both, an Admin and Non-Admin user. When the Admin user
is selected from the dropdownlist, we want to show the controls that are relevant to the Admin
user. When the Non-Admin user is selected, only the Non-Admin specific content and controls
should be shown.
HTML of the ASPX page. At the moment we are not using Panel control.
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true"
onselectedindexchanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem Text="Select User" Value="-1"></asp:ListItem>
<asp:ListItem Text="Admin" Value="Admin"></asp:ListItem>
<asp:ListItem Text="Non-Admin" Value="Non-Admin"></asp:ListItem>
</asp:DropDownList>
<table>
<tr>
<td colspan="2">
<asp:Label ID="AdminGreeting" runat="server" Font-Size="XX-Large"
Text="You are logged in as an administrator">
</asp:Label>
</td>
</tr>
<tr>
<td>
<asp:Label ID="AdminNameLabel" runat="server" Text="Admin Name:">
</asp:Label>
</td>
<td>
<asp:TextBox ID="AdminNameTextBox" runat="server" Text="Tom">
</asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="AdminRegionLabel" runat="server" Text="Admin Region:">
</asp:Label>
</td>
<td>
<asp:TextBox ID="AdminRegionTextBox" runat="server" Text="Asia">
</asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="AdminActionsLabel" runat="server" Text="Actions:">
</asp:Label>
</td>
<td>
<asp:TextBox ID="AdminActionsTextBox" runat="server" Font-Size="Medium"
TextMode="MultiLine"
Text="There are 4 user queries to be answered by the end of Dcemeber 25th 2013."
Font-Bold="True" ></asp:TextBox>
</td>
</tr>
</table>
<table>
<tr>
<td colspan="2">
<asp:Label ID="NonAdminGreeting" runat="server" Font-Size="XX-Large"
Text="Welcome Tom!">
</asp:Label>
</td>
</tr>
<tr>
<td>
<asp:Label ID="NonAdminNameLabel" runat="server" Text="User Name:">
</asp:Label>
</td>
<td>
<asp:TextBox ID="NonAdminNameTextBox" runat="server" Text="Mike">
</asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="NonAdminRegionLabel" runat="server" Text="User Region:">
</asp:Label>
</td>
<td>
<asp:TextBox ID="NonAdminRegionTextBox" runat="server" Text="United Kingdom">
</asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="NonAdminCityLabel" runat="server" Text="City:">
</asp:Label>
</td>
<td>
<asp:TextBox ID="NonAdminCityTextBox" runat="server" Text="London">
</asp:TextBox>
</td>
</tr>
</table>
Code-Behind code. Since we are not using the panel control, each control's visible
property need to be changed depending on the selection in the dropdownlist.
protected void Page_Load(object sender, EventArgs e)
{
// When the page first loads, hide all admin and non admin controls
if (!IsPostBack)
{
HideAdminControls();
HideNonAdminControls();
}
}
HTML of the ASPX page. The panel control is used to group the controls.
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true"
onselectedindexchanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem Text="Select User" Value="-1"></asp:ListItem>
<asp:ListItem Text="Admin" Value="Admin"></asp:ListItem>
<asp:ListItem Text="Non-Admin" Value="Non-Admin"></asp:ListItem>
</asp:DropDownList>
<asp:Panel ID="AdminPanel" runat="server">
<table>
<tr>
<td colspan="2">
<asp:Label ID="AdminGreeting" runat="server" Font-Size="XX-Large"
Text="You are logged in as an administrator">
</asp:Label>
</td>
</tr>
<tr>
<td>
<asp:Label ID="AdminNameLabel" runat="server" Text="Admin Name:">
</asp:Label>
</td>
<td>
<asp:TextBox ID="AdminNameTextBox" runat="server" Text="Tom">
</asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="AdminRegionLabel" runat="server" Text="Admin Region:">
</asp:Label>
</td>
<td>
<asp:TextBox ID="AdminRegionTextBox" runat="server" Text="Asia">
</asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="AdminActionsLabel" runat="server" Text="Actions:">
</asp:Label>
</td>
<td>
<asp:TextBox ID="AdminActionsTextBox" runat="server" Font-Size="Medium"
TextMode="MultiLine"
Text="There are 4 user queries to be answered by the end of Dcemeber 25th 2013."
Font-Bold="True" ></asp:TextBox>
</td>
</tr>
</table>
</asp:Panel>
<asp:Panel ID="NonAdminPanel" runat="server">
<table>
<tr>
<td colspan="2">
<asp:Label ID="NonAdminGreeting" runat="server" Font-Size="XX-Large"
Text="Welcome Tom!">
</asp:Label>
</td>
</tr>
<tr>
<td>
<asp:Label ID="NonAdminNameLabel" runat="server" Text="User Name:">
</asp:Label>
</td>
<td>
<asp:TextBox ID="NonAdminNameTextBox" runat="server" Text="Mike">
</asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="NonAdminRegionLabel" runat="server" Text="User Region:">
</asp:Label>
</td>
<td>
<asp:TextBox ID="NonAdminRegionTextBox" runat="server" Text="United Kingdom">
</asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="NonAdminCityLabel" runat="server" Text="City:">
</asp:Label>
</td>
<td>
<asp:TextBox ID="NonAdminCityTextBox" runat="server" Text="London">
</asp:TextBox>
</td>
</tr>
</table>
</asp:Panel>
Code-Behind code:
protected void Page_Load(object sender, EventArgs e)
{
// When the page first loads, hide admin and non admin panels
if (!IsPostBack)
{
AdminPanel.Visible = false;
NonAdminPanel.Visible = false;
}
}
The panel control is used as a container for other controls. A panel control is very handy, when
you want to group controls, and then show or hide, all the controls in the group. We have seen
how to do this in Part 42, of this video series.
Panel control, is also very useful, when adding controls to the webform dynamically. We
will discuss this in this session.
HTML of the ASPX page
<div style="font-family: Arial">
<table>
<tr>
<td><b>Control Type</b></td>
<td>
<asp:CheckBoxList ID="chkBoxListControlType" runat="server"
RepeatDirection="Horizontal">
<asp:ListItem Text="Label" Value="Label"></asp:ListItem>
<asp:ListItem Text="TextBox" Value="TextBox"></asp:ListItem>
<asp:ListItem Text="Button" Value="Button"></asp:ListItem>
</asp:CheckBoxList>
</td>
<td><b>How Many</b></td>
<td>
<asp:TextBox ID="txtControlsCount" runat="server" Width="40px"></asp:TextBox>
</td>
<td>
<asp:Button ID="btnGenerateControl" runat="server" Text="Generate Controls"
onclick="btnGenerateControl_Click" />
</td>
</tr>
<tr>
<td colspan="5">
<h3>Label Controls</h3>
</td>
</tr>
<tr>
<td colspan="5" id="tdLabels" runat="server">
<asp:Panel ID="pnlLabels" runat="server">
</asp:Panel>
<%--<asp:PlaceHolder ID="phLabels" runat="server">
</asp:PlaceHolder>--%>
</td>
</tr>
<tr>
<td colspan="5">
<h3>TextBox Controls</h3>
</td>
</tr>
<tr>
<td colspan="5" id="tdTextBoxes" runat="server">
<asp:Panel ID="pnlTextBoxes" runat="server">
</asp:Panel>
<%--<asp:PlaceHolder ID="phTextBoxes" runat="server">
</asp:PlaceHolder>--%>
</td>
</tr>
<tr>
<td colspan="5">
<h3>Button Controls</h3>
</td>
</tr>
<tr>
<td colspan="5" id="tdButtons" runat="server">
<asp:Panel ID="pnlButtons" runat="server">
</asp:Panel>
<%--<asp:PlaceHolder ID="phButtons" runat="server">
</asp:PlaceHolder>--%>
</td>
</tr>
</table>
</div>
Code-Behind Code:
protected void btnGenerateControl_Click(object sender, EventArgs e)
{
// Retrieve the count of the controls to generate
int Count = Convert.ToInt16(txtControlsCount.Text);
// Loop thru each list item in the CheckBoxList
foreach (ListItem li in chkBoxListControlType.Items)
{
if (li.Selected)
{
// Generate Lable Controls
if (li.Value == "Label")
{
for (int i = 1; i <= Count; i++)
{
Label lbl = new Label();
lbl.Text = "Label - " + i.ToString();
//phLabels.Controls.Add(lbl);
//tdLabels.Controls.Add(lbl);
pnlLabels.Controls.Add(lbl);
}
}
// Generate TextBox controls
else if (li.Value == "TextBox")
{
for (int i = 1; i <= Count; i++)
{
TextBox txtBox = new TextBox();
txtBox.Text = "TextBox - " + i.ToString();
//phTextBoxes.Controls.Add(txtBox);
//tdTextBoxes.Controls.Add(txtBox);
pnlTextBoxes.Controls.Add(txtBox);
}
}
// Generate Button Controls
else
{
for (int i = 1; i <= Count; i++)
{
Button btn = new Button();
btn.Text = "Button - " + i.ToString();
//phButtons.Controls.Add(btn);
//tdButtons.Controls.Add(btn);
pnlButtons.Controls.Add(btn);
}
}
}
}
}
Validation controls are used to ensure if, the data, entered by the user is valid. Microsoft
asp.net framework, provides 6 built-in validation controls.
1. RequiredFieldValidator
2. RangeValidator
3. RegularExpressionValidator
4. CompareValidator
5. CustomValidator
6. ValidationSummary
These validation controls can be used to perform both client side and server side
validation.
Browser's understand only client scripts and HTML. In the past to perform client side
validation, developers had to write themselves the required javascript code. With validation
controls, we don't have to write javascript, we can use the built-in validation controls, which will
generate the required javascript for us.
Client scripts can spread viruses and cause security concerns. Because of this, users may
disable JavaScript on their browser. If this happens, client side validation is skipped. That is
why, it is always a good practice to have server side validation as well.
The validation control's also perform server side validation. Server side validation is always
performed, irrespective of whether the client side validation is done or not.
In this video we will discuss about RequiredField validator control. This control, ensures
that the required field is entered by the user. Let us understand with an example.
Consider the HTML below. TextBox with ID="txtName" expects the user to enter their name.
This is required field. Next to this textbox is a RequiredFieldValidator control, which is used to
ensure that the user has entered his name. ControlToValidate property specifies the control to
validate. ErrorMessage is the message that is displayed when the validation fails. To
validate ddlGender DropDownList, we have RequiredFieldValidatorGender. We have
specified InitialValue="-1". This will ensure that, if the user has selected, "Select Gender"
option from the DropDownList, the control will still fail the validation.
<table>
<tr>
<td>
<b>Name</b>
</td>
<td>
:<asp:TextBox ID="txtName" runat="server" Width="150px">
</asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidatorName" runat="server"
ErrorMessage="Name is required" ForeColor="Red"
ControlToValidate="txtName" Display="Dynamic" >
</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
<b>Gender</b>
</td>
<td>
:<asp:DropDownList ID="ddlGender" runat="server" Width="150px">
<asp:ListItem Text="Select Gender" Value="-1"></asp:ListItem>
Code-Behind code: Page.IsValid is a read-only property. This property returns true if all the
validation controls has passed validation. Use this property to perform server side validation.
protected void btnSave_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
lblStatus.ForeColor = System.Drawing.Color.Green;
lblStatus.Text = "Data saved successfully!";
}
else
{
lblStatus.ForeColor = System.Drawing.Color.Red;
lblStatus.Text = "Data not valid and not saved!";
}
}
Run the project, and notice that the client side validation is performed. Now, disable
Javascript. To disable javascript in, internet explorer
1. Click on Tools on the Menu Bar. If the Menu bar is not visible, press ALT+T.
2. From the Tools menu, select Internet Options
3. Click on the Security tab, on the internet options window
4. Select Local Intranet icon and click on Cutom Level button
5. Scroll down in the Security Settings window, and find Scripting option in the list.
6. Click Disable radiobutton under active scripting
7. Click OK and click Yes on the warning.
The above steps should disable javascript. Run the project now, and click Save button,
without entering any data. Notice, that the client side validation is skipped, but the server side
validation is performed.
In this video we will discuss about Rangevalidator control. This control is used to check if
the value is within a specified range of values. For example, Rangevalidator can be used to
check if the age falls between 1 and 100.
In the HTML below, TextBox txtAge captures age of the person. If the user enter's any
number that is not between 1 & 100 the validation fails. The minimum and maximum value for
the age is specified by MinimumValue and MaximumValue properties. Since, age is an
integer, the Type is specified as integer.
<asp:TextBox ID="txtAge" runat="server"></asp:TextBox>
<asp:RangeValidator ID="RangeValidator1" runat="server"
ErrorMessage="Age must be between 1 & 100"
MinimumValue="1" MaximumValue="100"
ControlToValidate="txtAge" Type="Integer" >
</asp:RangeValidator>
Properties specific to Rangevalidator control:
Type - This property, specifies the data type of the value to check. Data types supported include
- Currency, Date, Double, Integer, String.
MinimumValue - The minimum value allowed
MaximumValue - The maximum value allowed
Comple HTML of the aspx page used in the Demo: Rangevalidator only checks if the
entered data is within the allowed range. If you want to check for a required field,
use RequiredFieldValidator. For the age field, we are using
both RequiredFieldValidator and RangeValidator. Also notice that, in this example we are
using the Display property. If the Display property is not set, or, if it is set to static, then the
error message will be rendered, with style visibility:hidden. Because of this, the error message
will always occupy the space on the screen even if the validation passes. This pushes "Age is
Required" error message to the right. To correct this we have set Display="Dynamic". This
renders the error message with style display:none. If a tag has this style, it will not occupy
space when not visible.
<table>
<tr>
<td>
<b>Age</b>
</td>
<td>
:<asp:TextBox ID="txtAge" runat="server" Width="150px">
</asp:TextBox>
<asp:RangeValidator ID="RangeValidatorAge" runat="server"
ErrorMessage="Age must be between 1 & 100"
MinimumValue="1" MaximumValue="100"
ControlToValidate="txtAge" Type="Integer"
ForeColor="Red" Display="Dynamic">
</asp:RangeValidator>
<asp:RequiredFieldValidator ID="RequiredFieldValidatorAge"
runat="server" ErrorMessage="Age is required"
ControlToValidate="txtAge" ForeColor="Red"
Display="Dynamic" >
</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
<b>Date Available</b>
</td>
<td>
:<asp:TextBox ID="txtDateAvailable" runat="server" Width="150px">
</asp:TextBox>
<asp:RangeValidator ID="RangeValidatorDateAvailable" runat="server"
ErrorMessage="Date must be between 01/01/2012 & 31/12/2012"
MinimumValue="01/01/2012" MaximumValue="31/12/2012"
ControlToValidate="txtDateAvailable" Type="Date"
ForeColor="Red">
</asp:RangeValidator>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="btnSave" runat="server" Text="Save" Width="100px"
onclick="btnSave_Click" />
</td>
</tr>
<tr>
<td colspan="2">
<asp:Label ID="lblStatus" runat="server" Font-Bold="true">
</asp:Label>
</td>
</tr>
</table>
Static - The error message is displayed next to the control if validation fails. Space is reserved
on the page for the message even if validation succeeds. The span tag is rendered with style
visibility:hidden
Dynamic - The error message is displayed next to the control if validation fails. Space is not
reserved on the page for the message if the validation succeeds. The span tag is rendered with
style display:none.
CompareValidator control is used to compare the value of one control with the value of
another control or a constant value. The comparison operation can be any of the
following.
1. Equal
2. GreaterThan
3. GreaterThanEqual
4. LessThan
5. LessThanEqual
6. NotEqual
7. DataTypeCheck
The following are the properties that are specific to the compare validator
1. ControlToCompare - The control with which to compare
2. Type - The DataType of the value to compare. String, Integer etc.
3. Operator = The comparison operator. Equal, NotEqual etc.
4. ValueToCompare - The constant value to compare with.
Using CompareValidator to compare the value of one control with the value of another
control.
<table>
<tr>
<td>
<b>Password</b>
</td>
<td>
:<asp:TextBox ID="txtPassword" runat="server" Width="150px"
TextMode="Password"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<strong>Retype Password</strong></td>
<td>
:<asp:TextBox ID="txtRetypePassword" runat="server"
Width="150px" TextMode="Password"></asp:TextBox>
<asp:CompareValidator ID="CompareValidatorPassword" runat="server"
ErrorMessage="Password and Retype Password must match"
ControlToValidate="txtRetypePassword" ControlToCompare="txtPassword"
Type="String" Operator="Equal" ForeColor="Red">
</asp:CompareValidator>
</td>
</tr>
</table>
Using CompareValidator to compare the value of one control with a constant value.
<asp:TextBox ID="txtDateofapplication" runat="server" Width="150px">
</asp:TextBox>
<asp:CompareValidator ID="CompareValidatorDateofbirth" runat="server"
ErrorMessage="Date of application must be greater than 01/01/2012"
ControlToValidate="txtDateofapplication" ValueToCompare="01/01/2012"
Type="Date" Operator="GreaterThan" ForeColor="Red"
SetFocusOnError="true"></asp:CompareValidator>
This is a very powerful validation control. This control is used to check if the value of an
associated input control matches the pattern specified by a regular expression. The only
property that is specific to the RegularExpressionValidator is ValidationExpression.
This example, checks if the input mataches the pattern of an email address
<asp:TextBox ID="txtEmail" runat="server" Width="150px">
</asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidatorEmail" runat="server"
ControlToValidate="txtEmail" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"
ErrorMessage="Invalid Email" ForeColor="Red"></asp:RegularExpressionValidator>
By default client side validation is turned on. To disable client side validation
set EnableClientScript o false. This property is supported by all validation controls.
CustomValidator control allows us to write a method with a custom logic to handle the
validation of the value entered. If none of the other validation controls, serve our purpose,
then the CustomValidator can be used.
Just like any other validation control, CustomValidator can be used for both client and server
side validation. Client side and server side validation functions, needs to be written by the
developer, and associate them to the custom validator control.
The following are the properties that are specific to the CustomValidator control
ClientValidationFunction - Specifies the name of the client side validation method.
ValidateEmptyText - Specifies whether the validator validates the control, when the text of the
control is empty. By default this property is false, and both the client side and server side
validation functions will not be invoked, if the associated input control is empty.
HTML of the aspx page: This example checks if the entered number is
even. ValidateEmptyText property of the CustomValidator control, is set to true. So the
validation is also triggered when the textbox is empty.
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script language="javascript" type="text/javascript">
// Client side validation function to check if the number is even.
function IsEven(source, args)
{
if (args.Value == "")
{
args.IsValid = false;
}
else
{
if (args.Value % 2 == 0)
{
args.IsValid = true;
}
else
{
args.IsValid = false;
}
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div style="font-family: Arial">
<table>
<tr>
<td>
<b>Please enter a positive even number</b>
</td>
<td>
<asp:TextBox ID="txtEvenNumber" runat="server"></asp:TextBox>
<asp:CustomValidator ID="CustomValidatorEvenNumber" runat="server"
ForeColor="Red"
ErrorMessage="Not an even number"
OnServerValidate="CustomValidatorEvenNumber_ServerValidate"
ControlToValidate="txtEvenNumber"
ClientValidationFunction="IsEven"
ValidateEmptyText="true">
</asp:CustomValidator>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="btnSubmit" runat="server" Text="Save"
onclick="btnSubmit_Click" />
</td>
</tr>
<tr>
<td colspan="2">
<asp:Label ID="lblStatus" runat="server" Font-Bold="true"></asp:Label>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
Code-Behind page code: Set EnableClientScript to true, to test the server side validation
method.
protected void btnSubmit_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
lblStatus.Text = "Data Saved";
lblStatus.ForeColor = System.Drawing.Color.Green;
}
else
{
lblStatus.Text = "Validation Error! Data Not Saved";
lblStatus.ForeColor = System.Drawing.Color.Red;
}
}
protected
void CustomValidatorEvenNumber_ServerValidate(object source, ServerValidateEventArgs arg
s)
{
if (args.Value == "")
{
args.IsValid = false;
}
else
{
int Number;
bool isNumber = int.TryParse(args.Value, out Number);
if (isNumber && Number >= 0 && (Number % 2) == 0)
{
args.IsValid = true;
}
else
{
args.IsValid = false;
}
}
}
The first problem here is that, when I click the Clear button, Form validation still happens.
When I click the clear button, I just want to clear the textboxes in the Registration section.
Validations doesn't make any sense here. So, how do I prevent validation from happening?
Just, set the CausesValidation property of the button control to false.
The second problem, is that when I click the Login button, only fields in the Login
section(UserName and Password) needs to be validated. Along the same lines when I click
the "Register" button, only fields in the Registration section(Email, UserName, Password
and ConfirmPassword) needs to validated. If we don't use validation groups, then by default,
whenever, you click a button, all the validation controls on the page get validated.
So, when you click the login button, and if you want only, the fields in the Login
section(UserName and Password) to be validated, then set, the ValidationGroup property of
the validation controls and the login button control to the same group name. Use a different
group name for the validation controls and register button, in the registration section.
<asp:Button ID="btnClear" runat="server" onclick="btnClear_Click"
CausesValidation="false" Text="Clear" />
</td>
</tr>
<tr>
<td colspan="2">
<asp:ValidationSummary ID="ValidationSummary1" runat="server"
ForeColor="Red" HeaderText="Page Errors" ShowMessageBox="True"
ShowSummary="true" DisplayMode="List"
ValidationGroup="Registration"/>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Label ID="lblStatus" runat="server"
Font-Bold="true"></asp:Label>
</td>
</tr>
</table>
</td>
</tr>
</table>
</div>
Code-Behind:
protected void btnSubmit_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
lblStatus.Text = "No registration validation Errors";
lblStatus.ForeColor = System.Drawing.Color.Green;
}
else
{
lblStatus.Text = "Registration validation Errors";
lblStatus.ForeColor = System.Drawing.Color.Red;
}
}
This is a very common interview question in asp.net. There are several techniques to
navigate between webforms in asp.net as listed below.
1. Hyperlink control - Is used to navigate to another page. The page you want to navigate to is
specified by the NavigateURL property. Using hyperlink, you can navigate to another page with
in the same application or to an external web site. The hyperlink control is rendered as an HTML
anchor tag. We have discussed in detail about the HyperLink control in Part 13 of ASP.NET
video series.
2. Response.Redirect
3. Server.Transfer
4. Server.Execute
5. Cross-Page postback
6. Window.Open
We will discuss about the rest of the page navigation techniques in the subsequent videos.
Response.Redirect in asp.net - Part 52
Suggested Videos
Part 49 - ValidationSummary
Part 50 - ValidationGroups
Part 51 - Different page navigation techniques
1. Hyperlink control - Discussed in Part 13 and Part 51 of the ASP.NET video series
2. Response.Redirect
3. Server.Transfer
4. Server.Execute
5. Cross-Page postback
6. Window.Open
So, if you want to intercept a click event in code, use the Button, LinkButton or the
ImageButton server control. In the button click event, call Response.Redirect() method. When
the user clicks the button, the web server receives, a request for redirection. The server then
sends a response header to the client. The client then automatically issues a new GET request
to the web server. The web server will then serve the new page. So, in short,
Response.Redirect causes 2 request/response cycles.
Also, note that when Response.Redirect is used the URL in the address bar changes and the
browser history is maintained.
Create an asp.net web application and add 2 webforms. Copy and paste the following
HTML on WebForm1.aspx
<div style="font-family: Arial">
<table>
<tr>
<td colspan="2">
<h1>
This is WebForm1</h1>
</td>
</tr>
<tr>
<td>
<b>Name</b>
</td>
<td>
:<asp:TextBox ID="txtName" runat="server">
</asp:TextBox>
</td>
</tr>
<tr>
<td>
<b>Email</b>
</td>
<td>
:<asp:TextBox ID="txtEmail" runat="server">
</asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="btnTransfer" runat="server"
Text="Transfer to WebForm2" Width="250px"
OnClick="btnTransfer_Click"/>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="btnTransferToExternalWebsite"
runat="server" Width="250px"
OnClick="btnTransferToExternalWebsite_Click"
Text="Transfer to External WebSite"/>
</td>
</tr>
</table>
</div>
WebForm2.aspx code:
<div>
<table>
<tr>
<td>
<b>Name</b>
</td>
<td>
:<asp:Label ID="lblName" runat="server">
</asp:Label>
</td>
</tr>
<tr>
<td>
<b>Email</b>
</td>
<td>
:<asp:Label ID="lblEmail" runat="server">
</asp:Label>
</td>
</tr>
</table>
</div>
WebForm2.aspx.cs code
protected void Page_Load(object sender, EventArgs e)
{
//Get the form values from the previous page
System.Collections.Specialized.NameValueCollection nameValueCollection =
Request.Form;
lblName.Text = nameValueCollection["txtName"];
lblEmail.Text = nameValueCollection["txtEmail"];
WebForm1.aspx code:
<div style="font-family: Arial">
<table>
<tr>
<td colspan="2">
<h1>
This is WebForm1</h1>
</td>
</tr>
<tr>
<td>
<b>Name</b>
</td>
<td>
:<asp:TextBox ID="txtName" runat="server">
</asp:TextBox>
</td>
</tr>
<tr>
<td>
<b>Email</b>
</td>
<td>
:<asp:TextBox ID="txtEmail" runat="server">
</asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="btnExecute" runat="server"
Text="Server.Execute - WebForm2"
Width="250px" onclick="btnExecute_Click"/>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="btnExecuteToExternalWebsite"
runat="server" Width="250px"
Text="Server.Execute - External WebSite"
onclick="btnExecuteToExternalWebsite_Click" />
</td>
</tr>
<tr>
<td colspan="2">
<asp:Label ID="lblStatus" ForeColor="Green"
Font-Bold="true" runat="server"></asp:Label>
</td>
</tr>
</table>
</div>
WebForm1.aspx.cs code:
protected void btnExecute_Click(object sender, EventArgs e)
{
Server.Execute("~/WebForm2.aspx", true);
lblStatus.Text = "The call returned after processing the second webform";
}
protected void btnExecuteToExternalWebsite_Click(object sender, EventArgs e)
{
Server.Execute("http://pragimtech.com/home.aspx");
}
WebForm2.aspx code:
<div style="font-family: Arial">
<table>
<tr>
<td colspan="2">
<h1>This is WebForm2</h1>
</td>
</tr>
<tr>
<td>
<b>Name</b>
</td>
<td>
:<asp:Label ID="lblName" runat="server">
</asp:Label>
</td>
</tr>
<tr>
<td>
<b>Email</b>
</td>
<td>
:<asp:Label ID="lblEmail" runat="server">
</asp:Label>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="btnPostBack" runat="server"
Text="Simply Post Back" />
</td>
</tr>
</table>
</div>
WebForm2.aspx.cs code:
protected void Page_Load(object sender, EventArgs e)
{
System.Collections.Specialized.NameValueCollection previousFormcollection =
Request.Form;
lblName.Text = previousFormcollection["txtName"];
lblEmail.Text = previousFormcollection["txtEmail"];
In this video we will discuss about Cross page posting. Cross page posting allows to post
one page to another page. By default, when you click a button, the webform posts to itself. If
you want to post to another webform on a button click, set the PostBackUrl of the button, to the
page that you want to post to.
WebForm1.aspx code: Notice that, the PostBackUrl property of the button
with ID=btnCrossPagePostback is set to WebForm2.aspx. When this button is
clicked WebForm1.aspx gets posted to WebForm2.aspx.
<div style="font-family: Arial">
<table>
<tr>
<td colspan="2">
<h1>
This is WebForm1</h1>
</td>
</tr>
<tr>
<td>
<b>Name</b>
</td>
<td>
:<asp:TextBox ID="txtName" runat="server">
</asp:TextBox>
</td>
</tr>
<tr>
<td>
<b>Email</b>
</td>
<td>
:<asp:TextBox ID="txtEmail" runat="server">
</asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="btnCrossPagePostback" runat="server"
Text="Cross Page Postback - WebForm2"
Width="250px" PostBackUrl="~/WebForm2.aspx"/>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="Button1" runat="server" onclick="Button1_Click"
Text="Server.Transfer - WebForm2" Width="250px" />
</td>
</tr>
</table>
</div>
WebForm1.aspx.cs code:
protected void Button1_Click(object sender, EventArgs e)
{
Server.Transfer("~/WebForm2.aspx");
}
WebForm2.aspx code:
<div style="font-family: Arial">
<table>
<tr>
<td colspan="2">
<h1>This is WebForm2</h1>
</td>
</tr>
<tr>
<td>
<b>Name</b>
</td>
<td>
:<asp:Label ID="lblName" runat="server">
</asp:Label>
</td>
</tr>
<tr>
<td>
<b>Email</b>
</td>
<td>
:<asp:Label ID="lblEmail" runat="server">
</asp:Label>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Label ID="lblStatus" runat="server"
ForeColor="Red" Font-Bold="true"></asp:Label>
</td>
</tr>
</table>
</div>
In Part 55, we have discussed the basics of cross page posting. In the previous session,
we used FindControl() method to get a reference to the TextBox on the previous page. The
problem with FindControl() method is that, if we mis-spell the ID of the control, we don't get any
compile time errors, but may cause runtime nullreference exceptions.
In this session, we will discuss about, obtaining a strongly types reference to the
previous page. There are 2 ways to obtain a strongly typed reference. We will explore both of
these options in this part of the video. We will be using the same example used in Part 55.
The first step in obtaining a strongly typed reference, is to create public properties. We
want to convert the values of TextBox controls(txtName and txtEmail) into properties(Name and
Email) respectively. The Name and Email properties are created as Read-Only properties, as
we just need to read the values on the destination page.
//Name - read only property
public string Name
{
get
{
return txtName.Text;
}
}
//Email - read only property
public string Email
{
get
{
return txtEmail.Text;
}
}
The next step is to obtain a strongly typed reference to the previous page and access the
public propertie as shown below. This code must be in the Page_Load event on
WebForm2.aspx.cs. If Name or Email properties are mis-spelled, we get an immediate compile
time error. Hence, strongly typed references can eliminate runtime nullreference exceptions.
protected void Page_Load(object sender, EventArgs e)
{
//Type cast PreviousPage to WebForm1
WebForm1 previousPage = (WebForm1)Page.PreviousPage;
if (previousPage != null && previousPage.IsCrossPagePostBack)
{
//Access the Name and Email public properties
lblName.Text = previousPage.Name;
lblEmail.Text = previousPage.Email;
}
else
{
lblStatus.Text = "You landed on this page using a technique other than cross page post
back";
}
}
PreviousPageType directive can also be used to obtain, a strongly typed reference to the
previous page. In our example, for WebForm2, the previous page is WebForm1. So, in the
HTML source of WebFOrm2.aspx paste the line below, after the Page directive.
<%@ PreviousPageType VirtualPath="~/WebForm1.aspx" %>
So in short to obtain a strongly typed reference, there are 2 very simple steps.
First Step – Create Public Properties (Read-Only is sufficient)
Second Step – Obtain a strongly typed reference by TypeCasting or by using the
PreviousPageType directive.
In this video we will discuss about, opening a new window using javascript method
window.open(). First the syntax of the window.open() method.
window.open(URL, name, features, replace)
HTML of WebForm1.aspx
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Training Demo</title>
<script type="text/javascript">
// Javascript function to open the new window
function OpenNewWindow()
{
var Name = document.getElementById('txtName').value;
var Email = document.getElementById('txtEmail').value;
HTML of WebForm2.aspx
<div style="font-family: Arial">
<table>
<tr>
<td colspan="2">
<h1>This is WebForm2</h1>
</td>
</tr>
<tr>
<td>
<b>Name</b>
</td>
<td>
:<asp:Label ID="lblName" runat="server">
</asp:Label>
</td>
</tr>
<tr>
<td>
<b>Email</b>
</td>
<td>
:<asp:Label ID="lblEmail" runat="server">
</asp:Label>
</td>
</tr>
</table>
</div>
In general, the members of one Web form are unavailable from a subsequently displayed
Web form. However, when navigating between Web forms using the Transfer or Execute
method, data can be retrieve from the previous Web form using Context.Handler object.
WebForm1.aspx.cs Code:
protected void Button1_Click(object sender, EventArgs e)
{
Server.Transfer("~/WebForm2.aspx");
}
WebForm2.aspx.cs Code:
//On postback Context.Handler returns WebForm2
if (!IsPostBack)
{
Page lastpage = (Page)Context.Handler;
if (lastpage is WebForm1)
{
//Use FindControl() if public properties does not exist on the
//previous page(WebForm1). FindControl() may cause
//NullRefernceExceptions due to mis-spelled conrol Id's
//lblName.Text = ((TextBox)lastpage.FindControl("txtName")).Text;
//lblEmail.Text = ((TextBox)lastpage.FindControl("txtEmail")).Text;
Or
&(ampersand) is encoded as %26, so use, Replace() function to replace & with %26
Response.Redirect("WebForm2.aspx?UserName=" + txtName.Text.Replace("&", "%26") +
"&UserEmail=" + txtEmail.Text.Replace("&", "%26"));
WebForm1.aspx HTML: We want to send Name and Email, that user enters on
WebForm1.aspx to WebForm2.aspx using query strings.
<div style="font-family: Arial">
<table>
<tr>
<td colspan="2">
<h1>
This is WebForm1</h1>
</td>
</tr>
<tr>
<td>
<b>Name</b>
</td>
<td>
:<asp:TextBox ID="txtName" runat="server">
</asp:TextBox>
</td>
</tr>
<tr>
<td>
<b>Email</b>
</td>
<td>
:<asp:TextBox ID="txtEmail" runat="server">
</asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="btnSendData" runat="server"
Text="Go to WebForm2" onclick="btnSendData_Click" />
</td>
</tr>
</table>
</div>
WebForm1.aspx.cs
protected void btnSendData_Click(object sender, EventArgs e)
{
//Using Server.UrlEncode to encode &(ampersand)
//Response.Redirect("WebForm2.aspx?UserName=" + Server.UrlEncode(txtName.Text) +
// "&UserEmail=" + Server.UrlEncode(txtEmail.Text));
WebForm2.aspx HTML:
<div style="font-family: Arial">
<table>
<tr>
<td colspan="2">
<h1>This is WebForm2</h1>
</td>
</tr>
<tr>
<td>
<b>Name</b>
</td>
<td>
:<asp:Label ID="lblName" runat="server">
</asp:Label>
</td>
</tr>
<tr>
<td>
<b>Email</b>
</td>
<td>
:<asp:Label ID="lblEmail" runat="server">
</asp:Label>
</td>
</tr>
</table>
</div>
WebForm2.aspx.cs Code:
protected void Page_Load(object sender, EventArgs e)
{
// Read the QueryString values
lblName.Text = Request.QueryString["UserName"];
lblEmail.Text = Request.QueryString["UserEmail"];
}
Just like QueryStrings, Cookies can also be used to send data from one webform to
another. In general, web sites use cookies to store user preferences or other information that is
client-specific. Cookies store small amounts of information on the client’s machine.
On WebForm1.aspx, user enters Name and Email. Let's write these values on to the client's
computer using cookies. Finally read the values from the cookie and display them in
WebForm2.aspx.
WebForm1.aspx.cs code:
protected void btnSendData_Click(object sender, EventArgs e)
{
// Create the cookie object
HttpCookie cookie = new HttpCookie("UserDetails");
cookie["Name"] = txtName.Text;
cookie["Email"] = txtEmail.Text;
// Cookie will be persisted for 30 days
cookie.Expires = DateTime.Now.AddDays(30);
// Add the cookie to the client machine
Response.Cookies.Add(cookie);
Response.Redirect("WebForm2.aspx");
}
WebForm2.aspx.cs Code:
protected void Page_Load(object sender, EventArgs e)
{
HttpCookie cookie = Request.Cookies["UserDetails"];
if (cookie != null)
{
lblName.Text = cookie["Name"];
lblEmail.Text = cookie["Email"];
}
}
How to Check if cookies are enabled or disabled - Part 61
Suggested Videos
Part 58 - Techniques to send data from one webform to another
Part 59 - QueryString in asp.net
Part 60 - Cookies in asp.net
How to Check if cookies are enabled or disabled in asp.net? This is what we will discuss in
this video session. Most of the articles on the internet, states we can use
Request.Browser.Cookies property to check, if the cookies are enabled or disabled. This is
incorrect.
if (Request.Browser.Cookies)
{
//Cookies Enabled
}
else
{
//Cookies Disabled
}
The above steps disable cookies, only for the internet zone. If you are testing code on your
local machine, and to disable cookies for localhost
1. Run the application
2. Press F12, to open developer tools
3. Then Select, Cache - Disable Cookies
WebForm1.aspx.cs code:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// Check if the browser supports cookies
if (Request.Browser.Cookies)
{
if (Request.QueryString["CheckCookie"] == null)
{
// Create the test cookie object
HttpCookie cookie = new HttpCookie("TestCookie", "1");
Response.Cookies.Add(cookie);
// Redirect to the same webform
Response.Redirect("WebForm1.aspx?CheckCookie=1");
}
else
{
//Check the existence of the test cookie
HttpCookie cookie = Request.Cookies["TestCookie"];
if (cookie == null)
{
lblMessage.Text = "We have detected that, the cookies are disabled on your
browser. Please enable cookies.";
}
}
}
else
{
lblMessage.Text = "Browser doesn't support cookies. Please install one of the modern
browser's that support cookies.";
}
}
}
Response.Redirect("WebForm2.aspx");
}
WebForm2.aspx.cs Code
protected void Page_Load(object sender, EventArgs e)
{
HttpCookie cookie = Request.Cookies["UserDetails"];
if (cookie != null)
{
lblName.Text = cookie["Name"];
lblEmail.Text = cookie["Email"];
}
}
Asp.net session state - Part 62
Suggested Videos
Part 59 - QueryString in asp.net
Part 60 - Cookies in asp.net
Part 61 - How to Check if cookies are enabled or disabled
Just like Query strings, Session State variables can also be used to send data from one
webform to another.
To turn of the session state at the page level, set EnableSessionState="False" in the page
directive
WebForm1.aspx.cs code:
protected void btnSendData_Click(object sender, EventArgs e)
{
Session["Name"] = txtName.Text;
Session["Email"] = txtEmail.Text;
Response.Redirect("WebForm2.aspx");
}
WebForm2.aspx.cs code:
protected void Page_Load(object sender, EventArgs e)
{
if (Session["Name"] != null)
{
lblName.Text = Session["Name"].ToString();
}
if (Session["Email"] != null)
{
lblEmail.Text = Session["Email"].ToString();
}
}
Cookie less sessions in asp.net - Part 63
Suggested Videos
Part 60 - Cookies in asp.net
Part 61 - How to Check if cookies are enabled or disabled
Part 62 - Asp.net session state
In this session, we will discuss about cookie less sessions in asp.net. By default sessions
use cookies. The session-id is stored as a cookie on the client computer. This session-id, is
then, used by the web-server to identify if the request is coming from the same user or a
different user.
We will be using the same example used in Part 62, to demonstrate cookieless sessions.
Run the application, and navigate to webform1.aspx. I am using google chrome as my default
browser for visual studio. To set google chrome as default browser.
1. Right click on WebForm1.aspx in the solution explorer
2. Select Browse with option
3. From the list select Google chrome
4. Click "Set as Default" button
5. Finally click browse.
At this point, webform1.aspx will be opened using google chrome. Fill in the details
for Name and Email fields, and click "Go To WebForm2" button. To view the session cookie
1. Right click on the browser, and select "Inspect Element"
2. Click on the "Resources" button
3. Expand cookies in the "Resources"
4. Finally, select localhost
5. You should now see a cookie with ASP.NET_SessionId
So, the cookies are disabled now. Run the application, and navigate to WebForm1.aspx.
Fill name and email fields and navigate to WebForm2.aspx. Notice that
the Name and Email fields are not displayed. This is because, cookies are disabled. When
cookies are disabled, the session-id is not sent to the server. So the server has no way to figure
out, if the request for WebForm2.aspx has come from the same user. That is the reason why
these fields are not displayed on WebForm2.aspx.
Some of the users, does not like websites writing information to their computers. So it is
very common for, users to disable cookies. If that is the case, then websites using cookies, to
manage sessions may not work as expected. However, to overcome this problem, cookieless
sessions can be enabled. To enable cookieless sessions, set cookieless="true" in web.config as
shown below.
<sessionState mode="InProc" cookieless="true"></sessionState>
With this change, navigate to WebForm1.aspx, fill in Name and Email fields, and then
navigate to WebForm2.aspx, and notice that, the Name and Email, fields are displayed as
expected. Notice, that the session-id is now part of the URL. This session-id is sent back and
forth between the client and the web server, with every request and response. The web server,
uses the session-id from the URL, to identify if the request has come from the same user or a
different user.
For cookieless sessions to work correctly, relative URL's must be used in the application, when
redirecting users to different webforms. For example, if you are
on http://pragimtech.com/WebForm1.aspx and if you want to navigate to WebForm2.aspx,
then use
Response.Redirect("~/WebForm2.aspx") - Relative URL
and not
Response.Redirect("http://pragimtech.com/WebFOrm2.aspx") - Absolute URL (or
Complete Path)
In this video, we will discuss about the asp.net session state mode - InProc. Asp.net
session state mode can have any of the following 4 values. Asp.net session state mode is set in
web.config file.
1. Off - Disables session state for the entire application.
2. InProc - Will be discussed in this session.
The following session state modes will be discussed in a later video session.
3. StateServer
4. SQLServer
5. Custom
InProc session state mode: When the session state mode is set to InProc, the session state
variables are stored on the web server memory inside the asp.net worker process. This is the
default session state mode.
Note:
Web Garden - Web application deployed on a server with multiple processors
Web Farm - Web application deployed on multiple server
In this video, we will discuss about the asp.net session state mode - StateServer. Asp.net
session state mode can have any of the following 4 values. Asp.net session state mode is set in
web.config file.
1. Off - Disables session state for the entire application.
2. InProc - Discussed in Part 64
3. StateServer - Will be discussed in this session.
The following session state modes will be discussed in a later video session.
4. SQLServer
5. Custom
When the session state mode is set to StateServer, the session state variables are stored in
a process, called as asp.net state service. This process is different from the asp.net worker
process. The asp.net state service can be present on a web server or a dedicated machine.
Note:
Web Garden - Web application deployed on a server with multiple processors
Web Farm - Web application deployed on multiple server
SQLServer asp.net session state mode management - Part 66
Suggested Videos
Part 63 - Cookie less sessions in asp.net
Part 64 - Inporc asp.net session state mode management
Part 65 - StateServer asp.net session state mode management
In this video, we will discuss about the asp.net session state mode - SQLServer. Asp.net
session state mode can have any of the following 4 values. Asp.net session state mode is set in
web.config file.
1. Off - Disables session state for the entire application.
2. InProc - Discussed in Part 64
3. StateServer - Discussed in Part 65
4. SQLServer - Will be discussed in this session.
5. Custom - Enables you to build your own Session State provider. For example, Oracle.
When the session state mode is set to SQLServer, the session state variables are stored in
a SQLServer database.
Note: If you use integrated security(windows authentication), you might get an error
stating "Failed to login to session state SQL server for user 'IIS APPPOOL\ASP.NET v4.0'.". To
resolve this error
a) click Start > Type Run > Press Enter
b) Type inetmgr > Press Enter
c) Expand IIIS and Click on Application Pools.
d) Right click on ASP.NET v4.0 and select Advanced settings
e) Change Process Model > Identity to LocalSystem and Click OK
Note:
Web Garden - Web application deployed on a server with multiple processors
Web Farm - Web application deployed on multiple server
Asp.net Application state - Part 67
Suggested Videos
Part 64 - Inporc asp.net session state mode management
Part 65 - StateServer asp.net session state mode management
Part 66 - SQLServer asp.net session state mode management
1. Application State variables are available across all pages and across all sessions.
Application State variables are like multi-user global data.
3. Application State variables are cleared, only when the process hosting the application is
restarted, that is when the application ends.
4. Application State variables are not shared across a Web Farm or a Web Garden.
5. Application state variables are not thread safe. Lock and Unlock methods of the
application class must be used to protect against race conditions, deadlocks, and access
violations.
Application.Lock();
Application["GlobalVariable"] = (int)Application["GlobalVariable"] + 1;
Application.UnLock();
Please Note: In this example, we are using application state variables to send data from one
web form to another. If the requirement, is just to send data from webform to another, you
should consider other alternatives.
6. Use application state variables only, when the variables need to have global access
and when you need them for entire time, during the life time of an application. Cache
object, can be used, as an alternative, if you need to have global access for a certain duration.
In this video we will discuss about a real time example, where we can use application
state variables.
Application state variables are global, and all sessions have access to them. So, these
variables can be used to track the number of users online. Every time a new user connects to
your application, we want to increase the number of users online by 1. Along, the same lines,
when ever a user session ends, then we need to decrease the number of users online by 1. But
how do we know, when a new user connects to our application. Session_Start() event is fired
when ever a new session is established. When the session ends, Session_End() event is fired.
The event handlers are in global.asax file.
Global.asax code:
public class Global : System.Web.HttpApplication
{
void Application_Start(object sender, EventArgs e)
{
// Code that runs when the application starts
Application["UsersOnline"] = 0;
}
void Session_Start(object sender, EventArgs e)
{
// Code that runs when a new user session is started
Application.Lock();
Application["UsersOnline"] = (int)Application["UsersOnline"] + 1;
Application.UnLock();
}
void Session_End(object sender, EventArgs e)
{
// Code that runs when an existing user session ends.
Application.Lock();
Application["UsersOnline"] = (int)Application["UsersOnline"] - 1;
Application.UnLock();
}
}
The application state variable is accessible across the entire application. Copy and paste
the following code in the Page_Load() event of any webform.
if (Application["UsersOnline"] != null)
{
Response.Write("Number of Users Online = " +
Application["UsersOnline"].ToString());
}
By default, the browser instances share the session cookie. To have a new session id
assigned, when a new browser instance requests the webform, set cookieless="true" for the
sessionstate element in web.config. Now run the application. The following message should be
displayed.
Number of Users Online = 1
Open a new browser window, copy and paste the URL from the other window. Make sure to
delete the session-id, so the web server, assigns a new session-id to the second request. At this
point, Number of Users Online should be incremented to 2.
In this session we will discuss about handling errors using try/catch blocks. In a later
video session we will discuss about, how exceptions are generally handled in a real time
asp.net application.
Exceptions are unforeseen errors that happen within the logic of an application. For
example, when reading a file, a number of exception conditions can occur.
1. The file might not exist
2. You may not have permissions to access the file
When an exception occurs and if it is not handled, then, that exception is called as
an, unhandled exception. An unhandled exception is displayed to the user using an "yellow
screen of death". Displaying the screen of death is bad for 2 reasons
1. The error messages are cryptic and may not make any sense to the end user
2. The exception information may be useful for a hacker, to hack into your application
catch - catches the exception and tries to correct the error and/or handles the exception
finally - Used to free resources. Finally block is guaranteed to execute irrespective of whether
an exception has occurred or not
The base class for all exceptions is the Exception class. Specific exceptions should be
caught, before catching the general parent exception.
Create an asp.net web application, and add an xml file, Countries.xml. Copy and paste the
following XML.
<?xml version="1.0" encoding="utf-8" ?>
<Countries>
<Country>
<Id>101</Id>
<Name>India</Name>
<Continent>Asia</Continent>
</Country>
<Country>
<Id>102</Id>
<Name>UK</Name>
<Continent>Europe</Continent>
</Country>
<Country>
<Id>103</Id>
<Name>US</Name>
<Continent>North America</Continent>
</Country>
<Country>
<Id>104</Id>
<Name>France</Name>
<Continent>Europe</Continent>
</Country>
</Countries>
Drag and drop, gridview and a lable control on WebForm1.aspx. HTML of the aspx page.
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
<br />
<asp:Label ID="lblError" Font-Bold="true" ForeColor="Red" runat="server"></asp:Label>
WebForm1.aspx.cs Code:
public partial class WebForm1 : System.Web.UI.Page
{
// Uncomment this line to print the name of the account
// used to run the application code
// Response.Write(System.Security.Principal.WindowsIdentity.GetCurrent().Name);
protected void Page_Load(object sender, EventArgs e)
{
try
{
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("~/Countries.xml"));
GridView1.DataSource = ds;
GridView1.DataBind();
}
// Catch specific exceptions first
catch (System.UnauthorizedAccessException unauthorizedAccessException)
{
//Log the exception information
lblError.Text = "Access to the file denied";
}
catch (System.IO.FileNotFoundException fileNotFoundException)
{
//Log the exception information
lblError.Text = "File does not exist";
}
catch (Exception ex)
{
//Log the exception information
lblError.Text = "There is an unkown problem. IT team is working on this issue. Please
check back after some time";
}
finally
{
// Code to clean up resources like closing file handles
// and database connection objects
}
}
}
Error events in asp.net - Part 70
Suggested Videos
Part 67 - Asp.net Application state
Part 68 - Asp.net application state real time example
Part 69 - Exception handling in asp.net
Create an asp.net web application. Add a webform with name Errors.aspx. Copy and paste
the following HTML in Errors.aspx.
<div style="font-family: Arial">
<table style="border:1px solid black">
<tr>
<td style="color:Red">
<h2>Application Error</h2>
</td>
</tr>
<tr>
<td>
<h3>
An unkown error has occured. We are aware of it and the IT team is currently
working
on this issue. Sorry for the inconvinience caused.</h3>
</td>
</tr>
<tr>
<td>
<h5>
If you need further assistance, please contact our helpdesk at
[email protected]
</h5>
</td>
</tr>
</table>
</div>
Add WebForm1.aspx to the project. Drag and drop a gridview control. Copy and paste the
following code in Webform1.aspx.cs.
protected void Page_Load(object sender, EventArgs e)
{
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("~/Data/Countries.xml"));
GridView1.DataSource = ds;
GridView1.DataBind();
}
The code tries to read xml data from Countries.xml file. At the moment, the file is not
present and we get a FileNotFound exception. Since this exception is not handled using a
try/catch block in the Page_Load event, the eror get to the page level and is handled by
Page_Error event handler. In Page_Error event handler
1. We get the exception information using Server.GetLastError() method.
2. Do something with the exception, such as redirect the user to a generic error page, display an
error message on the same page which caused the exception, try to correct the problem, or log
the exception to a database table or event viewer and notify the development team. We will
discuss about logging and notifications in a later video session.
3. We then clear, the exception, so that it is not get propagated to the application level.
4. Finally we redirect the user to a generic error page, Errors.aspx
If an exception is not handled at the page level using Page_Error event, it get's to the
application level and can be handled using the Application_Error event handler in Global.asax
and can be used as a single, centralized location for error handling.
Page level custom error pages takes precedence over application level custom error
pages.
Custom error pages provide the flexibility of displaying a specific page in response to one
or more of the available HTTP status codes. For a list of all the available HTTP status please
visit the following Article.
http://en.wikipedia.org/wiki/List_of_HTTP_status_codes
To specify the custom error pages at an application level, use customErrors element in
web.config
<customErrors mode="On" defaultRedirect="DefaultErrorPage.aspx">
<error statusCode="401" redirect="UnauthorizedErrorPage.aspx" />
<error statusCode="404" redirect="PageNotFoundErrorPage.aspx" />
<error statusCode="500" redirect="InternalServerErrorPage.aspx" />
</customErrors>
The mode attribute determines when a custom error page is displayed over the yellow
screen of death, exception page. Mode attribute can have On, Off, or RemoteOnly. Default is
RemoteOnly.
On - Custom error pages are displayed both on local and remote machines
Off - Custom error pages are not displayed anywhere
RemoteOnly - Custom error pages are displayed on remote machines, and exception page on
local machine
In your application, if you have to display specific custom error pages for specific http status
codes, then use custom errors. If you just have one generic error page, then Global.asax can be
used.
Please note that, the exception object needs to be retrieved, before the user is redirected to a
custom error page. Because a custom error page is displayed through redirection, the context
for the error is lost and Server.GetLastError returns nothing from the target custom error page.
Windows event viewer - Part 72
Suggested Videos
Part 69 - Exception handling
Part 70 - Error events
Part 71 - Custom errors
Exceptions in an asp.net web application can be logged to the event viewer. First let us
discuss about the event viewer and create custom event log and event source. To access the
event viewer
1. Click on Start
2. Type "Run" and press enter
3. In the "Run" window type "eventvwr" and press enter
4. This should open event viewer
Let us now create a windows application that can be used to create a custom windows
event log and event source. The Event Source is the name of the application that logged the
event. The event source is displayed in the details pane, when an event is selected in the event
viewer.
Steps to create the windows application to create the custom windows event log and
event source.
1. Drag and drop two textboxes, two labels, and a button control on to the windows form
2. Arrange the controls, so the form looks as shown below.
Open the event viewer. The newly created "event log" should be under Applications and Service
Logs. If you are not able to locate them, restart your machine.
In this video we will discuss about logging exceptions to windows eventviewer. In the
previous videos we discussed about, creating the custom event log and event source in
windows event viewer. Please watch Windows Event Viewer - Part 72, before continuing with
this session.
Create an asp.net web application project. Add WebForm1.aspx. Drag and drop girdview
control. Copy and paste the following code.
//try
//{
// DataSet is System.Data namespace
DataSet ds = new DataSet();
// This line throws FileNotFoundException
ds.ReadXml(Server.MapPath("~/Data/Countries.xml"));
GridView1.DataSource = ds;
GridView1.DataBind();
//}
//catch (Exception ex)
//{
// Logger.Log(ex);
//}
Copy and pste the following code, in Global.asax, in Application_Error() event handler
if (Server.GetLastError() != null)
{
// Get and Log the exception
Logger.Log(Server.GetLastError());
// Clear the exception
Server.ClearError();
// Transfer the user to Errors.aspx page
Server.Transfer("Errors.aspx");
}
Run the application. WebForm1.aspx throws an exception. Since this exception is not
handled any where it gets propagated till the application level, and Application_Error() event
handler will be executed. This should log the exception to the windows eventviewer.
In this video we will discuss about logging exceptions as Information entry type in
windows event viewer. Before continuing with this session please watch Part 72 and Part 73.
Create an asp.net web application and add a class file with name Logger.cs.
public class Logger
{
public static void Log(Exception exception)
{
Log(exception, EventLogEntryType.Error);
}
public static void Log(Exception exception, EventLogEntryType eventLogEntryType)
{
do
{
sbExceptionMessage.Append("Exception Type" + Environment.NewLine);
sbExceptionMessage.Append(exception.GetType().Name);
sbExceptionMessage.Append(Environment.NewLine + Environment.NewLine);
sbExceptionMessage.Append("Message" + Environment.NewLine);
sbExceptionMessage.Append(exception.Message + Environment.NewLine
+ Environment.NewLine);
sbExceptionMessage.Append("Stack Trace" + Environment.NewLine);
sbExceptionMessage.Append(exception.StackTrace + Environment.NewLine
+ Environment.NewLine);
exception = exception.InnerException;
}
while (exception != null);
Add a webform, with name Calculator.aspx and copy the following HTML.
<div style="font-family: Arial">
<table style="border: 1px solid black">
<tr>
<td><b>First Number</b></td>
<td>
<asp:TextBox ID="txtFirstNumber" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td><b>Second Number</b></td>
<td>
<asp:TextBox ID="txtSecondNumber" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="btnDivide" runat="server" Text="Divide" onclick="btnDivide_Click"
/>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Label ID="lblMessage" runat="server" Font-Bold="true"></asp:Label>
</td>
</tr>
</table>
</div>
Calculator.aspx.cs code:
protected void btnDivide_Click(object sender, EventArgs e)
{
try
{
int FirstNumber = Convert.ToInt32(txtFirstNumber.Text);
int SecondNumber = Convert.ToInt32(txtSecondNumber.Text);
lblMessage.ForeColor = System.Drawing.Color.Navy;
int Result = FirstNumber / SecondNumber;
lblMessage.Text = Result.ToString();
}
catch (FormatException formatException)
{
Logger.Log(formatException, EventLogEntryType.Information);
lblMessage.ForeColor = System.Drawing.Color.Red;
lblMessage.Text = "Only numbers are allowed";
}
catch (OverflowException overflowException)
{
Logger.Log(overflowException, EventLogEntryType.Information);
lblMessage.ForeColor = System.Drawing.Color.Red;
lblMessage.Text = "Numbers must be between " + Int32.MinValue.ToString() + " and
" + Int32.MaxValue.ToString();
}
catch (DivideByZeroException divideByZeroException)
{
Logger.Log(divideByZeroException, EventLogEntryType.Information);
lblMessage.ForeColor = System.Drawing.Color.Red;
lblMessage.Text = "Denominator cannot be ZERO";
}
catch (Exception exception)
{
Logger.Log(exception);
lblMessage.ForeColor = System.Drawing.Color.Red;
lblMessage.Text = "An unknown problem has occured. Please try later";
}
}
In this video we will discuss about loggin exceptions to a database table. The first step is to
create the required table, to log the exceptions.
do
{
sbExceptionMessage.Append("Exception Type" + Environment.NewLine);
sbExceptionMessage.Append(exception.GetType().Name);
sbExceptionMessage.Append(Environment.NewLine + Environment.NewLine);
sbExceptionMessage.Append("Message" + Environment.NewLine);
exception = exception.InnerException;
}
while (exception != null);
LogToDB(sbExceptionMessage.ToString());
}
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
Add a webform to the project. Drag and drop a gridview control. Copy and paste the following
code in the code behind file, in the Page_Load() event.
//try
//{
// DataSet is in System.Data namespace
DataSet ds = new DataSet();
// This line will throw an exception, as Data.xml
// file is not present in the project
ds.ReadXml(Server.MapPath("~/Data.xml"));
GridView1.DataSource = ds;
GridView1.DataBind();
//}
//catch (Exception ex)
//{
// Logger.Log(ex);
// Server.Transfer("~/Errors.aspx");
//}
Global.asax code:
void Application_Error(object sender, EventArgs e)
{
if (Server.GetLastError() != null)
{
// Log the exception
Logger.Log(Server.GetLastError());
// Clear the exception
Server.ClearError();
// Transfer the user to Errors.aspx page
Server.Transfer("Errors.aspx");
}
}
Run the application. The exception should be logged to the Database table - tblLog and the
user will be redirected to Errors.aspx page.
Add a key to web.config file using appsettings element. This key determines, where the
exceptions are logged.
<appSettings>
<!--LogProvider = Database|EventViewer|Both-->
<add key="LogProvider" value="Both"/>
</appSettings>
The "Logger" class reads the "LogProvider" key from web.config and logs the exceptions
accordingly.
do
{
sbExceptionMessage.Append("Exception Type" + Environment.NewLine);
sbExceptionMessage.Append(exception.GetType().Name);
sbExceptionMessage.Append(Environment.NewLine + Environment.NewLine);
sbExceptionMessage.Append("Message" + Environment.NewLine);
sbExceptionMessage.Append(exception.Message + Environment NewLine
+ Environment.NewLine);
sbExceptionMessage.Append("Stack Trace" + Environment.NewLine);
sbExceptionMessage.Append(exception.StackTrace + Environment NewLine
+ Environment.NewLine);
exception = exception.InnerException;
}
while (exception != null);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
Global.asax, Errors.aspx and WebForm1.aspx HTML and code have not changed from Part
75 - Logging exception to database.
Sending emails using asp.net - Part 77
Suggested Videos
Part 74 - Logging exceptions as information entry type in windows eventviewer
Part 75 - Logging exceptions to database
Part 76 - Customizing asp.net exception Logging
In the previous sessions of this video series, we have discussed about logging exceptions to
database and to the windows eventviewer. In this session, sending an email to the development
team or administrator along with logging exceptions.
To compose the email message, use MailMessage calss. To send the email use, SmtpClient
class. Both of these classes are present in System.Net.Mail namespace.
SendEmail() method can then be called in our Logger class. Pass in the exception string as
an input parameter.
SendEmail(sbExceptionMessage.ToString());
If you want the capability of sending emails to be configurable, add the following key in
web.config.
<appSettings>
<add key="SendEmail" value="true"/>
</appSettings>
Read "SendEmail" key from web.config. If SendEmail is set to true only then, send the email.
string sendEmail = ConfigurationManager.AppSettings["SendEmail"];
if (sendEmail.ToLower() == "true")
{
SendEmail(sbExceptionMessage.ToString());
}
In this video we discussed about sending emails using gmail smtp server and credentials. In
reality organisations have their own SMPT server. If you want to send emails using your own
SMTP server, use the respective smtp server address and credentials.
In the previous session, we discussed about sending emails using asp.net. All the SMTP
server settings were configured in code. In this session, we will discuss about specifying these
settings in web.config file.
Web.config settings for SMTP server. All the attributes here are self explanatory.
<system.net>
<mailSettings>
<smtp deliveryMethod="Network" >
<network host="smtp.gmail.com" enableSsl="true" port="587"
userName="[email protected]" password="your_password"/>
</smtp>
</mailSettings>
</system.net>
Since the SMTP server settings are now configured in web.config. In code all you have to
do is
1. Create an instance of the MailMessage class. Specify the FROM & TO email address,
subject and Body
2. Create an instance of SmtpClient class, and send the email using Send() method. The SMTP
settings will be automatically picked up from web.config file, when sending email.
Tracing enables us to view diagnostic information about a request and is very useful
when debugging application problems.
In a later video session, we will discuss about, how tracing can be used to solve a performance
related problem.
To enable tracing at the application level set "trace" element's "enabled" attribute
to "true" in web.config. This enables tracing for all pages in the application.
<trace enabled="true"/>
To disable tracing for specific pages, set Trace="false" in the webform's "Page" directive
<%@ Page Language="C#" Trace="false" AutoEventWireup="true" CodeBehind="WebFo
rm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
If tracing is enabled at the application level, the trace messages are written to a file
called trace.axd. Trace.xd file can only be accessed locally. To make the trace file available to
remote users set localOnly="false". Tracing displays sensitive information, that could be useful
to a hacker to hack into the server. So set this attribute to "false" only when it is required.
<trace enabled="true" localOnly="false"/>
Use RequestLimit attribute to control the number of trace requests that are stored on the
server. The default is 10. After this limit is reached, the sever will stop writing to the trace file.
<trace enabled="true" pageOutput="true" requestLimit="5" localOnly="false"/>
If you want to log trace messages, even after the requestLimit has reached set mostRecent
attribute to true. When this attribute is set to true, the old entries in the trace log are discarded
and the new entries get added.
<trace enabled="true" mostRecent="true" requestLimit="3"/>
To write custom asp.net trace messages Trace.Write() and Trace.Warn() methods can be
used. The only difference, between these 2 methods is that, the messages written using
Trace.Warn() are displayed in red colour, where as the messages written using Trace.Write()
are displayed in black colour. In fact, What is the difference between Trace.Write() and
Trace.Warn() is a very common asp.net interview question.
Webform1.aspx HTML:
<div style="font-family: Arial">
<table style="border: 1px solid black">
<tr>
<td>
<b>First Number</b>
</td>
<td>
<asp:TextBox ID="txtFirstNumber" runat="server">
</asp:TextBox>
</td>
</tr>
<tr>
<td>
<b>Second Number</b>
</td>
<td>
<asp:TextBox ID="txtSecondNumber" runat="server">
</asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="btnDivide" runat="server" Text="Divide"
OnClick="btnDivide_Click" />
</td>
</tr>
<tr>
<td colspan="2">
<asp:Label ID="lblMessage" runat="server" Font-Bold="true">
</asp:Label>
</td>
</tr>
</table>
</div>
lblMessage.ForeColor = System.Drawing.Color.Navy;
int Result = FirstNumber / SecondNumber;
lblMessage.Text = Result.ToString();
}
catch (FormatException formatException)
{
lblMessage.ForeColor = System.Drawing.Color.Red;
lblMessage.Text = "Only numbers are allowed";
// Check if tracing is enabled
if (Trace.IsEnabled)
{
// Write the exception message to the trace log
Trace.Write(formatException.Message);
}
}
catch (OverflowException overflowException)
{
lblMessage.ForeColor = System.Drawing.Color.Red;
lblMessage.Text = "Numbers must be between " + Int32.MinValue.ToString() + " and
" + Int32.MaxValue.ToString();
if (Trace.IsEnabled)
{
Trace.Warn(overflowException.Message);
}
}
catch (DivideByZeroException divideByZeroException)
{
lblMessage.ForeColor = System.Drawing.Color.Red;
lblMessage.Text = "Denominator cannot be ZERO";
if (Trace.IsEnabled)
{
Trace.Warn(divideByZeroException.Message);
}
}
catch (Exception exception)
{
lblMessage.ForeColor = System.Drawing.Color.Red;
lblMessage.Text = "An unknown problem has occured. Please try later";
if (Trace.IsEnabled)
{
Trace.Warn(exception.Message);
}
}
}
Trace.IsEnabled property can be used to check if tracing is enabled. In the following code
Trace.Write() method is invoked only if tracing is enabled. If you don't check, Trace.IsEnabled
property prior to writing out trace messages, we don't get an exception. But if you are going to
do any sort of significant work to build the trace message, then you can avoid that work by
checking the IsEnabled property first.
if (Trace.IsEnabled)
{
Trace.Write("Debugging information");
}
With classic ASP, the only option for printing debugging information is Response.Write().
There are 2 problems with this
1. Actual end users also, can see the debugging information that you have written using
Response.Write(). But with tracing, if pageoutput attribute is set to false, then the trace
messages are written to the trace.axd file. End users cannot see the trace information.
2. All the Response.Write() statements must be removed, before the application is deployed to
production. But with tracing, all you have to do is disable tracing in web.config.
We cannot debug an application on the production server, as we will usually, not have
visual studio installed, and as the code is optimized for release builds.
Step 1:
First find out which is slow, is it the application or the database : If the page is executing SQL
queries or stored procedures, run those on the database and check how long do they take to
run. Alternatively, SQL profiler, can be used, to inspect the queries and the time they take. If the
queries are taking most of the time, then you know you have to tune the queries for better
performance. To tune the queries, there are several ways and I have listed some of them below.
a) Check if there are indexes to help the query
b) Select only the required columns, avoid Select *.
c) Check if there is a possibility to reduce the number of joins
d) If possible use NO LOCK on your select statements
e) Check if there are cursors and if you can replace them with joins
Step 2:
If the queries are running fast, then we know it is the application code that is causing the
slowness. Isolate the page event that is causing the issue by turning tracing on. To turn tracing
on, set Trace="true" in the page directive. Once you have tracing turned on you should see
trace information at the bottom of the page or in trace.axd file. From the trace information, it
should be clear to identify the piece of code that is taking the maximum time.
Code Behind:
protected void Page_Load(object sender, EventArgs e)
{
Trace.Warn("GetAllEmployees() started");
GetAllEmployees();
Trace.Warn("GetAllEmployees() Complete");
Trace.Warn("GetEmployeesByGender() started");
GetEmployeesByGender();
Trace.Warn("GetEmployeesByGender() Complete");
Trace.Warn("GetEmployeesByDepartment() started");
GetEmployeesByDepartment();
Trace.Warn("GetEmployeesByDepartment() Complete");
}
Many a times, the issue is not reproducible on the development environment. The issue
happens only on the production server. In this case tracing is an invaluable mechanism to get to
the root cause of the issue.
In addition to these built-in accounts, we can also use a custom account, by specifying
the username and password.
Network Service : Restricted or limited service account that is generally used to run, standard
least-privileged services. This account has less privileges than Local System account. This
account can access network resources.
Local Service : Restricted or limited service account that is very similar to Network Service and
meant to run standard least-privileged services. This account cannot access network resources.
At this point, if you run WebApplication1, by using CTRL+F5, visual studio by default uses
built-in asp.net development server. Configure visual studio to use local IIS.
1. Right click on the "WebApplication1" project in solution explorer in visual studio and
select "Properties"
2. In the properties window click on "Web" tab.
3. Under "Servers" section, Select "Use Local IIS Web Server" radio button
4. Set project Url=http://localhost/WebApplication1 and save the changes.
Run both the applications. When you click the button on both the applications, Session data
should be available.
Now click the button controls, on both the applications. Notice that both the applications have
lost their session data.
Now create a new application pool in IIS, with name WebApplication2Pool and associate
WebApplication2, with this new pool. Run both the applications again. Click the button controls.
Session data should be available. Recycle WebApplication1Pool. Click the button controls again
on both the applications. Notice, that, only WebApplication1 has lost the session data but not
WebApplication2. WebApplication2 belong to WebApplication2Pool. We have not recycled
WebApplication2Pool, and hence it retains it's session data.
In the next video session, we will discuss about configuring different levels of security
for different application pools.
In this video, we will discuss about configuring different levels of security for different
application pools, with an example.
In your C:\ drive, create a folder with name "Data". Open a notepad. Copy and paste the
following XML into the notepad. Save the notepad as Application1Data.xml in C:\Data folder.
Open, another notepad, copy and paste the same XML. Now save the notepad
as Application2Data.xml in C:\Data folder. So, at this point, you should
have Application1Data.xml and Application2Data.xml in C:\Data folder.
Create an asp.net web application with name WebApplication1. Drag and drop a GridView,
FileUpload, Button and a Label control on to the webform. Set Text="Load Data" for the button
control. Remove the Text property of the Label control. Double click the button control to
generate the event handler. At this stage, the html of webform1.aspx should be as shown
below.
<div>
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
<br />
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server"
Text="Load Data" onclick="Button1_Click" />
<br />
<asp:Label ID="Label1" runat="server">
</asp:Label>
</div>
WebForm1.aspx.cs code:
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("Identity used = " +
System.Security.Principal.WindowsIdentity.GetCurrent().Name + "<br/>");
}
Create another asp.net web application with name WebApplication2. Copy and paste the
HTML and code of WebForm1 from WebApplication1. Create an application pool with
name Application2Pool, and associate WebApplication2, with this pool.
Run WebApplication1, and select Application1Data.xml from C:\Data folder, and click on
"Load Data" button. The data should load fine. Now,
select Application2Data.xml from C:\Data folder, and click on "Load Data" button. The data
should load fine, from Application2Data.xml file as well. Test the same,
with WebApplication2.
At this stage, all we have to do is, set the file permissions accordingly for the application
pool identities.
With these changes in place now, WebApplication1 should only be able to access
Application1Data.xml and WebApplication2, only Application2Data.xml. Instead of showing the
"Yellow screen of death", user friendly error message can be displayed in the label control by
catching the security exception as shown below.
Most of the public web sites, does not ask the user to enter any user name and
password. But still, we will be able to access the content of these web sites. ASP.NET Web
applications provide anonymous access to resources on the server. Anonymous authentication
allows users to access the public areas of the web site, without prompting the users for a user
name or password.
Create an asp.net web application. Copy and paste the following code in the Page_Load()
event of WebForm1.aspx.cs
Response.Write("Application code executed using ");
Response.Write(System.Security.Principal.WindowsIdentity.GetCurrent().Name + "<br/>");
In IIS 6.0
IUSR_ComputerName is used for providing anonymous access.
In IIS 7.0
IUSR account is used for providing anonymous access.
Run the application. Notice, that the application pool identity is used to execute the application
code. In the next video session, we will discuss about asp.net impersonation with anonymous
access.
To disable anonymous authentication, click "Disable" link under "actions" in the right hand
side panel in IIS.
To change the account that is associated with anonymous access, click "Edit" link under
actions in the right hand side panel in IIS. Notice, that the default account is IUSR. This can be
changed to a custom windows account or Application pool identity.
Please watch Part 85, before watching this video. In Part 85, we discussed that IIS provides
anonymous access to resources using IUSR account. Once the request is handed over to
asp.net, the application code is executed using the application pool identity.
In this video, we will discuss the effects of turning impersonation on, with anonymous access.
In "C:\Data" folder, create an XML file with name Countries.xml.
<?xml version="1.0" encoding="utf-8" ?>
<Countries>
<Country>
<Id>101</Id>
<Name>India</Name>
<Continent>Asia</Continent>
</Country>
<Country>
<Id>102</Id>
<Name>UK</Name>
<Continent>Europe</Continent>
</Country>
<Country>
<Id>103</Id>
<Name>US</Name>
<Continent>North America</Continent>
</Country>
<Country>
<Id>104</Id>
<Name>France</Name>
<Continent>Europe</Continent>
</Country>
</Countries>
Create an asp.net web application. Drag and drop a gridview control and a button control on
the webform. Copy and paste the following code in WebForm1.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("Application code executed using ");
Response.Write(System.Security.Principal.WindowsIdentity.GetCurrent().Name + "<br/>");
At this point, if you run the application, you may get an error stating
HTTP Error 500.24 - Internal Server Error
An ASP.NET setting has been detected that does not apply in Integrated managed pipeline
mode.
To correct this, we need to set the "Managed pipeline mode" of the DefaultAppPool to
"Classic".
Run the application, and notice that, the application code, is now executed, using 'NT
AUTHORITY\IUSR' account, instead of 'IIS APPPOOL\DefaultAppPool'
Windows authentication, identifies and authorizes users based on the server’s user list.
Access to resources on the server is then granted or denied based on the user account’s
privileges.
The advantage of Windows authentication is that, the Web application can use the exact
same security scheme that applies to your corporate network. User names, passwords, and
permissions are the same for network resources and Web applications.
We will be using the same project, that we worked with, in Part 86.
At this point, we have both anonymous and windows authentication enabled in IIS. We have
not configured anything in the application yet. Run the application, and notice that, the user is
still using anonymous authentication to access the webform.
So, if both, anonymous and windows authentication are enabled in IIS, and, if we don't have a
deny entry for anonymous users, in the web.config file, then the resources on the web server
are accessed using anonymous authentication.
Run the application now. Notice that the user is authenticated using the windows account, that
is used to log into the computer. Also, notice that, the application code is executed using the
application pool identity.
If you want to have the application code executed using the logged in user identity, then enable
impersonation. Impersonation can be enabled thru IIS or by adding the following element to
web.config file.
<identity impersonate="true"/>
If impersonation is enabled, the application executes using the permissions found in your user
account. So, if the logged in user has access, to a specific network resource, only then will he
be able to access that resource thru the application.
In Part 87, we have discussed the basics of windows authentication. In this session, we will
continue to discuss about windows authentication. Please watch Part 87, before proceeding.
? and * have special meaning when used in the authorization element in web.config
? (Question Mark) - Indicates anonymous users
* (Star) - Indicates all users
Please watch Parts 87 and 88, before proceeding. In this video we will discuss about folder
level authorization, with an example. Consider the project structure, shown in the solution
explorer below.
Only administrators should be able to access the pages in "Admin" folder. The rest of the
pages can be accessed by anyone. To achieve this, add another web.config file to the "Admin"
folder and include the following authorization element.
<authorization>
<allow roles="Administrators" />
<deny users="*" />
</authorization>
Application root level web.config file. This allows access to all authenticated users.
<authorization>
<deny users="?"/>
</authorization>
If you want to execute the application code, using the logged in Administrator account, then
enable impersonation, in the web.config file of the Admin folder. With this setting in place, all the
pages in the Admin folder are executed using the logged in user account, where as the pages
outside of the folder are executed using the identity of the application pool.
<system.web>
<authorization>
<allow roles="Administrators" />
<deny users="*" />
</authorization>
<identity impersonate="true"/>
</system.web>
It is also possible to impersonate, with a specific user name and password. With this
setting, whenever any user belonging to the "Administrators" group requests a page from the
Admin folder, the code will be executed using "Venkat" account.
<system.web>
<authorization>
<allow roles="Administrators" />
<deny users="*" />
</authorization>
<identity impersonate="true" userName="Venkat" password="test"/>
</system.web>
Anonymous authentication is fine for web sites that contain public information that every
one can see. We discussed about Anonymous authentication in
Part 85 - Anonymous authentication
Part 86 - Anonymous authentication and impersonation
Windows authentication is used for intranet web applications, where the users are part of a
windows domain-based network. We discussed about Windows authentication in Parts 87, 88
and 89.
In this video we will discuss about
1. When to use Forms Authentication
2. How to enable Forms Authentication
Welcome.aspx HTML:
<h1>Welcome Page</h1>
Login.aspx HTML:
<div style="font-family:Arial">
<table style="border: 1px solid black">
<tr>
<td colspan="2">
<b>Login</b>
</td>
</tr>
<tr>
<td>
User Name
</td>
<td>
:<asp:TextBox ID="txtUserName" runat="server">
</asp:TextBox>
</td>
</tr>
<tr>
<td>
Password
</td>
<td>
:<asp:TextBox ID="txtPassword" TextMode="Password" runat="server">
</asp:TextBox>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="btnLogin" runat="server" Text="Login" />
</td>
</tr>
</table>
<br />
<a href="Registration/Register.aspx">Click here to register</a>
if you do not have a user name and password.
</div>
Register.aspx HTML:
<h1>Registration Page</h1>
If you run the application now, we will be able to navigate to any page, just by changing the
name of the page in the address bar. We are not logged in, but we are still able to access all the
pages in the application.
Let us enable forms authentication now. To enable forms authentication, set authentication
element's mode attribute to forms in web.config file of the application.
<authentication mode="Forms">
<forms loginUrl="Login.aspx" timeout="30"
defaultUrl="Welcome.aspx" protection="All">
<credentials passwordFormat="Clear">
<user name="venkat" password="venkat"/>
<user name="pragim" password="pragim"/>
<user name="prasad" password="prasad"/>
</credentials>
</forms>
</authentication>
<authorization>
<deny users="?" />
</authorization>
timeout - Specifies the number of minutes the authentication cookie persists on the clients’s
computer. The default is 30 minutes.
Protection - Specifies the protection for authentication cookie stored on the clients’s computer.
The default is All, which performs encryption and data validation. Other possible settings are
Encryption, Validation, and None.
Double click the login button on the Login.aspx page. Copy and paste the following code in
the button click event handler.
// Authenticate againts the list stored in web.config
if (FormsAuthentication.Authenticate(txtUserName.Text, txtPassword.Text))
{
// Create the authentication cookie and redirect the user to welcome page
FormsAuthentication.RedirectFromLoginPage(txtUserName.Text,
chkBoxRememberMe.Checked);
}
else
{
lblMessage.Text = "Invalid UserName and/or password";
}
2. At the moment, users are not able to access Register.aspx page, if they are not logged in. If a
user does not have user name and password, he should be able to register himself using
Register.aspx page. In a later video session, we will discuss about this.
In this code sample, we have used validation controls and ADO.NET. If you have not
watched the videos on validation controls and ADO.NET, I would strongly encourage you to do
so, before continuing with this session.
Please watch Part - 90, before proceeding. In Part - 90, we have discussed the basics of
Forms authentication. One of the problems, with the example in Part 90, is that, we are not able
to navigate to Registration/Register.aspx page if we are not logged in.
To solve this issue, add another web.config file to the "Registration" folder, and specify the
authorization element to allow all users.
<authorization>
<allow users="*"/>
</authorization>
At this point, without logging into the application, users should be able to navigate to
Registration/Register.aspx page.
ControlToValidate="txtConfirmPassword" ForeColor="Red"
Display="Dynamic"></asp:RequiredFieldValidator>
<asp:CompareValidator ID="CompareValidatorPassword" runat="server"
ErrorMessage="Password and Confirm Password must match"
ControlToValidate="txtConfirmPassword" ForeColor="Red"
ControlToCompare="txtPassword" Display="Dynamic"
Type="String" Operator="Equal" Text="*">
</asp:CompareValidator>
</td>
</tr>
<tr>
<td>
Email
</td>
<td>
:<asp:TextBox ID="txtEmail" runat="server">
</asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidatorEmail"
runat="server" ErrorMessage="Email required" Text="*"
ControlToValidate="txtEmail" ForeColor="Red"
Display="Dynamic"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidatorEmail"
runat="server" ErrorMessage="Invalid Email" ControlToValidate="txtEmail"
ForeColor="Red" Display="Dynamic" Text="*"
ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">
</asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="btnRegister" runat="server" Text="Register"
onclick="btnRegister_Click"/>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Label ID="lblMessage" runat="server" ForeColor="Red">
</asp:Label>
</td>
</tr>
<tr>
<td colspan="2">
<asp:ValidationSummary ID="ValidationSummary1" ForeColor="Red" runat="server" />
</td>
</tr>
</table>
</div>
Copy and Paste the following code in the "Register" button click event.
// If the Page has no validation errors
if (Page.IsValid)
{
// Read the connection string from web.config.
// ConfigurationManager class is in System.Configuration namespace
string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
cmd.Parameters.Add(username);
cmd.Parameters.Add(password);
cmd.Parameters.Add(email);
con.Open();
int ReturnCode = (int)cmd.ExecuteScalar();
if (ReturnCode == -1)
{
lblMessage.Text = "User Name already in use, please choose another user name";
}
else
{
Response.Redirect("~/Login.aspx");
}
}
}
Run the application. Fill in the required details, and click "Register" button. The user
should be added to the database. In the next video session, we will discuss about,
authenticating with the credentials we stored in the database.
In Part 90, we have discussed about authenticating users against a list stored in
web.config file. In Part 91, we have discussed about, registering users, if they do not have a
username and password to log in. In this session, we will disuss about authenticating users
against a list stored in a database table.
This is continuation to Part 91. Please watch Part 91, before proceeding with this video.
Authenticating users against a list stored in web.config file is very easy. FormsAuthentication
class exposes a static method Authenticate(), which does all the hardwork of authenticating
users.
If we want to authenticate users against a list stored in a database table, we will have to
write the stored procedure and a method in the application to authenticate users.
First let us create a stored procedure, that accepts username and password as input
parameters and authenticate users.
Create Procedure spAuthenticateUser
@UserName nvarchar(100)
@Password nvarchar(100)
as
Begin
Declare @Count int
if(@Count = 1)
Begin
Select 1 as ReturnCode
End
Else
Begin
Select -1 as ReturnCode
End
End
Copy and paste the following private method in Login.aspx.cs page. This method invokes
stored procedure 'spAuthenticateUser'.
private bool AuthenticateUser(string username, string password)
{
// ConfigurationManager class is in System.Configuration namespace
string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
// SqlConnection is in System.Data.SqlClient namespace
using (SqlConnection con = new SqlConnection(CS))
{
SqlCommand cmd = new SqlCommand("spAuthenticateUser", con);
cmd.CommandType = CommandType.StoredProcedure;
// FormsAuthentication is in System.Web.Security
string EncryptedPassword
= FormsAuthentication.HashPasswordForStoringInConfigFile(password, "SHA1");
// SqlParameter is in System.Data namespace
SqlParameter paramUsername = new SqlParameter("@UserName", username);
SqlParameter paramPassword = new SqlParameter("@Password", EncryptedPassword);
cmd.Parameters.Add(paramUsername);
cmd.Parameters.Add(paramPassword);
con.Open();
int ReturnCode = (int)cmd.ExecuteScalar();
return ReturnCode == 1;
}
}
Please watch Parts 90, 91 and 92 before proceeding. In this video we will discuss about
locking or disabling user accounts, after repeated invalid attempts to login.
For example, if a user enters wrong username and password, he will be given 3 more chances,
to enter the correct password. After the 3 chances are elapsed, the account will be locked. After
the account is locked, the user will not be able to log in, even, if he provides a correct user
name and password.
Drop the table, tblUsers, that we have created in Part 90. Recreate tblUsers table using the
script below.
Create table tblUsers
(
[Id] int identity primary key,
[UserName] nvarchar(100),
[Password] nvarchar(200),
[Email] nvarchar(100),
[RetryAttempts] int,
[IsLocked] bit,
[LockedDateTime] datetime
)
-- If match found
if(@Count = 1)
Begin
-- Reset RetryAttempts
Update tblUsers set RetryAttempts = 0
where UserName = @UserName
if(@RetryCount <= 3)
Begin
-- If re-try attempts are not completed
Update tblUsers set RetryAttempts = @RetryCount
where UserName = @UserName
Copy and Paste the following version of AuthenticateUser() method in Login.aspx.cs page.
private void AuthenticateUser(string username, string password)
{
// ConfigurationManager class is in System.Configuration namespace
string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
//Formsauthentication is in system.web.security
string encryptedpassword
= FormsAuthentication.HashPasswordForStoringInConfigFile(password, "SHA1");
cmd.Parameters.Add(paramUsername);
cmd.Parameters.Add(paramPassword);
con.Open();
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
int RetryAttempts = Convert.ToInt32(rdr["RetryAttempts"]);
if (Convert.ToBoolean(rdr["AccountLocked"]))
{
lblMessage.Text = "Account locked. Please contact administrator";
}
else if (RetryAttempts > 0)
{
int AttemptsLeft = (4 - RetryAttempts);
lblMessage.Text = "Invalid user name and/or password. " +
AttemptsLeft.ToString() + "attempt(s) left";
}
else if (Convert.ToBoolean(rdr["Authenticated"]))
{
FormsAuthentication.RedirectFromLoginPage(txtUserName.Text,
chkBoxRememberMe.Checked);
}
}
}
}
Invoke AuthenticateUser() method in the click event handler of the login button control.
AuthenticateUser(txtUserName.Text, txtPassword.Text);
In the next video session, we will discuss about enabling the disabled user accounts.
In Part 93, of this video series we have discussed about locking user accounts, if a user
repeatedly enters the wrong password. The accounts are locked to prevent hackers from
guessing passwords and dictionary attacks. Please watch Part 93, before proceeding with this
video.
In this video, we will discuss about unlocking the locked user accounts. There are several
ways to unlock the user accounts.
Approach 1: The end user calls the technical help desk. The authorised person can issue a
simple update query to remove the lock.
Update tblUsers
set RetryAttempts = null, IsLocked = 0, LockedDateTime = null
where username='CallersUserName'
Approach 2: Another approach would be to provide a web page that lists all the locked user
accounts. From this page, the helpdesk agent, can unlock the account by clicking a button. This
is not as dangerous as running a manual update query, but still a manual process and may be
in-efficient. If you know how to write basic ADO.NET code, this approach should not be very
difficult to achieve. If you are new to ADO.NET, Click here for a video series that I have
recorded on ADO.NET
Approach 3: Another approach would be, to create a SQL Server job. This job checks tblUsers
table for locked accounts periodically and then unlocks them. The frequency at which the job
should run is configurable.
In this video, we will discuss about creating and scheduling the SQL Server Job to unlock user
accounts.
First let us write the update query to unlock the user accounts. For example, The organization's
policy is that, the user account can only be unlocked after 24 hours, since the account is locked.
The update query to satisfy the organization's policy is shown below. DateDiff function is used in
the update query. If you are new to DateTime functions in SQL Server, please check this video
by clicking here.
Update tblUsers
set RetryAttempts = null, IsLocked = 0, LockedDateTime = null
where IsLocked = 1
and datediff(HOUR,LockedDateTime,GETDATE()) > 24
Let us now, schedule this update query to run every 30 minutes, every day. This can be
very easily done using sql server agent jobs. In this video, we will discuss about creating and
scheduling sql server agent jobs, for sql server 2008.
1. Open sql serevr management studio
2. In the object explorer, check if "SQL Server Agent" is running.
3. If "SQL Server Agent" is not running, right click and select "Start".
This job, will run every 30 minutes daily, and unlocks the accounts that has been locked for
more than 24 hours.
Step 1:
The first step is to design a page, that allows the user to enter their user name, for
requesting, the reset of the password. Add a webform , with name "ResetPassword.aspx" to the
"Registration" folder. The web.config file in this folder, allows anonymous access to all the
pages without having the need to login. We discussed about having multiple web.config files
and allowing anonymous access to a set of pages in Part 91 of this video series. Click here to
watch Part 91, before proceeding.
Step 2:
Copy and paste the following HTML on "ResetPassword.aspx" page.
<div style="font-family:Arial">
<table style="border: 1px solid black; width:300px">
<tr>
<td colspan="2">
<b>Reset my password</b>
</td>
</tr>
<tr>
<td>
User Name
</td>
<td>
<asp:TextBox ID="txtUserName" Width="150px" runat="server">
</asp:TextBox>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="btnResetPassword" runat="server"
Width="150px" Text="Reset Password" onclick="btnResetPassword_Click" />
</td>
</tr>
<tr>
<td colspan="2">
<asp:Label ID="lblMessage" runat="server"></asp:Label>
</td>
</tr>
</table>
</div>
Step 3:
Create a table "tblResetPasswordRequests" in sql server. This table is going to store a
unique GUID (Globally Unique Identifier) along with the user id, each time a user requests a
password recovery. This GUID will then be passed as part of the querystring in the link to the
password reset page. This link will then be emailed to the email address that is associated with
the user id. When a user clicks on the link the page will look up the GUID
in "tblResetPasswordRequests" table and get the user id from there allowing the user to
change their password. I didn't use, UserId, as the querystring parameter, because it maybe
open to abuse.
Step 4:
Create a stored procedure to check if the username exists, and to insert a row into
"tblResetPasswordRequests" table.
Create proc spResetPassword
@UserName nvarchar(100)
as
Begin
Declare @UserId int
Declare @Email nvarchar(100)
Step 5:
Invoke the stored procedure and email the link, to the email address that is registered against
the username. Copy and paste the following code in ResetPassword.aspx.cs page.
cmd.Parameters.Add(paramUsername);
con.Open();
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
if (Convert.ToBoolean(rdr["ReturnCode"]))
{
SendPasswordResetEmail(rdr["Email"].ToString(), txtUserName.Text,
rdr["UniqueId"].ToString());
lblMessage.Text = "An email with instructions to reset your password is sent to your
registered email";
}
else
{
lblMessage.ForeColor = System.Drawing.Color.Red;
lblMessage.Text = "Username not found!";
}
}
}
}
mailMessage.IsBodyHtml = true;
mailMessage.Body = sbEmailBody.ToString();
mailMessage.Subject = "Reset Your Password";
SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587);
smtpClient.EnableSsl = true;
smtpClient.Send(mailMessage);
}
Step 6:
Add a webform with name, "ChangePassword.aspx", to "Registration" folder. Copy and paste
the following HTML in the aspx page. In the next video session we will implement
ChangePassword page.
<h1>Change Password Page</h1>
Implementing change password page in asp.net - Part 96
Suggested Videos
Part 93 - Forms authentication and locking user accounts
Part 94 - Unlocking the locked user accounts
Part 95 - Implementing password reset link
In this video we will discuss about, implementing change password page in asp.net. When
the user clicks on password reset link, the user lands on ChangePassword.aspx page. In Part
95, we discussed about, generating and emailing the password reset link. The password reset
link looks as shown below.
http://localhost/WebApplication1/Registration/ChangePassword.aspx?uid=c19b3a4a-
7fd2-47dc-9c2a-be541daed8fa
Notice that, ChangePassword.aspx page has a query string "uid". This GUID(Globally
unique identifier), is used to look up UserID, for whom the password needs to be changed. After
updating the password, delete the row from "tblResetPasswordRequests", so the link becomes
invalid after the user has changed his/her password. Since, user id's are integers, they may be
open for abuse as it is very easy to use random integers as query string values, to change other
users password.
if(@UserId is null)
Begin
-- If UserId does not exist
Select 0 as IsPasswordChanged
End
Else
Begin
-- If UserId exists, Update with new password
Update tblUsers set
[Password] = @Password
where Id = @UserId
Select 1 as IsPasswordChanged
End
End
</td>
<td>
<asp:Button ID="btnSave" runat="server"
Text="Save" onclick="btnSave_Click" Width="70px" />
</td>
</tr>
<tr>
<td colspan="2">
<asp:Label ID="lblMessage" runat="server">
</asp:Label>
</td>
</tr>
<tr>
<td colspan="2">
<asp:ValidationSummary ID="ValidationSummary1"
ForeColor="Red" runat="server" />
</td>
</tr>
</table>
</div>
con.Open();
return Convert.ToBoolean(cmd.ExecuteScalar());
}
}
private bool IsPasswordResetLinkValid()
{
List<SqlParameter> paramList = new List<SqlParameter>()
{
new SqlParameter()
{
ParameterName = "@GUID",
Value = Request.QueryString["uid"]
}
};
In the next video, we will discuss about changing password by providing the current password.
In this video we will discuss about, Changing password by providing current password. In
real time, users can change their password any time, by providing their current password.
Select 1 as IsPasswordChanged
End
Else
Begin
Select 0 as IsPasswordChanged
End
End
ChangePassword.aspx HTML
<div style="font-family: Arial">
<table style="border: 1px solid black">
<tr>
<td colspan="2">
<b>Change Password</b>
</td>
</tr>
<tr id="trCurrentPassword" runat="server">
<td>
Current Password
</td>
<td>
:<asp:TextBox ID="txtCurrentPassword" TextMode="Password"
runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidatorCurrentPassword"
runat="server" ErrorMessage="Current Password required"
Text="*" ControlToValidate="txtCurrentPassword" ForeColor="Red">
</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
New Password
</td>
<td>
:<asp:TextBox ID="txtNewPassword" TextMode="Password"
runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidatorNewPassword"
runat="server" ErrorMessage="New Password required"
</td>
<td>
<asp:Button ID="btnSave" runat="server"
Text="Save" onclick="btnSave_Click" Width="70px" />
</td>
</tr>
<tr>
<td colspan="2">
<asp:Label ID="lblMessage" runat="server">
</asp:Label>
</td>
</tr>
<tr>
<td colspan="2">
<asp:ValidationSummary ID="ValidationSummary1"
ForeColor="Red" runat="server" />
</td>
</tr>
</table>
</div>
ChangePassword.aspx.cs code:
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["uid"] == null && User.Identity.Name == "")
{
Response.Redirect("~/Login.aspx");
}
if (!IsPostBack)
{
if (Request.QueryString["uid"] != null)
{
if (!IsPasswordResetLinkValid())
{
lblMessage.ForeColor = System.Drawing.Color.Red;
lblMessage.Text = "Password Reset link has expired or is invalid";
}
trCurrentPassword.Visible = false;
}
else if (User.Identity.Name != "")
{
trCurrentPassword.Visible = true;
}
}
}
con.Open();
return Convert.ToBoolean(cmd.ExecuteScalar());
}
}
If a user repeatedly enters the wrong password. The accounts are locked to prevent hackers
from guessing passwords and making dictionary attacks. In Part 94, of this video series we have
discussed about un-locking user accounts, using a SQL Server agent job. Please watch Part
94, before proceeding with this video.
In this video, we will discuss about unlocking the locked user accounts, using a web page
that lists all the locked user accounts. From this page, the help desk agent, can unlock the
account by clicking a button. This is not as dangerous as running a manual update query, but
still a manual process and may be in-efficient.
Stored procedure to get the information about, all the locked user accounts.
Create proc spGetAllLocakedUserAccounts
as
Begin
Select UserName, Email, LockedDateTime,
DATEDIFF(hour, LockedDateTime, GETDATE()) as HoursElapsed
from tblUsers
where IsLocked = 1
End
Add a webform, with name "LockedAccounts.aspx". Copy and paste the following HTML on
this page.
<div style="font-family:Arial">
<asp:GridView ID="gvLockedAccounts" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="UserName" HeaderText="User Name" />
<asp:BoundField DataField="Email" HeaderText="Email" />
<asp:BoundField DataField="LockedDateTime"
HeaderText="Locked Date & Time" />
<asp:BoundField DataField="HoursElapsed" HeaderText="Hours Elapsed" >
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:TemplateField HeaderText="Enable">
<ItemTemplate>
<asp:Button ID="btnEnable" runat="server" Text="Enable"
Enabled='<%#Convert.ToInt32(Eval("HoursElapsed")) > 24%>'/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
"LockedAccounts.aspx.cs" code
protected void Page_Load(object sender, EventArgs e)
{
if (User.Identity.Name.ToLower() == "test")
{
if (!IsPostBack)
{
GetData();
}
}
else
{
Response.Redirect("~/AccessDenied.aspx");
}
}
con.Open();
gvLockedAccounts.DataSource = cmd.ExecuteReader();
gvLockedAccounts.DataBind();
}
}
In the next video session, we will discuss about implementing the "Enable" button.
This is continuation to Part 98. To implement, the "Enable" button, make the following
changes to the gridview control.
First Change: Specify the CommandArgument attribute of the Button control in the Template
column.
<asp:TemplateField HeaderText="Enable">
<ItemTemplate>
<asp:Button ID="btnEnable" runat="server" CommandArgument='<%#
Eval("UserName") %>'
Text="Enable" Enabled='<%#Convert.ToInt32(Eval("HoursElapsed")) > 24%>'/>
</ItemTemplate>
</asp:TemplateField>
Second Change: Generate the "RowCommand" event handler for the GridView control.
1. Right Click on the GridView Control and Select properties.
2. In the "Properties Window", click on events icon.
3. In the events windows, double click on the text box next to "Row Command" event.
With these 2 changes the HTML of the "LockedAccounts.aspx" should look as shown below.
<div style="font-family: Arial">
<asp:GridView ID="gvLockedAccounts" runat="server" AutoGenerateColumns="False"
OnRowCommand="gvLockedAccounts_RowCommand">
<Columns>
<asp:BoundField DataField="UserName" HeaderText="User Name" />
<asp:BoundField DataField="Email" HeaderText="Email" />
<asp:BoundField DataField="LockedDateTime" HeaderText="Locked Date & Time" />
<asp:BoundField DataField="HoursElapsed" HeaderText="Hours Elapsed">
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:TemplateField HeaderText="Enable">
<ItemTemplate>
<asp:Button ID="btnEnable" CommandArgument='<%# Eval("UserName") %>'
runat="server"
Text="Enable" Enabled='<%#Convert.ToInt32(Eval("HoursElapsed")) > 24%>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
cmd.Parameters.Add(paramUserName);
con.Open();
cmd.ExecuteNonQuery();
}
}
The certificate authority acts as a clearing house to verify the server’s identity over the Internet.
When a browser requests a page over https, the browser also, requests the server certificate
and checks it against a list of trusted sites provided by the certificate authority. If the server
certificate does not match one of the sites already authorized by the user, or if the server
certificate does not match the Web address for which it was registered, or if there are any other
problems with the server certificate, a warning message is displayed. The warning message
from internet explorer is shown below.
Besides providing encryption and decryption for secure data transmission, certificate authority
also provides assurance to users that a website is authentic.
It is also possible to generate our own server certificates, using a tool called makecert.exe.
This tool comes with visual studio and can be used from visual studio command prompt. The
certificates that are generated using this tool, can only be used for testing purposes and not for
production use. We will discuss about generating and installing server certificates in our next
video session.
The generated test certificate, is also automatically installed into the certificate store.
MakeCert.exe tool can be used as another way to generate, test certificates. The following link
from microsoft explains, various options that can be used with this tool. This is a command line
tool and must be run from visual studio command prompt.
http://msdn.microsoft.com/en-us/library/bfsktky3.aspx
At this point, you will be able to access your application using both HTTP and HTTPS protocol.
When the site is accessed over HTTPS, you may receive a browser warning about the
authenticity of the website. In a later video session we will discuss about resolving this.
If you want to dis-allow, access over HTTP protocol there are 2 ways
First Way: Remove HTTP binding at the IIS Server level. This option will prevent all the web
applications, running on that server to use only HTTPS binding.
Second Way: Let both the bindings be available at the server level and configure SSL settings
at an application or web site level.
1. Select your web application in IIS
2. Double click "SSL Settings" from the features window
3. Make sure "Require SSL" check box is checked.
4. Click "Apply" under "Actions" pane
Now, if you try to access the application using HTTP instead of HTTPS, you will get an error
HTTP Error 403.4 - Forbidden
The page you are trying to access is secured with Secure Sockets Layer (SSL)
Use Import and Export feature of IIS to import and export certificates
In this video we will discuss about redirecting users from HTTP to HTTPS. In the previous
session, we discussed about implementing SSL in asp.net. Please watch Part 101, before
proceeding with this video.
To redirect users from HTTP to HTTPS automatically, there are several ways. In this video
we will discuss about using "url rewrite" module. There are 3 simple steps
Step 1:
Please download and install "URL ReWrite" module from the following link.
http://www.iis.net/downloads/microsoft/url-rewrite
Step 2:
Uncheck "Require SSL" option from "SSL Settings" for the web application in IIS.
Step 3:
Copy and paste the following in the root web.config file of your application.
<system.webServer>
<httpRedirect enabled="false" destination="" httpResponseStatus="Found" />
<rewrite>
<rules>
<rule name="HTTP to HTTPS Redirection" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}"
redirectType="Found" />
</rule>
</rules>
</rewrite>
</system.webServer>
Now try to navigate to the application using HTTP, you will automatically be redirected to
HTTPS.
These rules can also be created in IIS directly using the "URL Rewrite" module
In the next video we will discuss about using "IIS Error Pages" to redirect users from HTTP to
HTTPS automatically.
In this video we will discuss about redirecting users from HTTP to HTTPS, using "IIS Error
Pages". In the previous session, we discussed about redirecting users using "URL ReWrite"
module.
Custom error pages can be set at the server level or at a specific application level in IIS.
In this demo, we will discuss about setting custom error pages at the server level. There are 3
simple steps.
Step 1:
Make sure "Require SSL" option from "SSL Settings" is checked for your web application in
IIS. Now, browse the web site, using HTTP, and you will receive the following error. Pay
attention to HTTP error code - 403.4, which we will be using later.
HTTP Error 403.4 - Forbidden
The page you are trying to access is secured with Secure Sockets Layer (SSL).
Step 2:
Copy and paste the following HTML in a notepad and save it
as "RedirectToHttps.htm" in "C:\inetpub".
<html>
<head>
<title>
Redirecting to HTTPS
</title>
</head>
<script language="JavaScript">
function redirectHttpToHttps()
{
var httpURL= window.location.hostname + window.location.pathname;
var httpsURL= "https://" + httpURL;
window.location = httpsURL;
}
redirectHttpToHttps();
</script>
<body>
</body>
</html>
Step 3:
1. In IIS, select the "Server Name" and double click "Error Pages" to open the feature.
2. Click on "Add" link under "Actions"
3. Set Status Code = 403.4, File Path = C:\Inetpub\RedirectToHttps.htm and click "OK"
4. Now click "Edit Feature Settings" link under "Actions"
5. Select "Custom Error Pages" and Path = C:\inetpub\RedirectToHttps.htm
Now, access the application using HTTP. You will be automatically redirected to HTTPS.
Web user controls combine one or more server or HTML controls on a Web user control
page, which can, in turn, be used on a Web form as a single control. For example, to capture
dates from the end user on a webform, we need a TextBox, ImageButton and, a Calendar
control. A web form to capture date of birth is shown below in the image.
If, I am capturing dates, on multiple web forms, rather than repeating the same HTML mark up
and code, on each and every web form, we can encapsulate everything into a single web user
control, which in turn, can be used on multiple web forms. This way we are reusing the same
code, which saves a lot of time in terms of development and testing. So in short, user controls,
increase re-usability of code, implement encapsulation and reduce development and
maintenance time.
Designing and implementing web user controls is very similar to web forms.Web forms,
have the extension of .aspx, where as web user controls have the extension of .ascx. Webforms
begin with @Page directive and can have <html>, <head>, and <body> elements, where as a
web user controls begin with @ Control directive and cannot have html, head, and body
elements. Just, like webforms, user controls also have code behind files.
In this demo, we will create a custom calendar user control, that can be reused on multiple
webforms. To create a user control
1. Right click on the web application project in solution explorer
2. Select Add >> New Item
3. From the "Add New Item" dialog box, select "Web User Control"
4. Set Name = CalendarUserControl
5. Click on "Add"
Notice that, CalendarUserControl.ascx page is created. Copy and paste the following HTML.
<asp:TextBox ID="txtDate" runat="server" Width="115px"></asp:TextBox>
<asp:ImageButton ID="ImgBtn" runat="server"
ImageUrl="~/Images/Calendar.png" onclick="ImgBtn_Click" />
<asp:Calendar ID="Calendar1" runat="server"
onselectionchanged="Calendar1_SelectionChanged">
</asp:Calendar>
CalendarUserControl.ascx.cs code
public partial class CalendarUserControl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Calendar1.Visible = false;
}
}
We are done creating the calendar user control. In the next video, we will discuss about
using this calendar control on a web form.
In the previous video, we discussed about creating a calendar user control. Please watch Part
104, before proceeding with this video. In this video we will discuss about
1. Adding and using user controls on a webform
2. Adding properties to the user control
Notice, the "tagprefix" and "tagname" in the "Register" directive. These are used in the
control declaration. For asp.net controls, the "tagprefix" is "asp". Tagprefix, can be changed, if
you wish to do so.
If you intend to add the user control on multiple web forms, rather than including the
"Register" directive on each and every web form, every time, the control can be registered once
in web.config file and can be used on any number of web forms, without the "Register" directive.
<system.web>
<pages>
<controls>
<add src="~/CalendarUserControl.ascx" tagName="CalendarUserControl" tagPrefix="uc1"/>
</controls>
</pages>
</system.web>
At this point, you get the following error, if both, the user control and the webform are in the
same directory. This limitation is by design due to an internal design consideration for
performance.
The page '/WebForm2.aspx' cannot use the user control '/CalendarUserControl.ascx', because
it is registered in web.config and lives in the same directory as the page.
To solve this error move the user control to a different folder, and update the "src" attribute of
the "Register" directive in web.config file accordingly.
For example, drag and drop a button control on the same webform. when I click this button, we
want to print the selected date. To do this let's add the following SelectedDate property for the
CalendarUserControl.
public string SelectedDate
{
get
{
return txtDate.Text;
}
set
{
txtDate.Text = value;
}
}
On the webform, in the button click event, I should now be able to retrieve, the selected date
using "SelectedDate" property of the "CalendarUserControl" as shown below.
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write(CalendarUserControl1.SelectedDate);
}
You can also set this property declaratively in the HTML at design time as shown below. When
this webform, loads, it shows the date, that we have set.
<uc1:CalendarUserControl SelectedDate="01/01/2013" ID="CalendarUserControl1" runat="serv
er" />
But one limitation, here with the user control, is that the design time value is not shown in the
control at design time. This is by design, and there are 2 ways to solve this issue.
1. Create a custom control instead of user control.
2. Compile the user control into a DLL.
In the next video session we will discuss about adding "events" to our "CalendarUserControl"
Most people feel "events and delegates" are complex and difficult to understand. Events and
delegates are not that complex to understand, if the basics are right. To get the most out of this
video, I strongly recommend to watch parts 36, 37 , 38 and 39 from C# Video series, and parts
104 and 105 from asp.net video series, before proceeding with this video.
Very important points to keep in mind, when understanding "Events and Delegates"
1. Delegates are function pointers, and their syntax is very similar to that of a function.
2. Events are variables of type delegates with an event keyword.
If these points are not clear at the moment, don't worry, they will be much clear as we
progress.
At the moment, the CalendarUserControl does not have any custom events. Let us say,
we want to raise CalendarVisibilityChanged event every time the visibility of the calendar
changes. The visibility of the calendar is toggled by clicking on the image button.
The following are the steps to raise CalendarVisibilityChanged event from the
CalendarUserControl
Step 1: Create CalendarVisibilityChangedEventArgs class that will contain the event data.
public class CalendarVisibilityChangedEventArgs : EventArgs
{
private bool _isCalendarVisible;
Step 4: Create a protected virtual method to raise the event. Since this method is protected
and virtual, all classes deriving from the CalendarUserControl class can overridde this
method, if they wish to do so. This method enables the derived classes to do some additional
work before the event can be raised. Just before raising the event, we are checking
if CalendarVisibilityChanged is null. If you are not sure about this, please don't worry. This
will be much clear in the next video session, when we discuss about consuming
CalendarVisibilityChanged event.
protected virtual void OnCalendarVisibilityChanged(CalendarVisibilityChangedEventArgs e)
{
if (CalendarVisibilityChanged != null)
{
CalendarVisibilityChanged(this, e);
}
}
Step 5: Finally raise the event, whenever the visibility of the Calendar is changed in the
CalendarUserControl. The calendar visibility is changed, whenevr the user clicks on the image
button and when the date in the calendar is selected. So, raise "CalendarVisibilityChanged"
event from ImgBtn_Click() and Calendar1_SelectionChanged(). Before raising the event, create
and instance of "CalendarVisibilityChangedEventArgs" and pass event data, that is "true" or
"false" to the contrustor of this class.
protected void ImgBtn_Click(object sender, ImageClickEventArgs e)
{
if (Calendar1.Visible)
{
Calendar1.Visible = false;
CalendarVisibilityChangedEventArgs calendarVisibilityChangedEventData =
new CalendarVisibilityChangedEventArgs(false);
OnCalendarVisibilityChanged(calendarVisibilityChangedEventData);
}
else
{
Calendar1.Visible = true;
CalendarVisibilityChangedEventArgs calendarVisibilityChangedEventData =
new CalendarVisibilityChangedEventArgs(true);
OnCalendarVisibilityChanged(calendarVisibilityChangedEventData);
}
}
Calendar1.Visible = true;
CalendarVisibilityChangedEventArgs calendarVisibilityChangedEventData =
new CalendarVisibilityChangedEventArgs(true);
OnCalendarVisibilityChanged(calendarVisibilityChangedEventData);
}
}
public delegate
void CalendarVisibilityChangedEventHandler(object sender, CalendarVisibilityChangedEventAr
gs e);
In Part 106 of this video series, we discussed about raising custom events from a user control.
Please watch part 106, before proceeding.
That's it. Run the project and click on the calendar image to toggle the display, the custom event
will be raised and handled. You should see a message "Calendar Visible = true" or "Calendar
Visible = false" depending on the visibility of the calendar control.
Understanding the importance of, checking if the event is null, before raising the event
Now comment the line that registers event handler method in the Page_Load() event. Run the
application and click on the image button. Nothing happens and also we don't get any run time
errors.
Now comment the line that checks for null in "OnCalendarVisibilityChanged()" method as shown
below.
protected virtual void OnCalendarVisibilityChanged(CalendarVisibilityChangedEventArgs e)
{
// NULL check
//if (CalendarVisibilityChanged != null)
//{
CalendarVisibilityChanged(this, e);
//}
}
Run the application and click on the image button. You should get a "NullReferenceException".
The exception is due to CalendarVisibilityChanged() being null. So, if there are no subscribers
for the event, that is, if there are no event handler methods registered with
CalendarVisibilityChanged event, and if we try to raise the event, we get the exception. To avoid
this it is always better to check for null, before raising the event.
In the next video, we will discuss about raising another custom event
from CalendarUserControl.
In this video we will discuss about raising another custom event from
CalendarUserControl. If you have not watched Parts 106 and 107, I would strongly encourage
you to do so before continuing with this video. In this part, we will discuss about raising
DateSelected custom event, whenever, the user selects a "Date" from
the "CalendarUserControl".
The following are the steps to raise this event
Step 1: Create "DateSelectedEventArgs" class that will contain the event data.
public class DateSelectedEventArgs : EventArgs
{
private DateTime _selectedDate;
Step 5: Finally raise the event, whenever a date is selected from the
CalendarUserControl.
protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{
txtDate.Text = Calendar1.SelectedDate.ToShortDateString();
DateSelectedEventArgs dateSelectedEventData
= new DateSelectedEventArgs(Calendar1.SelectedDate);
OnDateSelection(dateSelectedEventData);
Finally, here is the complete CalendarUserControl code, with both the custom events.
1. "DateSelected" event
2. "CalendarVisibilityChanged" event
Calendar1.Visible = true;
CalendarVisibilityChangedEventArgs calendarVisibilityChangedEventData
= new CalendarVisibilityChangedEventArgs(true);
OnCalendarVisibilityChanged(calendarVisibilityChangedEventData);
}
}
Calendar1.Visible = false;
CalendarVisibilityChangedEventArgs calendarVisibilityChangedEventData
= new CalendarVisibilityChangedEventArgs(false);
OnCalendarVisibilityChanged(calendarVisibilityChangedEventData);
}
public delegate
void CalendarVisibilityChangedEventHandler(object sender, CalendarVisibilityChangedEventAr
gs e);
Here is the complete code for WebForm1.aspx.cs, that consumes both the custom events.
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
CalendarUserControl1.CalendarVisibilityChanged +=
new CalendarVisibilityChangedEventHandler(CalendarUserControl1_CalendarVisibilityC
hanged);
CalendarUserControl1.DateSelected +=
new DateSelectedEventHandler(CalendarUserControl1_DateSelected);
}
protected
void CalendarUserControl1_CalendarVisibilityChanged(object sender, CalendarVisibilityChange
dEventArgs e)
{
Response.Write("Calendar Visible = " + e.IsCalendarVisible.ToString());
}
protected
void CalendarUserControl1_DateSelected(object sender, DateSelectedEventArgs e)
{
Response.Write("Selected Date = " + e.SelectedDate.ToShortDateString());
}
Hopefully, by now we should have a good understanding of events & delegates, and raising and
consuming custom events. If there are any questions or feedback, please feel free to leave a
comment.
In this video we will discuss about loading user controls dynamically. Normally, we drag
and drop user controls on the webform, at design time. However, there could be several
reasons in real time, for loading user controls dynamically. For example, depending on logged in
user preferences like age, gender, location etc, we may want to load different user controls.
Along the same line, based on Admin and Non-Admin users, different user controls should be
loaded.
In most of the internet articles, I read that, if we want, the dynamically created controls, to
maintain viewstate across postback, the controls should be loaded in Page_Init() event of the
webform. However, I used the Page_Load() event, but the controls are still able to retain their
state across postback.
Let us add the CalendarUserControl to the webform. Drag and drop, PlaceHolder control,
where we want the controls to be on the page. If we don't use the PlaceHolderControl, and if we
try to add controls using the following code, we may get a runtime HttpException.
Control 'CU1_txtDate' of type 'TextBox' must be placed inside a form tag with runat=server
this.Page.Controls.Add(LoadControl("~/UserControls/CalendarUserControl.ascx"));
WebForm1.aspx HTML
<form id="form1" runat="server">
<div>
<asp:PlaceHolder ID="PlaceHolder1" runat="server">
</asp:PlaceHolder>
<br />
<asp:Button ID="Button1" runat="server" Text="Button"
onclick="Button1_Click" />
</div>
</form>
Webform1.aspx.cs Code
protected void Page_Load(object sender, EventArgs e)
{
CalendarUserControl calendarUserControl =
(CalendarUserControl)LoadControl("~/UserControls/CalendarUserControl.ascx");
calendarUserControl.ID = "CU1";
calendarUserControl.DateSelected +=
new DateSelectedEventHandler(calendarUserControl_DateSelected);
calendarUserControl.CalendarVisibilityChanged +=
new CalendarVisibilityChangedEventHandler(calendarUserControl_CalendarVisibilityChan
ged);
PlaceHolder1.Controls.Add(calendarUserControl);
}
protected
void calendarUserControl_CalendarVisibilityChanged(object sender, CalendarVisibilityChanged
EventArgs e)
{
Response.Write("Calende Visible = " + e.IsCalendarVisible.ToString());
}
In the next video session, we will discuss about dynamically loading standard asp.net user
controls.
if (DropDownList1.SelectedValue == "TB")
{
TB.Visible = true;
}
else if (DropDownList1.SelectedValue == "DDL")
{
DDL.Visible = true;
}
}
This question was asked by 3 to 4 youtube subscribers of my channel. There are several
reasons or situations in real time, where we need to navigate to a specific month and an year in
an asp.net calendar control. For example, if the date of birth of a person is in the year 1960, we
may have to click on the calendar control several times to navigate to that year.
Let's see, how to skip months and years, so that the dates that are too far in the past or future
can be easily selected. To achieve this
Step 1: Create an XML file with name Years.XML. Copy and paste the following XML.
<?xml version="1.0" encoding="utf-8" ?>
<Years>
<Year>
<Number>2000</Number>
<Value>2000</Value>
</Year>
<Year>
<Number>2001</Number>
<Value>2001</Value>
</Year>
<Year>
<Number>2002</Number>
<Value>2002</Value>
</Year>
<Year>
<Number>2003</Number>
<Value>2003</Value>
</Year>
<Year>
<Number>2004</Number>
<Value>2004</Value>
</Year>
<Year>
<Number>2005</Number>
<Value>2005</Value>
</Year>
<Year>
<Number>2006</Number>
<Value>2006</Value>
</Year>
<Year>
<Number>2007</Number>
<Value>2007</Value>
</Year>
<Year>
<Number>2008</Number>
<Value>2008</Value>
</Year>
<Year>
<Number>2009</Number>
<Value>2009</Value>
</Year>
<Year>
<Number>2010</Number>
<Value>2010</Value>
</Year>
<Year>
<Number>2011</Number>
<Value>2011</Value>
</Year>
<Year>
<Number>2012</Number>
<Value>2012</Value>
</Year>
<Year>
<Number>2013</Number>
<Value>2013</Value>
</Year>
</Years>
Step 2: Create an XML file with name Months.XML. Copy and paste the following XML.
<?xml version="1.0" encoding="utf-8" ?>
<Months>
<Month>
<Number>1</Number>
<Name>January</Name>
</Month>
<Month>
<Number>2</Number>
<Name>February</Name>
</Month>
<Month>
<Number>3</Number>
<Name>March</Name>
</Month>
<Month>
<Number>4</Number>
<Name>April</Name>
</Month>
<Month>
<Number>5</Number>
<Name>May</Name>
</Month>
<Month>
<Number>6</Number>
<Name>June</Name>
</Month>
<Month>
<Number>7</Number>
<Name>July</Name>
</Month>
<Month>
<Number>8</Number>
<Name>August</Name>
</Month>
<Month>
<Number>9</Number>
<Name>September</Name>
</Month>
<Month>
<Number>10</Number>
<Name>October</Name>
</Month>
<Month>
<Number>11</Number>
<Name>November</Name>
</Month>
<Month>
<Number>12</Number>
<Name>December</Name>
</Month>
</Months>
Step 4: Copy and paste the following code in the code-behind file
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadYears();
LoadMonths();
}
}
DropDownList2.DataTextField = "Name";
DropDownList2.DataValueField = "Number";
DropDownList2.DataSource = dsMonths;
DropDownList2.DataBind();
}
DropDownList1.DataTextField = "Number";
DropDownList1.DataValueField = "Number";
DropDownList1.DataSource = dsYears;
DropDownList1.DataBind();
}
At this point, run the project and you should be able to skip to any month and year in an
asp.net calendar control. This code can be encapsulated in an user control, and reused
anywhere in the entire project.
In asp.net video series, in parts 104 to 111, we have discussed about asp.net user
controls. Please watch user control videos by clicking here before proceeding with this video.
In this video we will discuss about, asp.net composite custom controls. Unlike
UserControls, Composite controls are created completely in code. In the next few videos we will
be creating a composite custom calendar control, that is similar to the one in the image below.
In parts 104 to 111, we have discussed about achieving the same, by building asp.net
CalendarUserControl.
Copy and paste the following code in CustomCalendar.cs file. The code is well commented, to
describe the purpose of various attributes and methods, that we used.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace CustomControls
{
// Specifies the default tag to generate for our CustomCalendar control
// when it is dragged from visual studio toolbox on to the webform
[ToolboxData("<{0}:CustomCalendar runat=server></{0}:CustomCalendar>")]
// All composite custom controls inheirt from the base CompositeControl class
// that contains all the common functionality needed by all composite controls.
public class CustomCalendar : CompositeControl
{
// Child controls required by the CustomCalendar control
TextBox textBox;
ImageButton imageButton;
Calendar calendar;
At this point we are done, building the composite custom control. However, there will be
several problems with this control. In the upcoming videos, we will discuss about solving the
problems one by one. In our next video session, we will discuss about using this composite
custom control in an asp.net web application.
Adding composite custom controls to visual studio tool box - Part 113
Suggested Videos
Part 110 - Loading asp.net controls dynamically
Part 111 - Navigating to a specific month and an year in an asp.net calendar control
Part 112 - ASP.NET custom server controls
In Part 112, we discussed about building composite custom controls. Please watch Part
112, before proceeding with this video.
Notice that a "Register" directive is also automatically, added under "Page" directive.
<%@ Register assembly="CustomControls" namespace="CustomControls" tagprefix="cc1" %>
At this point, run the project and notice that the "CustomCalendar" control has several issues.
For example
1. There is no image associated with the Image button
2. The calendar is always visible. We want the Calendar to be invisible at first. The calendar
should be shown, when the user clicks the image button.
3. Also, when select a date with in calendar, nothing happens. We want the text box, to be
populated with the selected date.
At the moment, the calendar composite control does not have an image associated with
the image button. Let us create "ImageButtonImageUrl" property to associate an image.
Flip to the asp.net web application project, where the CustomCalendar is being tested.
Please remove the CustomCalendar control from visual studio tool boox, and add it again. Drag
and drop CustomCalendar control on to the webform. Right click and select properties. Notice
that "ImageButtonImageUrl" property is now displayed in the properties window. If you change
the properties display mode to "Categorized", then notice that "ImageButtonImageUrl" property
is displayed under "Appearance" category. Also, notice that, when the property is selected, the
property description is displayed at the bottom of the properties window.
To correct the design time problem, override RecreateChildControls() method. This method
is called by visual studio designer, to recreate child controls at design time. Copy and paste the
following code CustomCalendar.cs file.
protected override void RecreateChildControls()
{
EnsureChildControls();
}
At the moment, another cosmetic issue with this control is that, the "Image Button" and the
"TextBox" are not properly alligned. To correct this problem, change the Render() method in
CustomCalendar.cs file, as shown below. This method adds, cellpadding attribute, and puts the
"textbox" and "calendar" in a table.
writer.RenderBeginTag(HtmlTextWriterTag.Table);
writer.RenderBeginTag(HtmlTextWriterTag.Tr);
writer.RenderBeginTag(HtmlTextWriterTag.Td);
textBox.RenderControl(writer);
writer.RenderEndTag();
writer.RenderBeginTag(HtmlTextWriterTag.Td);
imageButton.RenderControl(writer);
writer.RenderEndTag();
writer.RenderEndTag();
writer.RenderEndTag();
calendar.RenderControl(writer);
}
namespace CustomControls
{
[ToolboxData("<{0}:CustomCalendar runat=server></{0}:CustomCalendar>")]
public class CustomCalendar : CompositeControl
{
TextBox textBox;
ImageButton imageButton;
Calendar calendar;
[Category("Appearance")]
[Description("Sets the image icon for the calendar control")]
public string ImageButtonImageUrl
{
get
{
EnsureChildControls();
return imageButton.ImageUrl != null ? imageButton.ImageUrl : string.Empty;
}
set
{
EnsureChildControls();
imageButton.ImageUrl = value;
}
}
this.Controls.Add(textBox);
this.Controls.Add(imageButton);
this.Controls.Add(calendar);
}
writer.RenderBeginTag(HtmlTextWriterTag.Table);
writer.RenderBeginTag(HtmlTextWriterTag.Tr);
writer.RenderBeginTag(HtmlTextWriterTag.Td);
textBox.RenderControl(writer);
writer.RenderEndTag();
writer.RenderBeginTag(HtmlTextWriterTag.Td);
imageButton.RenderControl(writer);
writer.RenderEndTag();
writer.RenderEndTag();
writer.RenderEndTag();
calendar.RenderControl(writer);
}
}
}
In Parts 112 to 114 in asp.net video series, we discussed about the basics of composite
custom controls in asp.net. Please watch Parts 112 to 114, before proceeding with this
video. Click here to access asp.net video tutorial
3. Populating the textbox control, with a date that is selected from the calendar
4. Retrieving the selected date from the CustomCalendar control on a webform.
Hiding the calendar control, when the CustomCalendar control is first loaded:
To hide the calendar set calendar.Visible = false in CreateChildControls() method in
CustomCalendar.cs file
Displaying and hiding the calendar, when the image button is clicked:
To toggle the visibility of the calendar object, first register an event handler method, with the
click event of the image button in CreateChildControls() method as shown below.
imageButton.Click += new ImageClickEventHandler(imageButton_Click);
Populating the textbox control, with a date that is selected from the calendar:
First register an event handler method, with the "SelectionChanged" event of the calendar in
CreateChildControls() method as shown below.
set
{
if (value != null)
{
EnsureChildControls();
textBox.Text = value.ToShortDateString();
}
else
{
EnsureChildControls();
textBox.Text = "";
}
}
}
namespace CustomControls
{
[ToolboxData("<{0}:CustomCalendar runat=server></{0}:CustomCalendar>")]
public class CustomCalendar : CompositeControl
{
TextBox textBox;
ImageButton imageButton;
Calendar calendar;
[Category("Appearance")]
[Description("Sets the image icon for the calendar control")]
public string ImageButtonImageUrl
{
get
{
EnsureChildControls();
return imageButton.ImageUrl != null ? imageButton.ImageUrl : string.Empty;
}
set
{
EnsureChildControls();
imageButton.ImageUrl = value;
}
}
[Category("Appearance")]
[Description("Gets or sets the selected date of custom calendar control")]
public DateTime SelectedDate
{
get
{
EnsureChildControls();
return string.IsNullOrEmpty(textBox.Text) ? DateTime.MinValue
: Convert.ToDateTime(textBox.Text);
}
set
{
if (value != null)
{
EnsureChildControls();
textBox.Text = value.ToShortDateString();
}
else
{
EnsureChildControls();
textBox.Text = "";
}
}
}
writer.RenderBeginTag(HtmlTextWriterTag.Table);
writer.RenderBeginTag(HtmlTextWriterTag.Tr);
writer.RenderBeginTag(HtmlTextWriterTag.Td);
textBox.RenderControl(writer);
writer.RenderEndTag();
writer.RenderBeginTag(HtmlTextWriterTag.Td);
imageButton.RenderControl(writer);
writer.RenderEndTag();
writer.RenderEndTag();
writer.RenderEndTag();
calendar.RenderControl(writer);
}
}
}
Please build CustomControls project, and add a reference to CustomCalendar in asp.net web
application for testing. Drag and drop the CustomCalendar control and a button onto the
webform.
Double click the button control to generate the click event handler.
Run the web application and select a date from the calendar control. Notice that the textbox is
populated with the selected date. Now click the button. The selected date should be printed on
the web form.
In this video we will discuss about adding custom event to the asp.net composite custom
calendar control that we have been working with from Parts 112 to 115. Please watch Parts 112
to 115 from the asp.net video tutorial before proceeding with this video.
We have discussed about adding custom events to user controls in Parts 106 to 108. You can
watch these videos from the asp.net video tutorial. Adding custom events to a composite control
is similar to adding events to user controls.
There are 5 simple steps to add "DateSelected" custom event to composite custom calendar
control
Step 1: Create "DateSelectedEventArgs" that will contain event data
public class DateSelectedEventArgs : EventArgs
{
private DateTime _selectedDate;
Step 5: Finally raise the event, when the date selection in the calendar changes.
DateSelectedEventArgs dateSelectedEventData
= new DateSelectedEventArgs(calendar.SelectedDate);
OnDateSelection(dateSelectedEventData);
namespace CustomControls
{
[ToolboxData("<{0}:CustomCalendar runat=server></{0}:CustomCalendar>")]
public class CustomCalendar : CompositeControl
{
TextBox textBox;
ImageButton imageButton;
Calendar calendar;
this.Controls.Add(textBox);
this.Controls.Add(imageButton);
this.Controls.Add(calendar);
}
DateSelectedEventArgs dateSelectedEventData
= new DateSelectedEventArgs(calendar.SelectedDate);
OnDateSelection(dateSelectedEventData);
calendar.Visible = false;
}
[Category("Appearance")]
[Description("Sets the image icon for the calendar control")]
public string ImageButtonImageUrl
{
get
{
EnsureChildControls();
return imageButton.ImageUrl != null ? imageButton.ImageUrl : string.Empty;
}
set
{
EnsureChildControls();
imageButton.ImageUrl = value;
}
}
[Category("Appearance")]
[Description("Gets or sets the selected date of custom calendar control")]
public DateTime SelectedDate
{
get
{
EnsureChildControls();
return string.IsNullOrEmpty(textBox.Text) ? DateTime.MinValue
: Convert.ToDateTime(textBox.Text);
}
set
{
if (value != null)
{
EnsureChildControls();
textBox.Text = value.ToShortDateString();
}
else
{
EnsureChildControls();
textBox.Text = "";
}
}
}
writer.RenderBeginTag(HtmlTextWriterTag.Table);
writer.RenderBeginTag(HtmlTextWriterTag.Tr);
writer.RenderBeginTag(HtmlTextWriterTag.Td);
textBox.RenderControl(writer);
writer.RenderEndTag();
writer.RenderBeginTag(HtmlTextWriterTag.Td);
imageButton.RenderControl(writer);
writer.RenderEndTag();
writer.RenderEndTag();
writer.RenderEndTag();
calendar.RenderControl(writer);
}
Build the CustomControls project. In an asp.net web application add a reference to the
CustomCalendar control. Drag and drop the CustomCalendar control on a webform. Right click
on the control and select properties. In the properties window, click on the events icon. Notice
that, the event "DateSelected" is displayed.
Double click on the "DateSelected" event. This should automatically generate an event
handler method. Implement the event handler method as shown below.
protected void CustomCalendar1_DateSelected(object sender,
CustomControls.DateSelectedEventArgs e)
{
Response.Write(e.SelectedDate.ToShortDateString());
}
Next Video: Assigning an image, to the Custom Calendar in visual studio tool box
In this video we will discuss about assigning an image to the Composite Custom
calendar control in visual studio tool box. Please watch Parts 112 to 116 from the asp.net
video tutorial before proceeding with this video.
To associate an image to the composite custom calendar control use ToolboxBitmap attribute.
This attribute is present in System.Drawing namespace. There are 2 ways we can use this
attribute.
Rebuild the CustomControls project. Re-add the CustomCalendar control to visual studio tool
box and notice that the asp.net calendar control icon is also used for the CustomCalendar
control.
Using ToolboxBitmap attribute to associate a custom image: Create an image measuring
16X16 pixels in dimensions. I have used Microsoft paint to come up with the custom image that
you can see below.
Save this image to "C:\Images" folder or in a folder of your choice. I have given the name
of "CustomCalendarIcon.bmp" to the image.
Finally use the ToolboxBitmap attribute and specify the path to the image.
[System.Drawing.ToolboxBitmap(@"C:\Images\CustomCalendarIcon.bmp")]
[ToolboxData("<{0}:CustomCalendar runat=server></{0}:CustomCalendar>")]
public class CustomCalendar : CompositeControl
{
TextBox textBox;
ImageButton imageButton;
Calendar calendar;
Rebuild the CustomControls project. Re-add the CustomCalendar control to visual studio tool
box and notice that the custom image "CustomCalendarIcon.bmp" is used for the
CustomCalendar control.
In our next video, we will discuss about the differences between user controls and custom
controls.
Custom controls are compiled into their own assembly(.dll) where as user controls are
not. User controls are compiled into the web application project's assembly that contain
them.
Custom controls can be added to toolbox where as user controls cannot be added.
User controls are easier to create as they are similar to creating web pages. Custom
controls are relatively complex, as there is no designer, and every thing has to be done
in code.
A separate copy of user control is required in each application you want to use, where as a
single copy of custom control can be used in multiple projects.
Please Note: When you drag and drop, a custom control, from the tool box onto a web form
designer, the following 2 things can happen
1. If the custom controls assembly is not installed in GAC, then custom controls assembly is
copied into the "BIN" folder of the application. So, in this case, if you need to use the custom
control in multiple applications, then a copy of the custom control's assembly, will be made into
the "BIN" folder of every application.
2. If the custom controls assembly is installed in GAC, then custom controls assembly is directly
referenced from the GAC. So, in this case, a single copy of custom control can be used in
multiple projects.
To learn about installing assemblies into GAC, please watch the following parts from Dot
Net Basics Video Tutorial
Part 3 - Strong Naming an Assembly
Part 4 - What is GAC? How and when to install an assembly into GAC
Click here for SQL Server video tutorial that can help you, if you are new to sql server and need
help creating tables, stored procedures and understanding sql server concepts.
Now, let us invoke the stored procedure in an asp.net web application, and display the
"Products" data in a gridview control. Drag and drop a "gridview" control onto the web form.
Copy and paste the following code in the code-behind file Page_Load() event.
string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
SqlConnection con = new SqlConnection(CS);
SqlDataAdapter da = new SqlDataAdapter("spGetProducts", con);
da.SelectCommand.CommandType = CommandType.StoredProcedure;
DataSet DS = new DataSet();
da.Fill(DS);
GridView1.DataSource = DS;
GridView1.DataBind();
We discussed about retrieving data from database in ado.net tutorial. Click here to access
ado.net tutorial.
At this point, if you run the application, the page takes about 5 seconds to load. This is
because, when you request the webform, the web server has to process the web form events,
execute the stored procedure, create objects, generate HTML and send that HTML to the client
broswer.
Now let us cache the webform. To cache a webform, use the @OutputCache page directive.
The @OutputCache directive has 2 mandatory attributes
Duration - Specifies the time in seconds for which the webform should be cached
VaryByParam - Cache multiple responses of a single webform. For now set the value to
"None". We will discuss about "VaryByParam" in a later video.
When any user requests this Web form for the first time, the web server will process the web
form events, execute the stored procedure, create objects, generate HTML and send that HTML
to the client browser, and retains a copy of the response in memory for the next 30 seconds.
Any subsequent requests during that time receive the cached response.
After the cache duration has expired, the next request for the Web form, has to process the web
form events, execute the stored procedure, create objects, generate HTML, which is then
cached for another 30 seconds. So this web form is processed by the server, once every 30
second, at the most.
Next Video: We will discuss about caching multiple responses for a single webform.
In this video we will discuss about, caching multiple responses for a single
webform. Please watch Part -119 from ASP.NET video tutorial, before proceeding with this
video.
First create a stored procedure to get products from "tblProducts" table by product
name. This procedure returns "ALL Products", if "All" is supplied as the product name, else,
only the product whose name matches with the parameter "@ProductName" is returned.
Create Procedure spGetProductByName
@ProductName nvarchar(50)
as
Begin
if(@ProductName = 'All')
Begin
Select Id, Name, Description
from tblProducts
End
Else
Begin
Select Id, Name, Description
from tblProducts
where Name = @ProductName
End
End
WebForm1.aspx HTML:
<div style="font-family:Arial">
Select Product:
<asp:DropDownList ID="DropDownList1" AutoPostBack="true" runat="server"
onselectedindexchanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem Text="All" Value="All"></asp:ListItem>
<asp:ListItem Text="Laptops" Value="Laptops"></asp:ListItem>
<asp:ListItem Text="iPhone" Value="iPhone"></asp:ListItem>
<asp:ListItem Text="LCD TV" Value="LCD TV"></asp:ListItem>
<asp:ListItem Text="Desktop" Value="Desktop"></asp:ListItem>
</asp:DropDownList>
<br />
<br />
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
<br />
<br />
Server Time:
<asp:Label ID="Label1" runat="server"></asp:Label>
<br />
<br />
Client Time:
<script type="text/javascript">
document.write(Date());
</script>
</div>
WebForm1.aspx.cs Code:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GetProductByName("All");
}
Label1.Text = DateTime.Now.ToString();
}
The basics of caching are discussed in Part 119, and caching multiple responses for a single
webform are discussed in Part 120. In both of these videos, we discussed about controlling
caching within the Webform HTML using the OutputCache directive. In this video, we will
discuss about controlling caching in code.
The basics of caching are discussed in Part 119, and caching multiple responses for a single
webform are discussed in Part 120. In both of these videos, we discussed about controlling
caching within the Webform HTML using the OutputCache directive. In this video, we will
discuss about controlling caching in code.
Web form caching based on GET and POST requests - Part 123
Suggested Videos
Part 120 - Caching multiple responses for a single webform
Part 121 - Controlling caching in code
Part 122 - Fragment caching
In this video, we will discuss about, how webforms are cached based on GET and POST
requests. One of my youtube subscribers asked this question, related to Part 120 - Caching
mulitple responses for a single web form. Please watch Part 120, from the asp.net video
tutorial before proceeding with this video.
We will be using the same example from Part 120 of the asp.net video tutorial, to understand
web form caching based on GET and POST requests.
Question asked by the youtube subscriber
When we have set VaryByParam="None", and accessed WebForm1.aspx, the form displayed
"All" products as expected. In "Select Product" dropdownlist "All" is selected. Now when I
change the selection in the dropdownlist to "iPhone", WebForm1.aspx gets posted back to the
server, the form is re-processed. At this point only "iPhone" product is displayed in the gridivew
control, and in the dropdownlist "iPhone" is selected.
First Question: Why was the GET request for the webform not cached for "All" products
selection in the dropdownlist?
Answer: Actually, the GET request for "All" products selection in the dropdownlist is cached. To
retrieve the cached response of the GET request, click any where in the URL address bar in the
browser, and press ENTER key. This sends another GET request to the server, which should to
return the cached response of the GET request.
Second Question: Why am I not getting the cached response of the GET request when I
change the selection in the dropdownlist to "All"?
Answer: This is because, when the selection in the dropdownlist is changed to "All", a
POSTBACK request is sent to the server. Since, for the POSTBACK request there is already a
cached version of the webform, we get that version.
I think, when we set VaryByParam="None", a separate response is cached for GET and POST
requests.
I welcome any suggestions to improve this answer. Please leave your valuable comments.
In Part 122 of the asp.net video tutorial, we discussed about fragment caching. Please
watch Part 122 before proceeding with this video. In this video we will discuss about, caching
multiple versions of user control using "VaryByControl" attribute of
the "OutputCache" directive.
We will be using "tblProducts" table for this demo. If you need the script to create and
populate this table, please refer to Part 122, by clicking here.
Add a user control to the project, with name = "UCProductsControl.ascx". Copy and paste the
following HTML in the usercontrol.
<table style="border: 1px solid black">
<tr>
<td style="background-color: Gray; font-size: 12pt">
Products User Control
</td>
</tr>
<tr>
<td>
Select Product:
<asp:DropDownList ID="DropDownList1" AutoPostBack="true" runat="server"
OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem Text="All" Value="All"></asp:ListItem>
<asp:ListItem Text="Laptops" Value="Laptops"></asp:ListItem>
<asp:ListItem Text="iPhone" Value="iPhone"></asp:ListItem>
<asp:ListItem Text="LCD TV" Value="LCD TV"></asp:ListItem>
<asp:ListItem Text="Desktop" Value="Desktop"></asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td>
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</td>
</tr>
<tr>
<td>
<b>User Control Server Time:
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</b>
</td>
</tr>
<tr>
<td>
<b>User Control Client Time:
<script type="text/javascript">
document.write(Date());
</script>
</b>
</td>
</tr>
</table>
Please run the application and test. Notice that, as the product selections change in the
dropdownlist, for each different selection a response from the user control is cached for 60
seconds. The difference between "User Control Server Time" and "User Control Client Time"
proves this. Since, we don't have "Caching" set on the WebForm1.aspx, "Page Server Time"
and "Page Client Time" stays the same always.
In our next video, we will discuss about when and how to use "VaryByParams" to cache multiple
versions of a user control.
In Part 124 of the asp.net video tutorial, we discussed about caching multiple versions of user
control using VaryByControl attribute of the "OutputCache" directive. Please watch Part
124 before proceeding with this video.
In this video we will discuss about, caching multiple versions of user control
using "VaryByParams" attribute of the "OutputCache" directive.
We will be using the same user control "UCProductsControl.ascx" from Part 124, for this
demo. If you need the code associated with the user control, please refer to Part 124, by
clicking here.
First let us modify "UCProductsControl.ascx" user control, to load products into the gridview
control, using a query string parameter. This means we can remove the "DropDownList1"
control from "UCProductsControl.ascx" file. The HTML is shown below.
<table style="border: 1px solid black">
<tr>
<td style="background-color: Gray; font-size: 12pt">
Products User Control
</td>
</tr>
<tr>
<td>
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</td>
</tr>
<tr>
<td>
<b>User Control Server Time:
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</b>
</td>
</tr>
<tr>
<td>
<b>User Control Client Time:
<script type="text/javascript">
document.write(Date());
</script>
</b>
</td>
</tr>
</table>
Make the following changes to the code-behind file of the user control
1. In Page_Load() event, remove "!IsPostBack" condition
2. Since, we want to load products using query string parameter, remove the reference to
"DropDownList1" and use Request.QueryString["ProductName"]
Please run the application and test. Notice that, as the "ProductName" query string value
changes, for each different value, a response from the user control is cached for 60 seconds.
The difference between "User Control Server Time" and "User Control Client Time" proves this.
Since, we don't have "Caching" set on the WebForm1.aspx, "Page Server Time" and "Page
Client Time" stays the same always.
Caching application data in asp.net - Part 126
Suggested Videos
Part 123 - Web form caching based on GET and POST requests
Part 124 - Caching multiple versions of user control using VaryByControl
Part 125 - Caching multiple versions of user control using VaryByParam
Please watch these videos, from the asp.net video tutorial by clicking here, before proceeding.
In this video we will discuss about caching application data. It is possible to store application
data in the web server memory, using the CACHE object, so that the data can be retrieved
faster. For example, let us say, we have a stored procedure that takes 5 seconds to execute
and return data. We can cache the data returned by this stored procedure with in an asp.net
web application using the CACHE object, so that, next time when we try to access the data, we
can get it from the cache, rather than reprocessing the stored procedure again.
We will be using "tblProducts" table for this demo. If you need the script to create and
populate this table, please refer to Part 122
The following stored procedure takes 5 seconds to execute and return data. We are using
WAITFOR DELAY, to introduce artificial query processing time of 5 seconds.
CREATE Procedure spGetProducts
as
Begin
Waitfor Delay '00:00:05'
Select * from tblProducts
End
Create an asp.net web application, copy and paste the following HTML in
WebForm1.aspx.
<div style="font-family:Arial">
<asp:Button ID="btnGetProducts" runat="server" Text="Get Products Data"
onclick="btnGetProducts_Click" />
<br /><br />
<asp:GridView ID="gvProducts" runat="server">
</asp:GridView>
<br />
<asp:Label ID="lblMessage" Font-Bold="true" runat="server"></asp:Label>
</div>
Copy and paste the following code in WebForm1.aspx.cs. The code is well documented and
is self explanatory.
protected void btnGetProducts_Click(object sender, EventArgs e)
{
DateTime dtStartDateTime = DateTime.Now;
System.Text.StringBuilder sbMessage = new System.Text.StringBuilder();
// Check if the data is already cached
if (Cache["ProductsData"] != null)
{
// If data is cached, retrieve data from Cache using the key "ProductsData"
DataSet ds = (DataSet)Cache["ProductsData"];
// Set the dataset as the datasource
gvProducts.DataSource = ds;
gvProducts.DataBind();
// Retrieve the total rows count
sbMessage.Append(ds.Tables[0].Rows.Count.ToString() + " rows retrieved from cache.");
}
// If the data is not cached
else
{
// Get the data from the database
DataSet ds = GetProductsData();
// Cache the dataset using the key "ProductsData"
Cache["ProductsData"] = ds;
// Set the dataset as the datasource
gvProducts.DataSource = ds;
gvProducts.DataBind();
sbMessage.Append(ds.Tables[0].Rows.Count.ToString() + " rows retrieved from
database.");
}
DateTime dtEndDateTime = DateTime.Now;
sbMessage.Append((dtEndDateTime - dtStartDateTime).Seconds.ToString() + " Seconds
Load Time");
lblMessage.Text = sbMessage.ToString();
}
In this video, we discussed about storing application data in cache, using direct assignment.
That is using a key and assiging value to it, as shown below.
Cache["ProductsData"] = ds
In our next video, we will discuss about all the other options that are available, to store data in
the Cache object.
In Part 126, of the asp.net video tutorial we discussed about, "Caching application data" using
direct assignment as shown below. Please watch Part 126, from the asp.net video tutorial by
clicking here, before proceeding.
Cache["Cache_Key"] = Data_To_Cache
Apart from caching data, using assignment, there are 2 other ways to cache application data
1. Using "Cache" object's Insert() method
2. Using "Cache" object's Add() method
The same thing can be achieved, using cache object's "Insert()" method, as shown
below.
Cache.Insert("ProductsData", ds);
We will discuss about other overloaded versions of "Insert()" method in a later video session.
Be cautious when dealing with cache keys: Assigning a value to a cache key, that is already
being used will silently overwrite the existing value, without any warnings. For example
The value stored in "MyKey" is "Value1"
Cache["MyKey"] = "Value 1";
The following line will silently overwrite "MyKey" value to "Value 2"
Cache["MyKey"] = "Value 2";
To Remove an item from cache explicitly, use Remove() method. The following line would
remove the cache item with key "MyKey"
Cache.Remove("MyKey");
Please Note: An item may be removed automatically, from cache, when any of the following
conditions are true
The cached item has expired.
The cache is full.
There is a cache dependency, and the item, that the cache object is dependent on has
changed.
In a way, Cache object is similar to Application state. The objects that we store in application
state variables are available anywhere within a Web application. Objects stored in cache are
also available anywhere within a Web application. But the difference is that, items stored in
cache can expire, where as items in application state will never expire.
Caching in asp.net - AbsoluteExpiration, SlidingExpiration, and
CacheItemPriority - Part 128
Suggested Videos
Part 125 - Caching multiple versions of user control using VaryByParam
Part 126 - Caching application data
Part 127 - Different ways to cache application data
When you cache an item using the Insert() or Add() method, you can also speicfy, how long
you want the item to be cached. There are 2 ways to do this.
AbsoluteExpiration: A DateTime object that specifies when the data should be removed from
the cache. When you specify "AbsoluteExpiration", the cache item will expire at that time,
irrespective of whether the cached item is accessed or not.
For example, the following line will cache dataset, ds, for 10 seconds, irrespective of whether
the dataset, is accessed or not within those 10 seconds, after it is cached. Since we are using
absolute expiration, we
specified Cache.NoSlidingExpiration for "SlidingExpiration" parameter of the Insert()
method.
Cache.Insert("ProductsData", ds, null, DateTime.Now.AddSeconds(10),
System.Web.Caching.Cache.NoSlidingExpiration);
SlidingExpiration: A TimeSpan object that identifies how long the data should remain in the
cache after the data was last accessed.
For example, the following line will cache dataset, ds, for 10 seconds. If the dataset is
accessed from the cache with in the specified 10 seconds, then, from that point, the dataset will
remain in cache for the next 10 seconds. Since we are using sliding expiration, we
specified Cache.NoAbsoluteExpiration for "AbsoluteExpiration" parameter of the Insert()
method.
Cache.Insert("ProductsData", ds, null, System.Web.Caching.Cache.NoAbsoluteExpiration,
TimeSpan.FromSeconds(10));
What happens if you specify both, AbsoluteExpiration and SlidingExpiration when caching
application data?
You get a run time exception stating 'absoluteExpiration must be DateTime.MaxValue or
slidingExpiration must be timeSpan.Zero'
CacheItemPriority: Sliding expiration and absolute expiration can be used to control, how long
the item is cached, but please note, if the web server is running low on memory, and if it
requires memory, it may remove cached items that may not have expired. However, the order in
which the items are removed is determined by the cached item's priority. Cache item's
priority can be specified using CacheItemPriority enum.
For example, let us say we have 3 items with the following priorities and they are cached.
Item 1 with CacheItemPriority.NotRemovable
Item 2 with CacheItemPriority.High
Item 3 with CacheItemPriority.AboveNormal
Now, if the server is running low on memory, it may remove the items from cache, even if they
have not expired. Since, Item 3 has the least priority, it will be removed first, followed by Item 2
and then finally Item 1.
In this video, we will discuss about cache dependency on files in asp.net. Let us
understand this with an example. Create an asp.net web application. Add a folder with name
= "Data" to your web application. Right click on the "Data" folder and add an xml file with name
= "Countries.xml".
Copy and paste the following xml data into Countries.xml file.
<?xml version="1.0" encoding="utf-8" ?>
<Countries>
<Country>
<Id>101</Id>
<Name>India</Name>
<Continent>Asia</Continent>
</Country>
<Country>
<Id>102</Id>
<Name>China</Name>
<Continent>Asia</Continent>
</Country>
<Country>
<Id>103</Id>
<Name>Frnace</Name>
<Continent>Europe</Continent>
</Country>
<Country>
<Id>104</Id>
<Name>United Kingdom</Name>
<Continent>Europe</Continent>
</Country>
<Country>
<Id>105</Id>
<Name>United State of America</Name>
<Continent>North America</Continent>
</Country>
</Countries>
WebForm1.aspx HTML:
<div style="font-family: Arial">
<asp:Button ID="btnGetCountries" runat="server" Text="Get Countries"
OnClick="btnGetCountries_Click" />
<br />
<br />
<asp:GridView ID="gvCountries" runat="server">
</asp:GridView>
<br />
<asp:Label ID="lblMessage" Font-Bold="true" runat="server"></asp:Label>
</div>
The following code reads xml data from "Countries.xml" file into dataset which is then cached.
Notice, that we are using "cache" object's "Insert()" method to cache the DataSet. Since we
are passing "null" as the argument for "CacheDependency" parameter of
the "Insert()" method, when the data in "Countries.xml" file changes, the data in the cache is
unaffected.
protected void btnGetCountries_Click(object sender, EventArgs e)
{
// Check if the data is already cached
if (Cache["CountriesData"] != null)
{
// If data is cached, retrieve data from Cache
DataSet ds = (DataSet)Cache["CountriesData"];
// Set the dataset as the datasource
gvCountries.DataSource = ds;
gvCountries.DataBind();
// Retrieve the total rows count
lblMessage.Text = ds.Tables[0].Rows.Count.ToString() + " rows retrieved from cache.";
}
// If the data is not cached
else
{
// Get data from xml file
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("~/Data/Countries.xml"));
When the data in Countries.xml file changes, we want the data in the cache to be removed
automatically. For this to happen we need to establish a dependency on the xml file as shown
below.
Cache.Insert("CountriesData",
ds, new CacheDependency(Server.MapPath("~/Data/Countries.xml")), DateTime.Now.AddSeco
nds(20), System.Web.Caching.Cache.NoSlidingExpiration);
Now run the application. After the dataset is cached, change the xml file and click "Get
Countries" button. Notice that the data is now retrieved from file directly, as the dataset is
removed from cache.
Create an asp.net web application. Copy and paste the following HTML on
WebForm1.aspx.
<div style="font-family: Arial">
<asp:Button ID="btnLoadCountriesAndCache" runat="server" Text="Load Countries &
Cache"
OnClick="btnLoadCountriesAndCache_Click" />
<asp:Button ID="btnGetCountriesFromCache" runat="server" Text="Get Countries from
Cache"
OnClick="btnGetCountriesFromCache_Click" />
<br />
<br />
<asp:GridView ID="gvCountries" runat="server">
</asp:GridView>
<br />
<asp:Button ID="btnRemoveCachedItem" runat="server" Text="Remove Cached Item"
OnClick="btnRemoveCachedItem_Click" />
<asp:Button ID="btnGetCacheStatus" runat="server" Text="Get Cache Status"
OnClick="btnGetCacheStatus_Click" />
<br />
<br />
<asp:Label ID="lblMessage" Font-Bold="true" runat="server"></asp:Label>
</div>
// Cache countries dataset. Please note that we are passing the delegate instance as an
// argument for CacheItemRemovedCallback delegate parameter of the insert() method.
Cache.Insert("CountriesData",
ds, new CacheDependency(Server.MapPath("~/Data/Countries.xml")), DateTime.Now.AddSeco
nds(60),
System.Web.Caching.Cache.NoSlidingExpiration, CacheItemPriority.Default,
onCacheItemRemoved);
// This method gets invoked automatically, whenever the cached item is removed from cache
public
void CacheItemRemovedCallbackMethod(string key, object value, CacheItemRemovedReason
reason)
{
// Retrieve the key and reason for removal
string dataToStore = "Cache item with key = \"" + key + "\" is no longer present. Reason = " +
reason.ToString();
// Cache the message
Cache["CacheStatus"] = dataToStore;
Alternatively we can also store information about the removed cache object, in a database table.
The ado.net code that does this is commented.
Finally we can also reload data ino cache, from the XML file. The code to reload data into
cache, is also commented.
In this video, we will discuss about removing data from cache, when the data in the table
from which it has come, has changed. Let us understand cache dependency on sql server
database table, with an example.
First create "tblProducts" table and populate with sample data using the script below
CREATE TABLE [tblProducts]
(
[Id] [int] PRIMARY KEY IDENTITY,
[Name] [nvarchar](50) NULL,
[Description] [nvarchar](250) NULL
)
Create an asp.net web application project. Copy and paste the following HTML on
WebForm1.aspx
<div style="font-family: Arial">
<asp:Button ID="btnGetData" runat="server" Text="Get Data"
OnClick="btnGetData_Click" />
<br />
<br />
<asp:GridView ID="gvProducts" runat="server">
</asp:GridView>
<br />
<asp:Label ID="lblStatus" runat="server" Font-Bold="true">
</asp:Label>
</div>
gvProducts.DataSource = ds;
gvProducts.DataBind();
lblStatus.Text = "Data retrieved from database @ " + DateTime.Now.ToString();
}
}
In the above code, notice that, to enable SqlCacheDependency on the database and table, we
are using "EnableNotifications()" and "EnableTableForNotifications()" methods, as shown
below.
System.Web.Caching.SqlCacheDependencyAdmin.EnableNotifications(CS)
System.Web.Caching.SqlCacheDependencyAdmin.EnableTableForNotifications(CS, "tblProduc
ts");
If you need to understand the purpose of -et, -E, -d, -t, use the help, by typing the following
command
aspnet_regsql /?
Notice that, we have set pollTime="2000". pollTime attribute specifies the frequency, at which,
asp.net is going to check the database for changes. This time is in milli-seconds. Since, we
have specified 2000 milli-seconds, asp.net is going to check the database for changes every 2
seconds. The default is 500 milli-seconds.
Run the application and when we click "Get Products" button for the first time, data is loaded
from database. Click again, the data should be loaded from cache. Now, execute the following
UPDATE query.
Update tblProducts set Name='Laptops' where Id = 1
Now, click the button again, and notice that the data is loaded from the database, as existing
data is removed from cache.
Reload data into cache automatically when data in the table changes -
Part 132
Suggested Videos
Part 129 - Cache dependency on files
Part 130 - Refreshing cache automatically, when cached data is removed
Part 131 - Cache dependency on sql server database table
In Part 130 of asp.net video tutorial, we discussed about reloading data into cache
automatically, when the xml file from which the data was initially retrieved, has changed.
In this video, we will discuss about reloading data into cache automatically when data in the
underlying database table has changed. Please watch Part 131, before proceeding with this
video.
To load data automatically into cache, when cached data is removed, we need to define
a callback method. This callback method will be invoked when the respective item is removed
from cache. So, the code to reload and re-cache data can be written in this method. The
callback method signature should match, with the signature
of "CacheItemRemovedCallback" delegate. The call back method is defined below.
public
void CacheItemRemovedCallbackMethod(string key, object value, CacheItemRemovedReason
reason)
{
string CS
= ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString;
CacheItemRemovedCallback onCacheItemRemoved
= new CacheItemRemovedCallback(CacheItemRemovedCallbackMethod);
In Part 131, to cache data, we used cache object's, Insert() method that takes 3 parameters, as
shown below.
Cache.Insert("ProductsData", ds, sqlDependency);
Instead, let's use the overloaded version that takes 7 parameters and
pass "onCacheItemRemoved" as an argument
for "CacheItemRemovedCallback" parameter.
Cache.Insert("ProductsData", ds,
sqlDependency, DateTime.Now.AddHours(24), Cache.NoSlidingExpiration,
CacheItemPriority.Default, onCacheItemRemoved);
That's it. We are done. Now, please run the application. When "Get Data" button is clicked for
the first time, the data is loaded from database. Once the data is loaded into cache, we always
get it from cache when we click "Get Data". Now, execute an update statement on the
database table. Notice that, when we click "Get Data" button now, we still get data from cache,
but notice that, the updated data is loaded.
Let us understand the use of AutoEventWireup property with an example. Create an asp.net
web application project.
A webform in asp.net raises several events in it's life cycle. The following are few of those
events.
1. Page Load
2. Page Load Complete
3. Page PreRender
4. Page PreRenderComplete
Run the application, and notice that the above event handler methods are executed as
expected.
Run the application, and notice that every event handler method is executed twice. This is
because,
1. Setting AutoEventWireup=true, registered the event handler method's once, and
2. Overriding OnInit() method, has registered the same event handler method again
Important points to remember:
1. When AutoEventWireup is set to true and if you want the event handlers to be wired up with
their events automatically, the event handler names should follow the standarad naming
convention - Page_EventName.
2. AutoEventWireup can be set in the page directive or in web.config file.
3. To set autoEventWireup in web.config, use pages element as shown below.
<configuration>
<system.web>
<pages autoEventWireup="true" />
</system.web>
</configuration>
4. If autoEventWireup, is set at both webform and web.config level, webform setting will take
precedence over web.config setting.
So, AutoEventWireup is a boolean property which, when set to true, the page event handler
methods are automatically wired with their respective events. If this property is set to false, then
the event handler methods need to be explicitly associated with their respective events.
Add image slideshow to your website using asp.net ajax and c# - Part
134
Suggested Videos
Part 131 - Cache dependency on sql server database table
Part 132 - Reload data into cache automatically when data in the table changes
Part 133 - What is AutoEventWireup in asp.net
In this video, we will discuss adding image slideshow to a website or web application.
Step 2: In the solution explorer, right click on the project name, and add "Images" folder.
Step 3: For this demo, we will use the sample pictures that are shipped with Microsoft operating
system. Copy the images that are present at the following path, and paste them into the images
folder.
C:\Users\Public\Pictures\Sample Pictures
Rename the images to use numbers. Since on my machine there are 8 images, I have named
them 1.jpg, 2.jpg to 8.jpg. At this point, your solution explorer should look as show below.
Step 4: Drag and drop "ScriptManager" control onto the webform. This control can be found
under "AJAX Extensions" in toolbox. ScriptManager control is required on any asp.net page,
where you want to take adavantage of asp.net ajax framework. We will discuss more about
ScriptManager control in our upcoming asp.net ajax tutorial.
Step 5: Drag and drop "UpdatePanel" control. This control, allow us to perform partial page
postbacks as opposed to a full page postback. The responsiveness of a page can be
significantly increased using partial page postback, as only the data that is relevant to that
UpdatePanel is sent to the server, and only the corresponding data is returned. Another benefit
of partial page postbacks is that, they avoid screen flickers that are very common with full page
postbacks.
All the content of the updatepanel, must be placed inside ContentTemplate element, so
include <ContentTemplate> tag directly inside updatepanel. Drag and drop, the timer and image
controls onto the webform, so that they are placed inside the <ContentTemplate> tag.
1. The Timer control raises a tick event. This event is raised when the specified timer interval
has elapsed and the timer is enabled.
2. Timer interval is specified in milli-seconds. For example, If you want the tick event to be
raised every one second, then set "Interval" property of timer control to "1000" milliseconds.
3. We will use the tick event of the timer control to change the image dynamically every one
second. So, flip the webform to design mode, if it's not already in design mode. Double click on
the timer control. This should generate an event handler for tick event.
4. Set the Image control height and width to 100px.
5. Finally copy and paste the following code in the code-behind file.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SetImageUrl();
}
}
At the moment, the problem with this code is that, it displays a random image every one second.
Let's say our requirement is such that, we want to display images in order from 1.jpg, 2.jpg to
8.jpg. Also, below the image, display the number of the image that is being displayed. We will
discuss fixing this in our next video.
To achieve this,
Step 1: Include "Label1" control in the aspx page. This label control displays the image number
that is being displayed.
<br /><asp:Label ID="Label1" Font-Bold="true" runat="server"></asp:Label>
At the moment, there is no mechanism in place to start or stop the slideshow. We will discuss
this in our next video.
Provide capability to start and stop image slideshow - Part 136
Suggested Videos
Part 133 - What is AutoEventWireup in asp.net
Part 134 - Add image slideshow to your website using asp.net ajax and c#
Part 135 - Display images in sequence in an image slideshow
Please watch Parts 134 & 135 before proceeding. We will be continuing with the example, that
we started in Part 135. At the moment, there is no mechanism in place to start or stop the
slideshow.
To achieve this
1. Drag and drop a button control on the webform.
2. Set Text= "Stop Slideshow".
3. Generate click event handler from Button1. Copy and paste the following code.
protected void Button1_Click(object sender, EventArgs e)
{
// If timer is enabled, disable timer and change
// the text on the button control accordingly
if (Timer1.Enabled)
{
Timer1.Enabled = false;
Button1.Text = "Start Slideshow";
}
// If timer is disabled, enable timer and change
// the text on the button control accordingly
else
{
Timer1.Enabled = true;
Button1.Text = "Stop Slideshow";
}
}
At the moment, there are 2 problems with this code. If we want to add a new image to the
slide show,
1. We will have to modify the application code
2. The new image has to be named in a specific way. Since we already have 8 images, the next
image has to be named 9.jpg.
There are 2 problems with the image slideshow, that we have built in Parts 134, 135,
& 136. If we want to add a new image to the slide show,
1. We will have to modify the application code
2. The new image has to be named in a specific way. Since we already have 8 images, the next
image has to be named 9.jpg.
In this video, we will discuss using an XML file and in our next video, we will discuss using a
database table.
Step 1: At the moment the images in "Images" folder have the following names
1.jpg
2.jpg
3.jpg
etc...
Delete all these 8 images. Now copy the images with their original names from
"C:\Users\Public\Pictures\Sample Pictures".
Step 2: Right click on the project name in solution explorer, and add "Data" folder.
Add "ImageData.xml" file. Copy and paste the following XML
<?xml version="1.0" encoding="utf-8" ?>
<Images>
<image name="Chrysanthemum.jpg" order="1"></image>
<image name="Desert.jpg" order="2"></image>
<image name="Hydrangeas.jpg" order="3"></image>
<image name="Jellyfish.jpg" order="4"></image>
<image name="Koala.jpg" order="5"></image>
<image name="Lighthouse.jpg" order="6"></image>
<image name="Penguins.jpg" order="7"></image>
<image name="Tulips.jpg" order="8"></image>
</Images>
Deafult.aspx code:
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master"
AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="ImageSlideShow._Default" %>
Default.aspx.cs code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
namespace ImageSlideShow
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadImageData();
}
}
ViewState["ImageDisplayed"] = 1;
DataRow imageDataRow = ds.Tables["image"].Select().FirstOrDefault(x =>
x["order"].ToString() == "1");
Image1.ImageUrl = "~/Images/" + imageDataRow["name"].ToString();
lblImageName.Text = imageDataRow["name"].ToString();
lblImageOrder.Text = imageDataRow["order"].ToString();
}
DataRow imageDataRow =
((DataSet)ViewState["ImageData"]).Tables["image"].Select().FirstOrDefault(x =>
x["order"].ToString() == i.ToString());
if (imageDataRow != null)
{
Image1.ImageUrl = "~/Images/" + imageDataRow["name"].ToString();
lblImageName.Text = imageDataRow["name"].ToString();
lblImageOrder.Text = imageDataRow["order"].ToString();
}
else
{
LoadImageData();
}
}
In Part 137, we discussed storing the image data in an xml file. In this video, we will be using a
database table to store image data. So, we can safely delete ImageData.xml file. Please
watch Part 137, before proceeding.
We now have to write ADO.NET code to retrieve image data from the database table. The
rest of the logic remains unchanged. Here's the complete code for your reference.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace ImageSlideShow
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SetImageUrl();
}
}
DataRow imageDataRow =
((DataSet)ViewState["ImageData"]).Tables["image"].Select().FirstOrDefault(x =>
x["order"].ToString() == i.ToString());
if (imageDataRow != null)
{
Image1.ImageUrl = "~/Images/" + imageDataRow["name"].ToString();
lblImageName.Text = imageDataRow["name"].ToString();
lblImageOrder.Text = imageDataRow["order"].ToString();
}
else
{
SetImageUrl();
}
}
How to upload and download files using asp.net and c# - Part 139
Suggested Videos
Part 136 - Provide capability to start and stop image slideshow
Part 137 - Add images to slideshow using xml file
Part 138 - Add images to slideshow using database table
The user interface to upload and download files, should be as shown below.
When the files are uploaded, they should be uploaded to a folder on the web server. In
our case, we will be uploading to "Data" folder.
WebForm1.aspx code:
<div style="font-family:Arial">
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="Upload"
OnClick="Button1_Click" />
<br />
<br />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
OnRowCommand="GridView1_RowCommand" BackColor="White"
BorderColor="#CC9966" BorderStyle="None"
BorderWidth="1px" CellPadding="4">
<Columns>
<asp:TemplateField HeaderText="File" ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server"
CausesValidation="False"
CommandArgument='<%# Eval("File") %>'
CommandName="Download" Text='<%# Eval("File") %>'>
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Size" HeaderText="Size in Bytes" />
<asp:BoundField DataField="Type" HeaderText="File Type" />
</Columns>
<FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
<HeaderStyle BackColor="#990000" Font-Bold="True"
ForeColor="#FFFFCC" />
<PagerStyle BackColor="#FFFFCC" ForeColor="#330099"
HorizontalAlign="Center" />
<RowStyle BackColor="White" ForeColor="#330099" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True"
ForeColor="#663399" />
<SortedAscendingCellStyle BackColor="#FEFCEB" />
<SortedAscendingHeaderStyle BackColor="#AF0101" />
<SortedDescendingCellStyle BackColor="#F6F0C0" />
<SortedDescendingHeaderStyle BackColor="#7E0000" />
</asp:GridView>
</div>
WebForm1.aspx.cs code:
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
string fileName = FileUpload1.FileName;
FileUpload1.PostedFile
.SaveAs(Server.MapPath("~/Data/") + fileName);
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
Please make sure to include the following using declarations in the code behind file.
using System.IO;
using System.Data;
In this video, we will discuss creating an image gallery using asp.net and c#. The images in
the galley should be displayed as click-able thumbnails. Upon clicking the thumbnail, the user
should be redirected to a page, where he can see the original image. Upon uploading an image,
the image should be added to the gallery immediately. The output should be as shown below.
The images used in the demo can be found at the following link
http://windows.microsoft.com/en-GB/windows/wallpaper
Add "Data" folder to the project. This folder stores the uploaded images.
WebForm1.aspx
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="Upload" OnClick="Button1_Click" />
<asp:Panel ID="Panel1" runat="server" Width="440px"
BorderStyle="Dashed" BorderColor="#000066">
</asp:Panel>
WebForm1.aspx.cs
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
LoadImages();
}
WebForm2.aspx
<asp:Button ID="Button2" Text="Back to Gallery" runat="server" onclick="Button1_Click" />
<br /><br />
<asp:Image ID="Image1" Width="800px" Height="550px" runat="server" />
<br /><br />
<asp:Button ID="Button1" Text="Back to Gallery" runat="server" onclick="Button1_Click" />
WebForm2.aspx.cs
public partial class WebForm2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Image1.ImageUrl = Request.QueryString["ImageURL"];
}
In this video, we will discuss designing contact us page using asp.net and c#. It's very
common for web sites to contain a contact us page which allows the users of the site to contact
either the administrator or the support group. A typical contact-us page is shown below.
contactus.aspx:
<div style="font-family: Arial">
<fieldset style="width: 350px">
<legend title="Contact us">Contact us</legend>
<table>
<tr>
<td>
<b>Name:</b>
</td>
<td>
<asp:TextBox
ID="txtName"
Width="200px"
runat="server">
</asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator
ForeColor="Red"
ID="RequiredFieldValidator1"
runat="server"
ControlToValidate="txtName"
ErrorMessage="Name is required"
Text="*">
</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
<b>Email:</b>
</td>
<td>
<asp:TextBox
ID="txtEmail"
Width="200px"
runat="server">
</asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator
Display="Dynamic"
ForeColor="Red"
ID="RequiredFieldValidator2"
runat="server"
ControlToValidate="txtEmail"
ErrorMessage="Email is required"
Text="*">
</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator
Display="Dynamic"
ForeColor="Red"
ID="RegularExpressionValidator1"
runat="server"
ErrorMessage="Invalid Email"
ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"
ControlToValidate="txtEmail"
Text="*">
</asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td>
<b>Subject:</b>
</td>
<td>
<asp:TextBox
ID="txtSubject"
Width="200px"
runat="server">
</asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator
ForeColor="Red"
ID="RequiredFieldValidator3"
runat="server"
ControlToValidate="txtSubject"
ErrorMessage="Subject is required"
Text="*">
</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td style="vertical-align: top">
<b>Comments:</b>
</td>
<td style="vertical-align: top">
<asp:TextBox
ID="txtComments"
Width="200px"
TextMode="MultiLine"
Rows="5"
runat="server">
</asp:TextBox>
</td>
<td style="vertical-align: top">
<asp:RequiredFieldValidator
ForeColor="Red"
ID="RequiredFieldValidator4"
runat="server"
ControlToValidate="txtComments"
ErrorMessage="Comments is required"
Text="*">
</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td colspan="3">
<asp:ValidationSummary
HeaderText="Please fix the following errors"
ForeColor="Red"
ID="ValidationSummary1"
runat="server" />
</td>
</tr>
<tr>
<td colspan="3">
<asp:Label
ID="lblMessage"
runat="server"
Font-Bold="true">
</asp:Label>
</td>
</tr>
<tr>
<td colspan="3">
<asp:Button
ID="Button1"
runat="server"
Text="Submit"
OnClick="Button1_Click" />
</td>
</tr>
</table>
</fieldset>
</div>
contactus.aspx.cs
public partial class contactus : System.Web.UI.Page
{
protected void Button1_Click(object sender, EventArgs e)
{
// To Do. Implementation will be discussed in Part 142
}
}
In our next video, we will discuss implementing the server side code.
In Part 141, we discussed designing "contact us" page. In this video, we will implement the
server side code. Once the form is submitted, an email should be sent to the support group.
The email should contain all the details that are entered in the contact us form.
contactus.aspx.cs:
protected void Button1_Click(object sender, EventArgs e)
{
try
{
if (Page.IsValid)
{
MailMessage mailMessage = new MailMessage();
mailMessage.From = new MailAddress("[email protected]");
mailMessage.To.Add("[email protected]");
mailMessage.Subject = txtSubject.Text;
Label1.ForeColor = System.Drawing.Color.Blue;
Label1.Text = "Thank you for contacting us";
txtName.Enabled = false;
txtEmail.Enabled = false;
txtComments.Enabled = false;
txtSubject.Enabled = false;
Button1.Enabled = false;
}
}
catch (Exception ex)
{
// Log the exception information to
// database table or event viewer
Label1.ForeColor = System.Drawing.Color.Red;
Label1.Text = "There is an unkwon problem. Please try later";
}
}
Part 143 - Difference between http get and http post methods
Suggested Videos
Part 140 - Creating an image gallery using asp.net and c#
Part 141 - Contact us page using asp.net and c#
Part 142 - Contact us page using asp.net and c# continued
HTTP stands for Hypertext Transfer Protocol. Http is the most common protocol used for
communication between a web server and a client. For example, to request home page of
Pragim Technologies, you would type the following URL. Notice that the URL has http prefix.
http://pragimtech.com/Home.aspx
The 2 most commonly used HTTP methods are GET and POST.
Build the solution. Open a browser window. In the Address bar type the following address and
press and enter key.
http://localhost/WebFormsDemo/WebForm1.aspx
So here we are issuing a GET request for WebForm1.aspx. In the fiddler tool, you can clearly
notice that a GET request is issued for WebForm1.aspx
At this point, you will have the user interface to enter FirstName, LastName and Email. Enter the
details and when you click "Submit" button, the data will be posted to the server. So, here we
are issuing a POST request to submit the data on WebForm1.aspx. In the fiddler tool, you can
clearly notice that a POST request is issued to submit WebForm1 data.
In the Button1_Click() event handler, we have Response.Redirect() call, which issues a GET
request to WebForm2.aspx
GET Request - is generally used to get data from the web server. A GET request is generally
issued,
1. When you click on a hyperlink
2. When Response.Redirect() statement is executed
3. When you type URL in the address bar and hit enter
POST Request - is generally used to submit data to the server. A POST request is generally
issued,
1. When you click on a submit button
2. When AUTOPOST back is set true and when a selection in the DropDownList is changed
In asp.net webforms, IsPostBack page property is used to check if the request is a GET or a
POST request. If IsPostBack property returns true, then the request is POST, else the request is
GET.
Part 144 - How to check if the request method is a GET or a POST in
MVC
Suggested Videos
Part 141 - Contact us page using asp.net and c#
Part 142 - Contact us page using asp.net and c# continued
Part 143 - Difference between http get and http post methods
In asp.net webforms, IsPostBack page property is used to check if the request is a GET or a
POST request. If IsPostBack property returns true, then the request is POST, else the request is
GET.
In asp.net mvc, use Request object's HttpMethod property to check if the request is a GET or
a POST request. Let's discuss using Request.HttpMethod with an example.
4. Right click on the Index() action method in HomeController and select "Add View" from the
context menu. Set
View name = Index
View engine = Razor
Create a strongly typed view = unchecked
Create a partial view = unchecked
Use a layout or master page = unchecked
Click Add.
@using (@Html.BeginForm())
{
<input type="submit" value="Submit" />
}
Step 4: Right click on the project name in "Solution Explorer" and add a Web Service with
name = StudentService.asmx. Copy and paste the following code. This web service is
responsible for retrieving matching student names to implement auto-complete when the user
types the name of the student in the textbox.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace WebFormsDemo
{
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
public class StudentService : System.Web.Services.WebService
{
[WebMethod]
public List<string> GetStudentNames(string searchTerm)
{
List<string> studentNames = new List<string>();
string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using (SqlConnection con = new SqlConnection(cs))
{
SqlCommand cmd = new SqlCommand("spGetStudentNames", con);
cmd.CommandType = CommandType.StoredProcedure;
return studentNames;
}
}
}
namespace WebFormsDemo
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GetStudents(null);
}
}
if (!string.IsNullOrEmpty(studentName))
{
SqlParameter parameter = new SqlParameter("@EmployeeName", studentName);
cmd.Parameters.Add(parameter);
}
con.Open();
SqlDataReader rdr = cmd.ExecuteReader();
gvStudents.DataSource = rdr;
gvStudents.DataBind();
}
}
It is common for a web site to have a consistent look and behaviour across all the pages
in the web application. For example, we want all the pages in our application to have the
layout as shown below.
To have a layout (similar to the one above) across all our pages, without the use of master
pages. There are 2 options
Option 1: Copy and paste the following HTML across all pages in the application
<table style="font-family:Arial">
<tr>
<td colspan="2" style="width:800px; height:80px; background-color:#BDBDBD;
text-align:center">
<h1>
WebSite Header
</h1>
</td>
</tr>
<tr>
<td style="height:500px; background-color:#D8D8D8; width:150px">
<h3>Menu</h3>
</td>
<td style="height:500px; background-color:#E6E6E6; width:650px">
<h2>This section changes on a page by page basis</h2>
</td>
</tr>
<tr>
<td colspan="2" style="background-color:#BDBDBD; text-align:center">
<b>Website Footer</b>
</td>
</tr>
</table>
So, the best approach to have a consistent look and feel across all pages in a web application is
to use master pages. We will discuss master pages in our next video.
This is continuation to Part 146, please watch Part 146 before proceeding.
Master pages in asp.net allow you to create a consistent look and behaviour for all the pages in
an asp.net web application.
ContentPlaceHolder control defines a region on the master page, where the content pages can
plugin page specific content.
Now, let's design the master page so that it looks as shown below.
Notice that, we only have one ContentPlaceHolder now. Please pay attention to the ID of
the ContentPlaceHolder control. In a bit, we will see, how content pages use this ID to plugin
their page specific content.
Now, let's add a content page, that is going to use the master page for it's layout.
1. Right click on the "Master Page" in solution explorer and select "Add Content Page"
At this point, you should have a webform, with the following HTML.
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" C
odeBehind="WebForm1.aspx.cs" Inherits="WebFormsDemo.WebForm1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
</asp:Content>
Points to remember
1. html, head and body sections are not present on the content page, as they are already
present on the master page, that this content page is subscribed to.
2. Content pages are associated with master pages using "MasterPageFile" attribute of
the "Page" directive
So, an asp.net master page allows us to define a common layout for all the pages in the
application. Later, if we have to change something in the common layout, we only have to do it
at one place, that is in the master page.
Part 148 - Passing data from content page to master page in asp.net
Suggested Videos
Part 145 - Implementing autocomplete textbox in asp.net web forms
Part 146 - Why use master pages in asp.net
Part 147 - Master pages in asp.net
This is continuation to Part 147, please watch Part 147 before proceeding.
Let us understand how to pass data from a content page to a master page, with an example.
2. On the Content page, we have another textbox and a Button. The ID of this textbox
is TextBox1 and the ID of the button is Button1
3. When you type some text in the textbox on the content page and when you hit Set
Text button, we want to display the text entered in the textbox on the content page in the
textbox that is present in the master page.
Step 2: In the code-behind file of the master page, include a public property that returns the
textbox control. Content pages will use this property to set the text of the textbox on the master
page.
// The property returns a TextBox
public TextBox TextBoxOnMasterPage
{
get
{
// Return the textbox on the master page
return this.txtBoxOnMasterPage;
}
}
Step 3: Include a TextBox and a Button control on the content page. Here is the HTML.
<b>Type the text in the textbox that you want to display
in Master Page TextBox and click Set Text Button</b>
<asp:TextBox ID="TextBox1" runat="server">
</asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Set Text"
onclick="Button1_Click" />
Step 4: Copy and paste the following Button1_Click() event handler method in the content
page
protected void Button1_Click(object sender, EventArgs e)
{
// Retrieve the master page associated with this content page using
// Master property and typecast to the actual master page and then
// reference the property
((Site)Master).TextBoxOnMasterPage.Text = TextBox1.Text;
}
If you want Master property to return a strongly typed reference of the associated master
page, include the following MasterType directive on the content page.
<%@ MasterType VirtualPath="~/Site.Master" %>
Once you have included the above MasterType directive on the content page, you will now
have a strongly typed reference of the master page and can access it's properties without
having to typecast.
protected void Button1_Click(object sender, EventArgs e)
{
Master.TextBoxOnMasterPage.Text = TextBox1.Text;
}
To retrieve a master page associated with a content page, we can use either
1. Master propery
2. Page.Master property
Page.Master property always returns an object of type MasterPage and we need to explicitly
typecast it to the actual master page, if we need to access it's properties and methods.
Where as Master property returns a strongly typed reference of the actual master page if the
content page has MasterType directive specified. Otherwise Master property works in the
same way as Page.Master property.
Part 149 - Passing data from master page to content page in asp.net
Suggested Videos
Part 146 - Why use master pages in asp.net
Part 147 - Master pages in asp.net
Part 148 - Passing data from content page to master page
This is continuation to Part 148, please watch Part 148 before proceeding.
Let us understand how to pass data from a master page to a content page, with an example.
Step 2: Include the following 3 properties in the code-behind file of the master page. We will use
these properties in the content page.
public Panel SearchPanel
{
get
{
return panelSearch;
}
}
public string SearchTerm
{
get
{
return txtSerach.Text;
}
}
Step 3: We will be using table tblStudents. Here is the SQL Script to create and populate this
table with sample data.
Create Table tblStudents
(
ID int Identity Primary Key,
Name nvarchar(50),
Gender nvarchar(20),
TotalMarks int,
)
Step 4: Search stored procedure below, returns all students whose name contains
@SearchTerm parameter.
Create Proc spSearch
@SearchTerm nvarchar(50)
as
Begin
Select * from tblStudents where Name like '%' + @SearchTerm + '%'
End
Step 5: Add a content page, and include a GridView control to display the students.
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
Step 6: Copy and paste the following code in the code-behind file of the content page.
protected void Page_Init(object sender, EventArgs e)
{
Master.SearchButton.Click += new EventHandler(SearchButton_Click);
}
Let us look at another example of implementing search functionality on another content page.
This time we will use table tblEmployee. Here is the sql script to create and populate this table.
Create Table tblEmployee
(
Id int Identity Primary Key,
Name nvarchar(50),
Email nvarchar(50),
Age int,
Gender nvarchar(50),
HireDate date,
)
Search stored procedure below, returns all employees whose name contains @SearchTerm
parameter.
Create Proc spSearchEmployees
@SearchTerm nvarchar(50)
as
Begin
Select * from tblEmployee where Name like '%' + @SearchTerm + '%'
End
Add a content page, and include a GridView control to display the employees.
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
Copy and paste the following code in the code-behind file of the content page.
protected void Page_Init(object sender, EventArgs e)
{
Master.SearchButton.Click += new EventHandler(SearchButton_Click);
}
At this point, run the application and enter a search term in the search textbox on the master
page, and click Search button. Notice that the matching employees are displayed in the gridview
control on the content page.
Let's say on a specific content page, we don't want this search functionality. If that's the case,
we can very easily hide the search interface on the master page. Here are the steps.
Step 1: Add a new content page
Step 3: Copy and paste the following code in the code-behind file of the content page
protected void Page_Load(object sender, EventArgs e)
{
Master.SearchPanel.Visible = false;
}
Let's understand this with an example. The following ContentPlaceHolder in the master page
has some default content in an <h1> tag.
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
<h1>Content pages can override this content if they wish to</h1>
</asp:ContentPlaceHolder>
Now right click on the master page and select "Add Content Page". A new content page
should be added and would see the following HTML on the content page
<%@ Page Title="" Language="C#" MasterPageFile="~/Site2.Master" AutoEventWireup="true"
CodeBehind="WebForm5.aspx.cs" Inherits="WebFormsDemo.WebForm5" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
</asp:Content>
Notice that we have a content control in the content page. Normally we would place the
asp.net controls and may be some text inside the content control. However, if we want the
content page to default to the content in the ContentPlaceHolder of the master page.
1. Flip the content page to the design mode
2. Right click on the "ContentPlaceHolder" control and select "Default to Master's content"
3. You would get a confirmation dailog box
If you default to the Master Page content, everything in this region will be removed from the
page. Do you want to continue?
4. Click Yes
Notice that the default content from the master page "ContentPlaceHolder" is displayed. Now,
flip the content page to Source mode, and notice that, the content control is removed. So, if you
want a content page to default to master page default content, all you need to do is to remove
the content control from the content page.
Part 151 - Assigning a master page dynamically in asp.net
Suggested Videos
Part 148 - Passing data from content page to master page
Part 149 - Passing data from master page to content page
Part 150 - Default content in contentplaceholder of a master page
In this video, we will discuss, how to select a master page dynamically at runtime. Let us
understand this with an example.
2. Add a class file to the project and name it BaseMaterPage.cs. Copy and paste the following
code. Notice that the BaseMaterPage class inherits from System.Web.UI.MasterPage. This
BaseMaterPage class will be used as the base class for the 2 master pages that we will be
creating later.
using System.Web.UI.WebControls;
namespace WebFormsDemo
{
public abstract class BaseMaterPage : System.Web.UI.MasterPage
{
public abstract Panel SearchPanel { get; }
3. Add a master page and name it Site.Master. Copy and paste the following html.
<table style="font-family: Arial">
<tr>
<td colspan="2" style="width: 800px; height: 80px; text-align:
center; background-color: #BDBDBD;">
<h1>WebSite Header</h1>
<asp:Panel ID="panelSearch" runat="server">
<b>Search : </b>
<asp:TextBox ID="txtSerach" runat="server"></asp:TextBox>
<asp:Button ID="btnSearch" runat="server" Text="Search" />
</asp:Panel>
</td>
</tr>
<tr>
<td style="height: 500px; background-color: #D8D8D8; width: 150px">
<b>Select Master Page</b>
<asp:DropDownList ID="DropDownList1" runat="server"
AutoPostBack="true"
OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem Text="Master Page 1" Value="~/Site.Master">
</asp:ListItem>
<asp:ListItem Text="Master Page 2" Value="~/Site2.Master">
</asp:ListItem>
</asp:DropDownList>
</td>
<td style="height: 500px; background-color: #E6E6E6; width: 650px">
<asp:ContentPlaceHolder ID="MainContent" runat="server">
<h1>
Section that changes on a page by page basis</h1>
</asp:ContentPlaceHolder>
</td>
</tr>
<tr>
<td colspan="2" style="background-color: #BDBDBD; text-align: center">
<b>Website Footer</b>
</td>
</tr>
</table>
4. Copy and paste the following code in the code behind file. Notice that the Site master page
class inherits from the BaseMaterPage class that we created in Step 1.
public partial class Site : BaseMaterPage
{
protected void Page_Load(object sender, EventArgs e)
{
}
5. Add another master page and name it Site2.Master. Copy and paste the following html.
<table style="font-family: Arial; color:White">
<tr>
<td colspan="2" style="width: 800px; height: 80px;
text-align: center; background-color:Red;">
<h1>WebSite Header</h1>
<asp:Panel ID="panelSearch" runat="server">
<b>Search : </b>
<asp:TextBox ID="txtSerach" runat="server"></asp:TextBox>
<asp:Button ID="btnSearch" runat="server" Text="Search"/>
</asp:Panel>
</td>
</tr>
<tr>
<td style="height: 500px; background-color:Green; width: 650px">
<asp:ContentPlaceHolder ID="MainContent" runat="server">
6. Copy and paste the following code in the code behind file. Notice that the Site2 master page
class inherits from the BaseMaterPage class that we created in Step 1.
public partial class Site2 : BaseMaterPage
{
protected void Page_Load(object sender, EventArgs e)
{
}
7. Add a class file and name it BasePage.cs. Copy and paste the following code. Notice that
the BasePage class inherits from System.Web.UI.Page. This BasePage class will be used as
the base class for all the content pages in our application.
using System;
namespace WebFormsDemo
{
public class BasePage : System.Web.UI.Page
{
protected override void OnPreInit(EventArgs e)
{
if (Session["SELECTED_MASTERPAGE"] != null)
{
this.MasterPageFile = Session["SELECTED_MASTERPAGE"].ToString();
}
}
}
}
8. Right click on Site.Master page and select Add Content Page. Copy and paste the following
html. Notice that we have set MasterType to BaseMaterPage class.
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true"
9. In the code-behind file of the content page, copy and paste the following code.
public partial class WebForm1 : BasePage
{
protected void Page_Init(object sender, EventArgs e)
{
Master.SearchButton.Click += new
EventHandler(SearchButton_Click);
}
If you need the sql script to create and populate table tblStudents, please refer to Part 149.
The T-SQL for stored procedure spSearch is also available in Part 149.
Part 152 - Master page content page user control life cycle in asp.net
Suggested Videos
Part 149 - Passing data from master page to content page
Part 150 - Default content in contentplaceholder of a master page
Part 151 - Assigning a master page dynamically
In this video, we will understand the order of events execution, when we have a content
page with a master page and a user control.
2. Add a UserControl with name="TestUserControl.ascx". Copy and paste the following html.
This is a very simple user control, with just an h1 tag with some static text.
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="TestUserControl.ascx.cs"
Inherits="WebFormsDemo.TestUserControl" %>
<h1>This is a Test User Control</h1>
3. Add a MasterPage and name it Site.Master. Copy and paste the following code in the code-
behind file.
public partial class Site : System.Web.UI.MasterPage
{
protected void Page_Init(object sender, EventArgs e)
{
Response.Write("Master Page Init Event<br/>");
}
4. Right click on the MasterPage, and select Add Content Page. This should
add "WebForm1.aspx" with a content control in it. Flip WebForm1.aspx to design mode. Drag
and drop a textbox control and TestUserControl onto the content page. Associate event
handler methods for OnInit, OnLoad and OnPreRender events for the textbox and
the usercontrol. At this point the HTML should look as shown below.
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master"
AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs"
Inherits="WebFormsDemo.WebForm1" %>
<%@ Register src="TestUserControl.ascx"
tagname="TestUserControl" tagprefix="uc1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1"
runat="server">
<asp:TextBox ID="TextBox1" runat="server"
OnInit="TextBox1_Init" OnLoad="TextBox1_Load"
OnPreRender="TextBox1_PreRender">
</asp:TextBox>
<br />
<uc1:TestUserControl ID="TestUserControl1" runat="server"
OnInit="TestUC1_Init" OnLoad="TestUC1_Load"
OnPreRender="TestUC1_PreRender"/>
</asp:Content>
5. Finally copy and paste the following code in the code-behind file of WebForm1.
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Init(object sender, EventArgs e)
{
Response.Write("Content Page Init Event<br/>");
}
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("Content Page Load Event<br/>");
}
protected void Page_PreRender(object sender, EventArgs e)
{
Response.Write("Content Page PreRender Event<br/>");
}
protected void TextBox1_Init(object sender, EventArgs e)
{
Response.Write("TextBox Init Event<br/>");
}
protected void TextBox1_Load(object sender, EventArgs e)
{
Response.Write("TextBox Load Event<br/>");
}
protected void TextBox1_PreRender(object sender, EventArgs e)
{
Response.Write("TextBox PreRender Event<br/>");
}
protected void TestUC1_Init(object sender, EventArgs e)
{
Response.Write("UserControl Init Event<br/>");
}
protected void TestUC1_Load(object sender, EventArgs e)
{
Response.Write("UserControl Load Event<br/>");
}
protected void TestUC1_PreRender(object sender, EventArgs e)
{
Response.Write("UserControl PreRender Event<br/>");
}
}
Run the application and notice that, the events are executed in the following order.
TextBox Init Event
UserControl Init Event
Master Page Init Event
Content Page Init Event
Content Page Load Event
Master Page Load Event
TextBox Load Event
UserControl Load Event
Content Page PreRender Event
Master Page PreRender Event
TextBox PreRender Event
UserControl PreRender Event
So, in general the initialization events are raised from the innermost control to the outermost
one, and all other events are raised from the outermost control to the innermost one.
Please note that the master page is merged into the content page and treated as a control in
the content page. Master page is merged into the content page during the initialization stage of
page processing.
Two of our valuable youtube channel subscribers were asked the following question in
an interview.
Question 1: We have a master page, content page and a user control, with the following events
Master init & master load
Content init & Content load
User control init & User control load
When we run the page containing above things, in what sequence the above events are fired.
As the name speaks for itself, Menu control in asp.net is used to display a menu in an
asp.net web application.
The content for the Menu control can be specified directly in the control or the menu
control can be bound to a data source. We will discuss data binding in a later video session.
In this video, let's discuss specifying the contents directly in the control. A Menu control is a
collection of MenuItem objects.
To get a menu as shown in the image below,
By default, the menu control is displayed using vertical orientation. If you want to change
its orientation to Horizontal, use Orientation property.
<asp:Menu ID="Menu1" runat="server" Orientation="Horizontal">
By default, StaticDisplayLevels Property is set to 1. This means only the first level is statically
displayed. If you want 2 levels to be displayed statically, you would set this property to 2.
To control the amount of time it takes for the dynamically displayed portion of a menu to
disappear, use DisappearAfter property. This is in milliseconds. If you want the menu to
disappear after 2 seconds, you would set it to 2000 milliseconds as shown below.
<asp:Menu ID="Menu1" runat="server" DisappearAfter="2000">
In this video, we will discuss applying styles to the asp.net menu control. This is continuation
to Part 153, please watch Part 153 before proceeding.
To configure styles for the statically displayed portion of the menu, use
StaticMenuStyle - Applicable for the entire statically displayed portion of the menu
StaticMenuItemStyle - Applicable for the individual menu items in the statically displayed
portion of the menu
StaticSelectedStyle - Applicable for the selected menu item in the statically displayed portion
of the menu
StaticHoverStyle - Applicable for the statically displayed menu item over which mouse is
hovered over
To configure styles for the dynamically displayed portion of the menu, use
DynamicMenuStyle - Applicable for the entire dynamically displayed portion of the menu
DynamicMenuItemStyle - Applicable for the individual menu items in the dynamically displayed
portion of the menu
DynamicSelectedStyle - Applicable for the selected menu item in the dynamically displayed
portion of the menu
DynamicHoverStyle - Applicable for the dynamically displayed menu item over which mouse is
hovered over
LevelSelectedStyles - If the menu control has 2 levels, you will have the html as shown below.
Yellow color will be applied for the menu item selected at level 1. Maroon color will be applied
for menu item selected at level 2.
<LevelSelectedStyles>
<asp:MenuItemStyle ForeColor="Yellow" />
<asp:MenuItemStyle ForeColor="Maroon" />
</LevelSelectedStyles>
If you are following along with the example in the demo, for LevelSelectedStyles,
StaticSelectedStyle and DynamicSelectedStyle to work, you need the following code in
Page_Load of the master page.
protected void Page_Load(object sender, EventArgs e)
{
foreach (MenuItem item in Menu1.Items)
{
Check(item);
}
}
In this video, we will discuss binding asp.net menu control to an xml file using
xmldatasource control. This is continuation to Part 154, please watch Part 154 before
proceeding.
1. Add an XML file and name it MenuData.xml. Copy and paste the following xml.
<?xml version="1.0" encoding="utf-8" ?>
<Items>
<MenuItem NavigateUrl="~/Home.aspx" Text="Home"/>
<MenuItem NavigateUrl="~/Employee.aspx" Text="Employee">
<MenuItem NavigateUrl="~/UploadResume.aspx" Text="Upload Resume"/>
<MenuItem NavigateUrl="~/EditResume.aspx" Text="Edit Resume"/>
<MenuItem NavigateUrl="~/ViewResume.aspx" Text="View Resume"/>
</MenuItem>
<MenuItem NavigateUrl="~/Employer.aspx" Text="Employer">
<MenuItem NavigateUrl="~/UploadJob.aspx" Text="Upload Job"/>
<MenuItem NavigateUrl="~/EditJob.aspx" Text="Edit Job"/>
<MenuItem NavigateUrl="~/ViewJob.aspx" Text="View Job"/>
</MenuItem>
<MenuItem NavigateUrl="~/Admin.aspx" Text="Admin">
<MenuItem NavigateUrl="~/AddUser.aspx" Text="Add User"/>
<MenuItem NavigateUrl="~/EditUser.aspx" Text="Edit User"/>
<MenuItem NavigateUrl="~/ViewUser.aspx" Text="View User"/>
</MenuItem>
</Items>
3. Drag and drop a menu control and set DataSourceID attribute to the xmldatasource control
we created in Step 2. Also, set DataBindings as shown below.
<asp:Menu ID="Menu1" runat="server" DataSourceID="XmlDataSource1"
OnPreRender="Menu1_PreRender">
<DataBindings>
<asp:MenuItemBinding DataMember="MenuItem"
NavigateUrlField="NavigateUrl" TextField="Text" />
</DataBindings>
</asp:Menu>
4. To set the styles for the selected menu item, copy and paste the following code in the code-
behind file.
private void Check(MenuItem item)
{
if (item.NavigateUrl.Equals(Request.AppRelativeCurrentExecutionFilePath,
StringComparison.InvariantCultureIgnoreCase))
{
item.Selected = true;
}
else if (item.ChildItems.Count > 0)
{
foreach (MenuItem menuItem in item.ChildItems)
{
Check(menuItem);
}
}
}
In this video, we will discuss binding asp.net menu control to database table. This is
continuation to Part 155, please watch Part 155 before proceeding.
2. Create a stored procedure that returns data from both the tables.
Create Proc spGetMenuData
as
Begin
Select * from tblMenuItemsLevel1
Select * from tblMenuItemsLevel2
End
4. Copy and paste the following ado.net code in the code-behind file.
protected void Page_Load(object sender, EventArgs e)
{
GetMenuItems();
}
ds.Relations.Add("ChildRows", ds.Tables[0].Columns["ID"],
ds.Tables[1].Columns["ParentId"]);
If you want the menu control to apply selected menu item styles.
1. Include OnPreRender attribute in the aspx page
2. Copy and paste the following code in the code-behind file.
private void Check(MenuItem item)
{
if (item.NavigateUrl.Equals(Request.AppRelativeCurrentExecutionFilePath,
StringComparison.InvariantCultureIgnoreCase))
{
item.Selected = true;
}
else if (item.ChildItems.Count > 0)
{
foreach (MenuItem menuItem in item.ChildItems)
{
Check(menuItem);
}
}
}
In this video, we will discuss SiteMapPath control. This is continuation to Part 156, please
watch Part 156 before proceeding.
SiteMapPath control displays navigation path. This navigation path is often called
as breadcrumb. Using SiteMapPath control is straight forward.
2. Right click on the project name in solution explorer and add a SiteMap file. Copy and paste
the following content.
<?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0">
<siteMapNode title="DummyRoot">
<siteMapNode url="~/Home.aspx" title="Home"/>
<siteMapNode url="~/Employee.aspx" title="Employee">
<siteMapNode url="~/UploadResume.aspx" title="Upload Resume"/>
<siteMapNode url="~/EditResume.aspx" title="Edit Resume"/>
<siteMapNode url="~/ViewResume.aspx" title="View Resume"/>
</siteMapNode>
<siteMapNode url="~/Employer.aspx" title="Employer">
<siteMapNode url="~/UploadJob.aspx" title="Upload Job"/>
<siteMapNode url="~/EditJob.aspx" title="Edit Job"/>
<siteMapNode url="~/ViewJob.aspx" title="View Job"/>
</siteMapNode>
<siteMapNode url="~/Admin.aspx" title="Admin">
<siteMapNode url="~/AddUser.aspx" title="Add User"/>
<siteMapNode url="~/EditUser.aspx" title="Edit User"/>
<siteMapNode url="~/ViewUser.aspx" title="View User"/>
</siteMapNode>
</siteMapNode>
</siteMap>
That's it we are done. Run the application and notice that SiteMapPath control displays
the navigation path. The SiteMapPath control automatically reads data
from Web.sitemap file.
At the moment, the only problem is that it is displaying DummyRoot node as well. Here are the
steps to hide the Root node in SiteMapPath control in asp.net.
1. Specify OnItemCreated attribute for the SiteMapPath control on the master page.
2. Copy and paste the following code in the code-behind file of the master page
protected void SiteMapPath1_ItemCreated(object sender, SiteMapNodeItemEventArgs e)
{
if (e.Item.ItemType == SiteMapNodeItemType.Root ||
(e.Item.ItemType == SiteMapNodeItemType.PathSeparator && e.Item.ItemIndex == 1))
{
e.Item.Visible = false;
}
}
This is continuation to Part 157. Please watch Part 157, before proceeding. In Part 155, we
discussed binding menu control to an xml file using xmldatasource control.
3. Finally drag and drop Menu control onto the webform and set DataSourceID attribute, to the
ID of the SiteMapDataSource control created in Step 2.
<asp:Menu ID="Menu1" DataSourceID="SiteMapDataSource1" runat="server">
</asp:Menu>
That's it. Run the application and notice that, the Menu control is bound to web.sitemap file.
One problem here is that, the Root node is also displayed.
TreeView Web control is useful to display hierarchical data in a tree structure. The content
for the TreeView control can be specified directly in the control or the TreeView control can be
bound to
1. XML file
2. web.sitemap file
3. Database table
we would configure the TreeView control as shown below. Notice that we have
set Target attribute of TreeNode to _blank. This ensures that the target page is opened in a
new window.
<asp:TreeView ID="TreeView1" runat="server">
<Nodes>
<asp:TreeNode Text="Home" NavigateUrl="~/Home.aspx" Target="_blank" />
<asp:TreeNode Text="Employee" NavigateUrl="~/Employee.aspx" Target="_blank">
<asp:TreeNode Text="Upload Resume" NavigateUrl="~/UploadResume.aspx"
Target="_blank"/>
<asp:TreeNode Text="Edit Resume" NavigateUrl="~/EditResume.aspx"
Target="_blank"/>
<asp:TreeNode Text="View Resume" NavigateUrl="~/ViewResume.aspx"
Target="_blank"/>
</asp:TreeNode>
<asp:TreeNode Text="Employer" NavigateUrl="~/Employer.aspx" Target="_blank">
<asp:TreeNode Text="Upload Job" NavigateUrl="~/UploadJob.aspx" Target="_blank"/>
<asp:TreeNode Text="Edit Job" NavigateUrl="~/EditJob.aspx" Target="_blank"/>
<asp:TreeNode Text="View Job" NavigateUrl="~/ViewJob.aspx" Target="_blank"/>
</asp:TreeNode>
<asp:TreeNode Text="Admin" NavigateUrl="~/Admin.aspx" Target="_blank">
<asp:TreeNode Text="Add User" NavigateUrl="~/AddUser.aspx" Target="_blank"/>
<asp:TreeNode Text="Edit User" NavigateUrl="~/EditUser.aspx" Target="_blank"/>
<asp:TreeNode Text="View User" NavigateUrl="~/ViewUser.aspx" Target="_blank"/>
</asp:TreeNode>
</Nodes>
</asp:TreeView>
To configure the look and feel of the TreeView control, the following styles can be used.
1. HoverNodeStyle
2. LeafNodeStyle
3. RootNodeStyle
4. SelectedNodeStyle etc.
Part 160 - Binding asp.net treeview control to an xml file using
xmldatasource control
Suggested Videos
Part 157 - SiteMapPath control in asp.net
Part 158 - Binding menu control to web.sitemap file
Part 159 - TreeView control
1. Add an XML file and name it TreeViewData.xml. Copy and paste the following xml.
<?xml version="1.0" encoding="utf-8" ?>
<Items>
<TreeViewItem NavigateUrl="~/Home.aspx" Text="Home"/>
<TreeViewItem NavigateUrl="~/Employee.aspx" Text="Employee">
<TreeViewItem NavigateUrl="~/UploadResume.aspx" Text="Upload Resume"/>
<TreeViewItem NavigateUrl="~/EditResume.aspx" Text="Edit Resume"/>
<TreeViewItem NavigateUrl="~/ViewResume.aspx" Text="View Resume"/>
</TreeViewItem>
<TreeViewItem NavigateUrl="~/Employer.aspx" Text="Employer">
<TreeViewItem NavigateUrl="~/UploadJob.aspx" Text="Upload Job"/>
<TreeViewItem NavigateUrl="~/EditJob.aspx" Text="Edit Job"/>
<TreeViewItem NavigateUrl="~/ViewJob.aspx" Text="View Job"/>
</TreeViewItem>
<TreeViewItem NavigateUrl="~/Admin.aspx" Text="Admin">
<TreeViewItem NavigateUrl="~/AddUser.aspx" Text="Add User"/>
<TreeViewItem NavigateUrl="~/EditUser.aspx" Text="Edit User"/>
<TreeViewItem NavigateUrl="~/ViewUser.aspx" Text="View User"/>
</TreeViewItem>
</Items>
This is continuation to Part 160. Please watch Part 160, before proceeding. In Part 158, we
discussed binding menu control to web.sitemap file using sitemapdatasource control.
Binding treeview control to web.sitemap file is similar.
3. Finally drag and drop TreeView control onto the webform and set DataSourceID attribute, to
the ID of the SiteMapDataSource control created in Step 2.
<asp:TreeView ID="Treeview1" DataSourceID="SiteMapDataSource1" runat="server">
</asp:TreeView>
That's it. Run the application and notice that, the TreeView control is bound
to web.sitemap file. One problem here is that, the Root node is also displayed.
To open the target page in a new window set Target attribute of TreeView control to _blank as
shown below.
<asp:TreeView ID="Treeview1" Target="_blank" DataSourceID="SiteMapDataSource1" runat="
server">
</asp:TreeView>
In this video, we will discuss binding asp.net treeview control to database table. This is
continuation to Part 161, please watch Part 161 before proceeding.
4. Copy and paste the following ado.net code in the code-behind file.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GetTreeViewItems();
}
}
private void GetTreeViewItems()
{
string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
SqlConnection con = new SqlConnection(cs);
SqlDataAdapter da = new SqlDataAdapter("spGetTreeViewItems", con);
DataSet ds = new DataSet();
da.Fill(ds);
ds.Relations.Add("ChildRows", ds.Tables[0].Columns["ID"],
ds.Tables[0].Columns["ParentId"]);
The above code works as expected only with 2 levels of TreeNode objects. If you include a
third level, those TreeNodes will not be displayed in the TreeView control. For example, if you
execute the following insert sql script. These rows will not be displayed in the TreeView control,
we will discuss fixing this in our next video.
Insert into tblTreeViewItems values ('AAA', '~/AAA.aspx', 5)
Insert into tblTreeViewItems values ('BBB', '~/BBB.aspx', 5)
Part 163 - Dynamically adding treenodes to treeview control in asp.net
Suggested Videos
Part 160 - Binding asp.net treeview control to an xml file
Part 161 - Binding treeview control to web.sitemap file
Part 162 - Binding asp.net treeview control to database table
This is continuation to Part 162. Please watch Part 162, before proceeding.
We want to bind this data to a TreeView control. If you need the SQL Script to create and
populate the table with sample data, please refer to Part 162.
namespace WebFormsDemo
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GetTreeViewItems();
}
}
ds.Relations.Add("ChildRows", ds.Tables[0].Columns["ID"],
ds.Tables[0].Columns["ParentId"]);
if (row.GetChildRows("ChildRows").Length > 0)
{
GetChildRows(row, childTreeNode);
}
}
}
}
}
Part 164 - Displaying organization employee chart using treeview control
in asp.net
Suggested Videos
Part 161 - Binding treeview control to web.sitemap file
Part 162 - Binding asp.net treeview control to database table
Part 163 - Dynamically adding treenodes to treeview control
In this video, we will discuss, displaying organization employee chart using TreeView control.
We want to display above data in a TreeView control as shown below. A check box should be
displayed next to every TreeNode. On clicking the button, the selected employees must be
added to the listbox.
ASPX HTML
<div style="font-family:Arial">
<table>
<tr>
<td style="border:1px solid black">
<asp:TreeView ID="TreeView1" ShowCheckBoxes="All"
runat="server">
</asp:TreeView>
</td>
<td>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click"
Text=">>" />
</td>
<td>
<asp:ListBox ID="ListBox1" runat="server" Height="145px"
Width="100px">
</asp:ListBox>
</td>
</tr>
</table>
</div>
ASPX.CS Code:
using System;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace WebFormsDemo
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GetTreeViewItems();
}
}
ds.Relations.Add("ChildRows", ds.Tables[0].Columns["ID"],
ds.Tables[0].Columns["ManagerId"]);
if (row.GetChildRows("ChildRows").Length > 0)
{
GetChildRows(row, childTreeNode);
}
}
}
In this video we will discuss displaying an icon for an asp.net website on a browser tab. Let
me explain what I mean.
The size of the image should be 16 X16 pixels and it should be named favicon.ico. You can
either create your own image or use many free websites that are
available. http://www.favicon.cc is one such website.
Once you have the image, place it in the root directory of your web application and include the
following link in the head section of the page. If you have a master page, place it in the head
section of the master page.
<link rel="shortcut icon" href="~/favicon.ico" type="image/x-icon"/>
If you are testing this on your local machine, make sure to use local IIS Web Server instead of
built-in visual studio development server, otherwise the image will not be displayed.
In this video, we will discuss using captcha control in asp.net web application.
2. To prevent bogus user registrations. For example, when you try to create a new gmail
account, notice that there is a captcha control displayed on the registration page. This ensures
only humans can register and not automated scripts.
It is very difficult for automated computer programs to read the distorted text displayed in a
captcha control.
Example : The data entered in the webform below should only be saved to the database table
when captcha control passes validation
There are several free captcha controls available on the internet. I have
tested reCAPTCHA control with ASP.NET and works very well. It is very easy to integrate as
well. Here are the steps.
Step 4 : Create an empty asp.net web application. Name it Demo. Add a reference to
Recaptcha.dll assembly.
Step 9 : Copy and paste the following code in the code-behind page
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
namespace Demo
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void btnSubmit_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
string cs = ConfigurationManager.ConnectionStrings["CS"].ConnectionString;
using (SqlConnection con = new SqlConnection(cs))
{
SqlCommand cmd = new SqlCommand("spRegisterUser", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter paramName = new SqlParameter("@Name", txtName.Text);
SqlParameter paramEmail = new SqlParameter("@Email", txtEmail.Text);
SqlParameter paramPassword = new
SqlParameter("@Password", txtPassword.Text);
cmd.Parameters.Add(paramName);
cmd.Parameters.Add(paramEmail);
cmd.Parameters.Add(paramPassword);
con.Open();
cmd.ExecuteNonQuery();
}
lblMessage.Text = "Registration Successful";
}
else
{
lblMessage.Text = "Word verification failed";
}
}
}
}
If you have a validation summary control on the page and if the Recaptcha control has failed
validation it automatically displays the error message in Validation Summary control. All you
need is a validation summary control on the page where you want the error message to appear.
Run the application and click the Submit Button. Notice that the error message is displayed in
the validation summary control as expected.
Now let's discuss using a custom validator control with the recaptcha control. When the
word verification fails we want to display a RED asterisk (*) next to the captcha control and the
error message in validation summary control.
To achieve this
Step 1 : Include the custom validator on the page where you want the RED asterisk (*) to
appear
<asp:CustomValidator ID="CustomValidator1" runat="server" Text="*" ForeColor="Red"
OnServerValidate="RecaptchaValidator_ServerValidate">
</asp:CustomValidator>
Step 2 : Include the server side validation method in the code-behind file
protected void RecaptchaValidator_ServerValidate(object sender,
ServerValidateEventArgs e)
{
e.IsValid = this.recaptcha.IsValid;
}
In this short video we will discuss using the standard themes that come with recaptcha
control to customize it's look and feel in an asp.net web application.
The following are the different standard themes available. The default theme is red.
1. red
2. white
3. blackglass
4. clean
The Theme property can be set declaratively in the HTML or programatically in the code-
behind file. The theme that is set programatically overrides the theme that is set declaratively in
the HTML.
and this is how the recaptcha control looks with blackglass theme
For complete documentation on customizing the Look and Feel of reCAPTCHA, please visit
https://developers.google.com/recaptcha/docs/customization
In this video we will discuss how to save images to database table using asp.net and c#.
When upload button is clicked, the image should be uploaded to a database table and "View
Uploaded Image" link should appear. When you click on this link we should load the image
from the database and display it on the web page.
In this video we will discuss how to upload the image to the database. In our next video we will
discuss how to download the image from the database and display it on the web page.
Step 1 : Create table tblImages. This table stores the uploaded images along with it's name
and size in bytes.
Step 5 : Add 2 WebForms to the ASP.NET project. Name them WebForm1.aspx and
WebForm2.aspx.
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Web;
namespace Demo
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
lblMessage.Visible = false;
hyperlink.Visible = false;
}
}
string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using (SqlConnection con = new SqlConnection(cs))
{
SqlCommand cmd = new SqlCommand("spUploadImage", con);
cmd.CommandType = CommandType.StoredProcedure;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
lblMessage.Visible = true;
lblMessage.ForeColor = System.Drawing.Color.Green;
lblMessage.Text = "Upload Successful";
hyperlink.Visible = true;
hyperlink.NavigateUrl = "~/WebForm2.aspx?Id=" +
cmd.Parameters["@NewId"].Value.ToString();
}
}
else
{
lblMessage.Visible = true;
lblMessage.ForeColor = System.Drawing.Color.Red;
lblMessage.Text = "Only images (.jpg, .png, .gif and .bmp) can be uploaded";
hyperlink.Visible = false;
}
}
}
}
In our next video we will discuss how to download an image from the database and display
it on the web page.
Load image from database in asp.net
Suggested Videos
Part 167 - Using ASP.NET validation controls with recaptcha
Part 168 - Customizing recaptcha control
Part 169 - Save image to database using asp.net
In this video we will discuss how to load image from database using asp.net and c#. This is
continuation to Part 169. Please watch Part 169 from ASP.NET tutorial for beginner.
When we click on "View Uploaded Image" link, the user will be redirected to WebForm2.aspx.
The Id of the image will be passed as a query string parameter. In the page load event, we need
to retrieve the image from the database and display it on the web page.
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
namespace Demo
{
public partial class WebForm2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using (SqlConnection con = new SqlConnection(cs))
{
SqlCommand cmd = new SqlCommand("spGetImageById", con);
cmd.CommandType = CommandType.StoredProcedure;
con.Open();
byte[] bytes = (byte[])cmd.ExecuteScalar();
string strBase64 = Convert.ToBase64String(bytes);
Image1.ImageUrl = "data:Image/png;base64," + strBase64;
}
}
}
}
In this video we will discuss how to retrieve images from database and display them in
gridview using c# and asp.net. This is continuation to Parts 169 and 170. Please watch Parts
169 and 170 from ASP.NET tutorial for beginners tutorial.
When the web page initially loads, all the images should be retrieved from the database table
and displayed in the GridView control. When you click upload button, the new image should be
uploaded to the database, and the GridView should refresh and display the image you just
uploaded.
Webform HTML
<asp:FileUpload ID="FileUpload1" runat="server" />
<br /><br />
<asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="btnUpload_Click" />
<br /><br />
<asp:Label ID="lblMessage" runat="server"></asp:Label>
<br /><br />
<asp:HyperLink ID="hyperlink" runat="server">View Uploaded Image</asp:HyperLink>
<br /><br />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Size" HeaderText="Size (bytes)" />
<asp:TemplateField HeaderText="Image">
<ItemTemplate>
<asp:Image ID="Image1" runat="server" Height="100px" Width="100px"
ImageUrl='<%# "data:Image/png;base64,"
+ Convert.ToBase64String((byte[])Eval("ImageData")) %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Webform Code-Behind
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Web;
namespace Demo
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
lblMessage.Visible = false;
hyperlink.Visible = false;
LoadImages();
}
}
private void LoadImages()
{
string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using (SqlConnection con = new SqlConnection(cs))
{
SqlCommand cmd = new SqlCommand("Select * from tblImages", con);
con.Open();
SqlDataReader rdr = cmd.ExecuteReader();
GridView1.DataSource = rdr;
GridView1.DataBind();
}
}
string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using (SqlConnection con = new SqlConnection(cs))
{
SqlCommand cmd = new SqlCommand("spUploadImage", con);
cmd.CommandType = CommandType.StoredProcedure;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
lblMessage.Visible = true;
lblMessage.ForeColor = System.Drawing.Color.Green;
lblMessage.Text = "Upload Successful";
hyperlink.Visible = true;
hyperlink.NavigateUrl = "~/WebForm2.aspx?Id=" +
cmd.Parameters["@NewId"].Value.ToString();
LoadImages();
}
}
else
{
lblMessage.Visible = true;
lblMessage.ForeColor = System.Drawing.Color.Red;
lblMessage.Text = "Only images (.jpg, .png, .gif and .bmp) can be uploaded";
hyperlink.Visible = false;
}
}
}
}
Maximum request length exceeded error : You get this error when the default upload size of
4MB is exceeded. To increase the maxRequestLength include the following setting in your
web.config file.
<system.web>
<httpRuntime executionTimeout="3600" maxRequestLength="1048576"/>
<compilation debug="true"/>
</system.web>
If you are using II7 and above, in addition to the above, you also need to
increase maxAllowedContentLength
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="1073741824" />
</requestFiltering>
</security>
</system.webServer>
In this video we will discuss displaying data from two or more database table columns in an
ASP.NET Dropdownlist
Retrieve data from the database table and display it in the dropdownlist as shown below. Data
from the Currency and Rate columns must be concatenated and displayed in the dropdownlist.
Step 1 : Create tblRates table and populate it with test data using the SQL Script below
Use SampleDB
Go
Step 5 : Drag and Drop a Dropdownlist on the WebForm. Set the ID of the dropdownlist to
ddlPrice. The HTML in the WebForm at this point should be as shown below.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body style="font-family: Arial">
<form id="form1" runat="server">
Price :
<asp:DropDownList ID="ddlPrice" runat="server"></asp:DropDownList>
</form>
</body>
</html>
Step 6 : Copy and paste the following code in the code-behind file
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.WebControls;
namespace Demo
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindDataSetData();
}
}
ddlPrice.DataTextField = "CurrencyAndRate";
ddlPrice.DataValueField = "Id";
ddlPrice.DataSource = ds;
ddlPrice.DataBind();
}