Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

ASP.

NET PRACTICAL SOLUTION


PREPARED BY: PROF. BHAUMIK SHAH

12 Create following web page using File Upload Control. Provide following restrictions on it.
1. If user doesn’t select a file and press submit button then message should be display “Please
select file to upload...” in red color.
2. It allows only Image file with extension .jpg or .jpeg
3. Max file size is 2 MB to upload.

public partial class _Default : System.Web.UI.Page


{
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
string fileExtension =
System.IO.Path.GetExtension(FileUpload1.FileName);

if (fileExtension.ToLower() != ".doc" && fileExtension.ToLower()


!= ".docx")
{
Label1.Text = "Only .jpg or .jpeg are allowed....";
Label1.ForeColor = System.Drawing.Color.Red;
}
else
{
int filesize = FileUpload1.PostedFile.ContentLength;
if (filesize > 2097152)
{
Label1.Text = "Maximum file size (2MB) exceeded....";
Label1.ForeColor = System.Drawing.Color.Red;
}
else
{
FileUpload1.SaveAs(Server.MapPath("~/upload/" +
FileUpload1.FileName));
Label1.Text = "File uploaded successfully....";
Label1.ForeColor = System.Drawing.Color.Green;
}

1
PREPARED BY: PROF. BHAUMIK SHAH
ASP.NET PRACTICAL SOLUTION
PREPARED BY: PROF. BHAUMIK SHAH
}
}
else
{
Label1.Text = "Please select the File....";
Label1.ForeColor = System.Drawing.Color.Red;
}
}
}

13 Create a website that displays two advertisements alternately. When the user clicks on one of
the advertisements, he/she is redirected to “www.amazon.com”, and the other advertisement
redirects the user to “www.sony.com”. The advertisement should be centred horizontally and should
cover 60% of the width of the screen. Its height should be 80 units. The width of the border should be 5
units.

Code: XMLFile.xml

<?xml version="1.0" encoding="utf-8" ?>


<Advertisements>
<Ad>
<ImageUrl>~/Images/Amazon.jpg</ImageUrl>
<NavigateUrl>http://amazon.com</NavigateUrl>
<AlternateText>This is the advertisement of amazon</AlternateText>
<Impressions>30</Impressions>
<Keyword>shopping</Keyword>
</Ad>
<Ad>
<ImageUrl>~/Images/google.jpg</ImageUrl>
<NavigateUrl>http://google.com</NavigateUrl>
<AlternateText>This is the advertisement of google</AlternateText>
<Impressions>50</Impressions>
<Keyword>learn</Keyword>
</Ad>
</Advertisements>
2
PREPARED BY: PROF. BHAUMIK SHAH
ASP.NET PRACTICAL SOLUTION
PREPARED BY: PROF. BHAUMIK SHAH

Code: Prg13.aspx

<body>
<form id="form1" runat="server">
<div>
<table width="80%" align="center">
<tr>
<td align="center">
<h2>AdRotator Demo</h2>
</td>
</tr>
<tr>
<td align="center">
<asp:AdRotator ID="AdRotator1" runat="server"
AdvertisementFile="~/XMLFile.xml"
BorderWidth="5px" Height="80px" Width="100px" />

</td>
</tr>
</table>
</div>
</form>
</body>

14 Program using ASP.NET Validation controls. Create the application that accepts name, password, age,
email id, and user id. All the information entry is compulsory. Password should be reconfirmed. Age
should be within 21 to 30. Email id should be valid. User id should have at least a capital letter and digit
as well as length should be between 7 and 20 characters.

3
PREPARED BY: PROF. BHAUMIK SHAH
ASP.NET PRACTICAL SOLUTION
PREPARED BY: PROF. BHAUMIK SHAH
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page


{

protected void CustomValidator1_ServerValidate(object source,


ServerValidateEventArgs args)
{
string inputData = args.Value;
args.IsValid = false;
if (inputData.Length < 7 || inputData.Length > 20) return;
bool upperCase = false;
foreach (char ch in inputData)
{
if (ch >= 'A' && ch <= 'Z')
{
upperCase = true;
break;
}
}
if (!upperCase) return;
bool number = false;
foreach (char ch in inputData)
{
if (ch >= '0' && ch <= '9')
{
number = true; break;
}
}
if (!number) return;
args.IsValid = true;
}

15 Using all validation controls.

4
PREPARED BY: PROF. BHAUMIK SHAH
ASP.NET PRACTICAL SOLUTION
PREPARED BY: PROF. BHAUMIK SHAH

Properties to be set

Field Name Control Properties


First Name TextBox1
Last Name TextBox2, RequiredFieldValidator Error Message: Enter Last Name
ControlToValidate: TextBox2
Text: *
Age TextBox3, RangeValidator Error Message: Enter Age Between
18 to 30
ControlToValidate: TextBox3
Text: *
Enter Password TextBox4
Re-type TextBox5, CompareValidator Error Message:Both Password must
Password be same
ControlToValidate: TextBox5
ControlToCompare:TextBox4
Text: *
Email TextBox6, RegularExpressionValidator Error Message: Enter Valis Email
Address
ControlToValidate: TextBox6
Text: *
ValidationExpression:Internet Email
Address
ValidationSummary

16 Develop following program using Textbox, ImageButton and calendar control. When user clicks on
calendar ImageButton, Calendar should be displayed. Selected date should be displayed in a textbox.

5
PREPARED BY: PROF. BHAUMIK SHAH
ASP.NET PRACTICAL SOLUTION
PREPARED BY: PROF. BHAUMIK SHAH

public partial class _Default : System.Web.UI.Page


{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Calendar1.Visible = false;
}

}
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
if (Calendar1.Visible)
{
Calendar1.Visible = false;
}
else
{
Calendar1.Visible = true;
}
}
protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{
TextBox1.Text = Calendar1.SelectedDate.ToString("d");
}
}

