Rich Server Control
Rich Server Control
ASP.NET provides large set of controls. These controls are divided into different categories,
depends upon their functionalities. The followings control comes under the rich controls category.
FileUpload control
Calendar control
AdRotator control
MultiView control
FileUpload control
FileUpload control is used to browse and upload files. After the file is uploaded, you can store the
file on any drive or database. FileUpload control is the combination of a browse button and a text
box for entering the filename.
namespace file
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Button1_Click(object sender, EventArgs e)
{
if(IsPostBack)
{
string path = "D:\\ASP\\";
if(FileUpload1.HasFile)
{
string extention = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
if(extention==".jpg")
{
Response.Write("Upload Success");
FileUpload1.SaveAs(path+FileUpload1.FileName);
}
else
{
Response.Write("Upload not success");
}
}
}
Calendar control
Calendar control provides you lots of property and events. By using these properties and events you
can perform the following task with calendar control.
Select date.
Selecting a day, a week or a month.
Customize the calendar's appearance.
Event Description
SelectionChanged This event is fired when you select a day, a week or an entire month.
DayRender This event is fired when each data cell of the calendar control is rendered.
VisibleMonthChanged It is raised when user changes a month.
Calendar control supports SelectionMode property that allows you to select a single day,
week, or entire month.
Example
using System;
using System.Text;
public partial class RichControl : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{
Label1.Text ="Todays date is: "+ Calendar1.TodaysDate.ToShortDateString();
Label2.Text = "Your date of birth is: " + Calendar1.SelectedDate.ToShortDateString();
}
}
When you select a date, SelectionChanged event will fired and displays the date in a label
controls. In this example the date format is MM/DD/YYYY. Output:
AdRotator control
AdRotator control is used to display different advertisements randomly in a page. The list of
advertisements is stored in either an XML file or in a database table. Lots of websites uses
AdRotator control to display the advertisements on the web page.
To create an advertisement list, first add an XML file to your project.
WebForm1.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs"
Inherits="ad.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:AdRotator ID="AdRotator1" runat="server" AdvertisementFile="~/XMLFile1.xml" />
</div>
</form>
</body>
</html>
XMLFile1.xml
<?xml version="1.0" encoding="utf-8" ?>
<Advertisements>
<Ad>
<ImageUrl>~/Images/fileswap.gif</ImageUrl>
<NavigateUrl>http://www.fileswap.com</NavigateUrl>
<AlternateText>
Upload Files
</AlternateText>
<Impressions>20</Impressions>
<Keyword>Upload</Keyword>
</Ad>
<Ad>
<ImageUrl>~/Images/ssprw.gif</ImageUrl>
<NavigateUrl>http://www.speedysparrow.com</NavigateUrl>
<AlternateText>Host Website</AlternateText>
<Impressions>20</Impressions>
<Keyword>Host</Keyword>
</Ad>
</Advertisements>
MultiView control
MultiView control can be used when you want to create a tabbed page. In many situations, a web
form may be very long, and then you can divide a long form into multiple sub forms. MultiView
control is made up of multiple view controls. You can put multiple ASP.NET controls inside view
controls. One View control is displayed at a time and it is called as the active view. View control
does not work separately. It is always used with a Multiview control.
If working with Visual Studio 2010 or later, you can drag and drop a MultiView control onto the
form. You can drag and drop any number of View controls inside the MultiView control. The
number of view controls is depends upon the need of your application.
WebForm1.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs"
Inherits="multi.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h3>Multiview with 3 view</h3>
<asp:DropDownList ID="d1" OnSelectedIndexChanged="d1_SelectedIndexChanged"
runat="server" AutoPostBack="true">
<asp:ListItem Value="0">View 1</asp:ListItem>
<asp:ListItem Value="1">View 2</asp:ListItem>
<asp:ListItem Value="2">View 3</asp:ListItem>
</asp:DropDownList>
</asp:MultiView>
</div>
</form>
</body>
</html>
WebForm1.aspx.cs
using System.Web.UI.WebControls;
namespace multi
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void d1_SelectedIndexChanged(object sender, EventArgs e)
{
m.ActiveViewIndex = d1.SelectedIndex;
}
}
}