17 By using a PlaceHolder control dynamically add Image by clicking Add Image button.

6
PREPARED BY: PROF. BHAUMIK SHAH
ASP.NET PRACTICAL SOLUTION
PREPARED BY: PROF. BHAUMIK SHAH

Design Output

protected void Button1_Click(object sender, EventArgs e)


{
Image img = new Image();
img.ImageUrl = "~/Images/cal.jpg";
img.BorderWidth = 5;
PlaceHolder1.Controls.Add(img);

18 Create Master Page with menu control and content place holder.

Creating a MasterPage

To create a master page, we need to:

7
PREPARED BY: PROF. BHAUMIK SHAH
ASP.NET PRACTICAL SOLUTION
PREPARED BY: PROF. BHAUMIK SHAH

 Go to "Add New Item".


 Select the MasterPage and design it.
 Our master page is MasterPageOne.Master.
 Add a menu bar on this master page on top of the page. This Menu bar will be common to all the
pages.
 We can have content pages use the master page.
 Add content pages like home.aspx, about_us.aspx, Services.aspx, Products.aspx and
Contact_us.aspx.
 When we add these content pages, we need to remember to select the option of "Use master
Page".

19 Count number of clicks when user press Count Clicks button in a Textbox using ViewState.

public partial class view_state : System.Web.UI.Page


{
int c = 0;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
TextBox1.Text = "0";
}

}
protected void Button1_Click(object sender, EventArgs e)
{
if (ViewState["clicks"] != null)
{
c = (int)ViewState["clicks"] + 1;
}

TextBox1.Text = c.ToString();
ViewState["clicks"] = c;
}
protected void Button2_Click(object sender, EventArgs e)
{

}
8
PREPARED BY: PROF. BHAUMIK SHAH
ASP.NET PRACTICAL SOLUTION
PREPARED BY: PROF. BHAUMIK SHAH
}
20 Send the information from one page to another page using QueryString.

Page 1 Page 2

Code of Page 1

protected void Button1_Click(object sender, EventArgs e)


{
Response.Redirect("nextpage2.aspx?name=" + Server.UrlEncode(txtname.Text) +
"&city=" + Server.UrlEncode(txtcity.Text));

Code of Page 2 (nextpage2.aspx)

protected void Button1_Click(object sender, EventArgs e)


{
Label1.Text = "Name = " + Request.QueryString["name"].ToString();
Label2.Text = "City = " + Request.QueryString["city"].ToString();
}

21 Create a Form that receives the user name, address, date, nationality, country preferred for working and
skill sets from the user and stores the user name on the client side using cookies. The country preferred
data should appear in a dropdownlist, whereas others should be entered in a textbox. By Clicking
submit button all information should be display on another web page. Store cookies on client machine
for 10Days.

9
PREPARED BY: PROF. BHAUMIK SHAH
ASP.NET PRACTICAL SOLUTION
PREPARED BY: PROF. BHAUMIK SHAH

protected void Button1_Click(object sender, EventArgs e)


{
HttpCookie cookie1 = new HttpCookie("student");
cookie1["name"] = TextBox1.Text;
cookie1["add1"] = TextBox2.Text;
cookie1["dob"] = TextBox3.Text;
cookie1["nationality"] = TextBox4.Text;
cookie1["country"] = DropDownList1.Text;
cookie1["skill"] = TextBox5.Text;

cookie1.Expires = DateTime.Now.AddDays(10);
Response.Cookies.Add(cookie1);
Response.Redirect("next_page.aspx");
}

Code of next_page.aspx

protected void Page_Load(object sender, EventArgs e)


{
HttpCookie cookie = Request.Cookies["student"];
if (cookie != null)
{
Label1.Text = cookie["name"];
Label2.Text = cookie["add1"];
Label3.Text = cookie["dob"];
Label4.Text = cookie["nationality"];
Label5.Text = cookie["country"];
Label6.Text = cookie["skill"];

}
}

10
PREPARED BY: PROF. BHAUMIK SHAH
ASP.NET PRACTICAL SOLUTION
PREPARED BY: PROF. BHAUMIK SHAH

11
PREPARED BY: PROF. BHAUMIK SHAH

You might also like