0% found this document useful (0 votes)
120 views

ASP .Net Practicals-SEM-5

.net practical
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
120 views

ASP .Net Practicals-SEM-5

.net practical
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

1. Write a Program to Print "Hello Computer" On Web Form.

<form id="form1" runat="server">

<div>

<%Response.Write( "Hello Computer"); %>

</div>

</form>

Output :

2. Write a Program Using Different Namespaces.

using System;

namespace MyProgram

public class MyClass

public static void Main()

MyNamespace.SampleClass.myMethod();

namespace MyNamespace
{

public class SampleClass

public static void myMethod()

Console.WriteLine("Creating my namespace");

Output :

3. Write a Program To Set a Link For New Page.

Step 1 – Open the Visual Studio –> Create a new empty Web application.

Step 2 – Create two New web page one for display hyperlink and other for navigate to it.

Step 3 – Drag and drop HyperLink control on web page from Toolbox.

Step 4 – Set Text property of Hyperlink Control

Step 5 – Set NavigateUrl Property to destination web page name.

<form id="form1" runat="server">


<div>

<asp:HyperLink ID="HyperLink1" runat="server"


NavigateUrl="~/SamplePage2.aspx">Navigate to Second Page</asp:HyperLink>

</div>

</form>

Output :
4. Write a Program to Demonstrate Request, Response and Server Object.

<form id="form1" runat="server">

<div>

<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>

<%-- Requesting Response With Button --%>

<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click"


/>

</div>

</form>

The code behind Button1_Click:

protected void Button1_Click(object sender, EventArgs e)

// Send Response When Button1 Clicks

Label1.Text = "It Works";


// Send SERVER's OBJECT Responce.

Label2.Text = Server.UrlDecode(Request.Url.ToString());

Output:

5. Write a Program using delegate in which addition and substraction of two Interger
value Possible.

using System;

public delegate int MyDelegate(int a, int b); //declaring a delegate

class Program

static void Main(string[] args)

MyDelegate addDelegate = Calculation.add;

Console.WriteLine(addDelegate(5, 2));

MyDelegate subDelegate = Calculation.sub;

Console.WriteLine(subDelegate(5, 2));

class Calculation

{
public static int add(int n1, int n2)

return n1 + n2;

public static int sub(int a, int b)

return a - b;

Output :

6. Write a program using while or for loop to print sum of first 100 ODD and Even
Numbers.

using System;

namespace SumOfOdd_Even

class Program

static void Main(string[] args)

// Calling Functions..

sumOfOdd();

sumOfEven();

}
// Function for Sum of First 100 Odd Integers.

static void sumOfOdd()

int num = 100, count = 1, sum = 0;

while (count <= num)

if (count % 2 != 0)

sum = sum + count;

count++;

Console.WriteLine("Sum of ODD integer number is " + sum);

// Function for Sum of First 100 Even Integers.

static void sumOfEven(){

int num = 100, count = 1, sum = 0;

while (count <= num){

if (count % 2 == 0){

sum = sum + count;

count++;

Console.WriteLine("Sum of Even integer number is " + sum);

}
Output :

7. Write a program using Foreach loop.

using System;

namespace Basics

class Program

static void Main(string[] args)

string[] Avengers = { "Thor", "Hulk", "Ironman", "Natasha" };

foreach (string i in Avengers)

{
Console.WriteLine(i);

Output :

8. Write a program to Create Simple Web Application using two or more web form.

Web-Form-1.aspx

<asp:HyperLink ID="HyperLink1" runat="server"


NavigateUrl="~/WebForm2.aspx">Go to Web Form 2</asp:HyperLink>

<br/>

<asp:HyperLink ID="HyperLink2" runat="server"


NavigateUrl="~/WebForm3.aspx">Go to Web Form 3</asp:HyperLink>

Web-Form-2.aspx

<div>

<h1> Web Form 2 </h1>

</div>

Web-Form-3.aspx

<div>

<h1> Web Form 2 </h1>


</div>

9. Write a program to demonstrate different Common Control.

<form id="form1" runat="server">

<div>

<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> <br />

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <br/>

<asp:Button ID="Button1" runat="server" Text="Button" />

</div>

</form>

Output :

10. Write a program to add the value of Text Box in to Dropdown List and List box
Controls.

<form id="form1" runat="server">

<div>

<asp:DropDownList ID="DropDownList1" runat="server">

<asp:ListItem>Value 1</asp:ListItem>

<asp:ListItem>Value 2</asp:ListItem>

<asp:ListItem>Value 3</asp:ListItem>

</asp:DropDownList>

<asp:Button ID="btnAdd" runat="server" OnClick="btnAdd_Click" Text="Add Item"


/>

<br />
<asp:ListBox ID="ListBox1" runat="server">

<asp:ListItem>List Item 1</asp:ListItem>

<asp:ListItem>List Item 2</asp:ListItem>

<asp:ListItem>List Item 3</asp:ListItem>

</asp:ListBox>

<asp:Button ID="btnAdd2" runat="server" Text="Add item"


OnClick="btnAdd2_Click" />

</div>

</form>

Code-Behind :

protected void btnAdd_Click(object sender, EventArgs e)

DropDownList1.Items.Add("New Added");

protected void btnAdd2_Click(object sender, EventArgs e)

ListBox1.Items.Add("New Added");

Output :

11. Write a program to Delete Items from Dropdown list and List box.
<form id="form1" runat="server">

<div>

<asp:DropDownList ID="DropDownList1" runat="server">

<asp:ListItem>Item 1</asp:ListItem>

<asp:ListItem>Item 2</asp:ListItem>

<asp:ListItem>Item 3</asp:ListItem>

</asp:DropDownList>

<asp:Button ID="btnDelete" runat="server" OnClick="btnDelete_Click" Text="Delete


Item" />

<br />

<asp:ListBox ID="ListBox1" runat="server">

<asp:ListItem>Item 1</asp:ListItem>

<asp:ListItem>Item 2</asp:ListItem>

<asp:ListItem>Item 3</asp:ListItem>

</asp:ListBox>

<asp:Button ID="btnDelete2" runat="server" OnClick="Button1_Click" Text="Delete Item" />

<br />

</div>

</form>

Code-Behind :

protected void btnDelete_Click(object sender, EventArgs e)

DropDownList1.Items.RemoveAt(DropDownList1.Items.IndexOf(DropDownList1.SelectedItem));

protected void Button1_Click(object sender, EventArgs e)

{
ListBox1.Items.RemoveAt(ListBox1.Items.IndexOf(ListBox1.SelectedItem));

Output :

12. Write a program to set Image on Image Control according to selection of image name
from dropdown list.

Web-Form :

<form id="form1" runat="server">

<div>

<asp:DropDownList ID="ddlImages" runat="server" AppendDataBoundItems="true"


AutoPostBack = "true" OnSelectedIndexChanged = "FetchImage">

<asp:ListItem Text="Select Image" Value="0" />

<asp:ListItem Value="1">Image 1</asp:ListItem>

<asp:ListItem Value="2">Image 2</asp:ListItem>

<asp:ListItem Value="3">Image 3</asp:ListItem>

</asp:DropDownList>

<hr />

<asp:Image ID="Image1" runat="server" Visible = "false"/>

</div>

</form>

Code-Behind :

protected void FetchImage(object sender, EventArgs e)

string id = ddlImages.SelectedItem.Value;
Image1.Visible = id != "0";

if (id == "1")

Image1.ImageUrl = "images/image1.jpg";

if (id == "2")

Image1.ImageUrl = "images/image2.jpg";

if (id == "3")

Image1.ImageUrl = "images/image3.jpg";

Output :

13. Write a program which demonstrate the Validation Control.


<form id="form1" runat="server">

<div>

Username : <br />

<asp:TextBox ID="txtUsername" runat="server" style="width:


128px"></asp:TextBox>

<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"


ControlToValidate="txtUsername" ErrorMessage="*" ForeColor="#FF3300"
Display="Dynamic">Username is Requied</asp:RequiredFieldValidator>

<br />

<asp:Label ID="Label1" runat="server" Text="Email :"></asp:Label>:

<br />

<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>

<asp:RegularExpressionValidator ID="RegularExpressionValidator1"
runat="server" ControlToValidate="txtEmail" ErrorMessage="*" ForeColor="Red"
ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">Invalid
Email!</asp:RegularExpressionValidator>

<br />

<asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click"


/>

</div>

</form>

Output :
14. Write a program which is use different Rich Control.

<form id="form1" runat="server">

<div>

<asp:FileUpload ID="FileUpload1" runat="server" />

<br />

<asp:Calendar ID="Calendar1" runat="server"></asp:Calendar>

<br />

<asp:MultiView ID="MultiView1" runat="server">

</asp:MultiView>

</div>

</form>
Output :

15. Write a program using Menu Control.

<form id="form1" runat="server">

<div>
<asp:Menu ID="Menu1" runat="server" Orientation="Horizontal">

<Items>

<asp:MenuItem Text="Blog" Value="Blog"></asp:MenuItem>

<asp:MenuItem Text="Docs" Value="Docs"></asp:MenuItem>

<asp:MenuItem Text="Downloads" Value="Downloads"></asp:MenuItem>

<asp:MenuItem NavigateUrl="~/Contact.aspx" Text="Contact Us"


Value="Contact Us"></asp:MenuItem>

<asp:MenuItem NavigateUrl="~/About.aspx" Text="About Us" Value="About


Us"></asp:MenuItem>

</Items>

</asp:Menu>

</div>

</form>

Output :

16. Write a program using TreeView Control,

<form id="form1" runat="server">

<div>
<asp:TreeView ID="TreeView1" runat="server">

<Nodes>

<asp:TreeNode Text="Parent1" Value="Parent1">

<asp:TreeNode Text="Child 1" Value="Child 1"></asp:TreeNode>

<asp:TreeNode Text="Child 2" Value="Child 2"></asp:TreeNode>

</asp:TreeNode>

<asp:TreeNode Text="Parent 2" Value="Parent 2">

<asp:TreeNode Text="Child 1" Value="Child 1"></asp:TreeNode>

<asp:TreeNode Text="Child 2" Value="Child 2"></asp:TreeNode>

</asp:TreeNode>

</Nodes>

</asp:TreeView>

</div>

</form>

Output :

17. Write a program using Sitemap Control.

Step 1: Add Four web forms to the application named Default.aspx Sem1.aspx and Sem2.aspx
Sem3.aspx by performing the following steps:
Move to the Solution Explorer window

Right-click on the application name and select the Add New Item option from the context
menu

Name the web form as Defalult.aspx and click the Add button

Step 2: Similarly add the Sem1.aspx, Sem2.aspx, Sem3.aspx and Clang.aspx web form to the
application. After that we have to add the Site Map file into the project.

The Site Map file is the XML file and has the extension .sitemap. The steps are as
follows.

Step 3: Right-click the application in the Solution Explorer window and then click the
Add New Item option from the context menu.

Step 4: Select the Site Map Template from the Templates Pane. Note that, by default, the
file has the name web.sitemap.

<siteMapNode url="Default.aspx" title="Home" description="" >

<siteMapNode url="~/Sem1.aspx" title="Sem 1" description="" >

<siteMapNode url="~/Clang.aspx" title="C Language" description="" />

</siteMapNode>

<siteMapNode url="~/Sem2.aspx" title="Sem 2" description="" />

<siteMapNode url="~/Sem3.aspx" title="Sem 3" description="" />

</siteMapNode>

Step 5: Now Add one SiteMapPath control on the Default.aspx page from the navigation tab
of the toolbox.

Step 6: Now Add one SiteMapPath control on the All pages from the navigation tab of the
toolbox.

Output :
18. Write a program to demonstrate use of Master Page and Theme.

Master Page :-

Step 1 : Create a new project using "File" -> "New" -> "Project..." then select web
"ASP.NET Web Forms Application". Name it "MasterPageDemo".

Step 2 : Go to Solution Explorer then right-click on your application then select "Add
New Item" then select "Master Page" and name it “Site.Master”.

Step 3 : Now, go to Solution Explorer then right-click on your application then select
"Add New Item" then select "Web form using Master Page" and name it “Home.aspx”. Next add
click.

Step 4 : Now, select a Master Page; just click "OK" and Edit like below.

<form id="form1" runat="server">

<div>

<div style ="text-align: center">

<asp:Label ID="Label1" runat="server" Text="My Website" Font-Bold="True"


Font-Size="XX-Large" ForeColor="#6600CC"></asp:Label>

</div>

<br />
<asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">

</asp:ContentPlaceHolder>

<br />

<div style ="text-align: center">

<asp:Label ID="Label2" runat="server" Text="&copy 2021 All rights


reserved"></asp:Label>

</div>

</div>

</form>

Step 5 : Now add Web Form with Master Page and Edit.

Output :

Theme :

Step 1 : Go to Solution Explorer then right-click on your application then select "Add
New Item" then select "Skin File and add this.

<asp:Label runat="server" SkinId="lbl" Font-Bold="True" Font-Size="Large"


ForeColor="#FF9933"></asp:Label>
<asp:TextBox runat="server" SkinId="txtBox" BorderColor="#99FF66"></asp:TextBox>

<asp:Button runat="server" SkinId="btn" BackColor="#99FF99" ForeColor="#333333"/>

Step 2 : Add New Web Form and add StylesheetTheme="SkinFile" in <Page> tag.

<asp:Label ID="Label1" runat="server" Text="Your Text" Font-Bold="True" Font-Size="Large"


ForeColor="#FF9933"></asp:Label>

<br />

<asp:TextBox ID="TextBox1" runat="server" BorderColor="#99FF66"></asp:TextBox

<br />

<asp:Button ID="Button1" runat="server" BackColor="#99FF99" ForeColor="#333333"


Text="Button" />

Step 3 : Now to an new Label, Textbox and Button and edit SkinID Properties.

Output :

19. Write a program to display data into GridView control.

<form id="form1" runat="server">

<div>

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"


DataSourceID="SqlDataSource1">

<Columns>
<asp:BoundField DataField="Name" HeaderText="Name"
SortExpression="Name" />

<asp:BoundField DataField="City" HeaderText="City"


SortExpression="City" />

<asp:BoundField DataField="Gender" HeaderText="Gender"


SortExpression="Gender" />

</Columns>

</asp:GridView>

<br />

<br />

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$


ConnectionStrings:ConnectionString %>" SelectCommand="SELECT [Name], [City], [Gender]
FROM [mytable]"></asp:SqlDataSource>

</div>

</form>

20. Write a program for display data into DetailView Control.

<form id="form1" runat="server">

<div>

<asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="192px"


AutoGenerateRows="False" DataSourceID="SqlDataSource1">

<Fields>

<asp:BoundField DataField="Name" HeaderText="Name"


SortExpression="Name" />

<asp:BoundField DataField="City" HeaderText="City"


SortExpression="City" />

<asp:BoundField DataField="Gender" HeaderText="Gender"


SortExpression="Gender" />

</Fields>

</asp:DetailsView>

s <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$


ConnectionStrings:ConnectionString %>" SelectCommand="SELECT [Name], [City], [Gender]
FROM [mytable]"></asp:SqlDataSource>
</div>

</form>

21. Write a program to perform insert and update operation in Database.

Code-Behind :

using System.Data;

using System.Data.SqlClient;

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

SqlConnection conn = new SqlConnection(@"Data


Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated
Security=True");

protected void Page_Load(object sender, EventArgs e)

conn.Open();

protected void Button1_Click(object sender, EventArgs e)

SqlCommand cmd = conn.CreateCommand();

cmd.CommandType = CommandType.Text;

cmd.CommandText = "insert into tbl_user (Name, Email, Contact) values('"+


txtName.Text + "', '"+ txtEmail.Text + "', '"+ txtContact.Text + "')";

cmd.Connection = conn;

cmd.ExecuteNonQuery();

protected void Button2_Click(object sender, EventArgs e)

SqlCommand cmd = conn.CreateCommand();

cmd.CommandType = CommandType.Text;
cmd.CommandText = "UPDATE tbl_user SET Name='" + txtName.Text + "', Email='" +
txtEmail.Text + "', Contact='" + txtContact.Text + "' WHERE Name='"+txtName.Text+"'";

cmd.Connection = conn;

cmd.ExecuteNonQuery();

22. Write a program to perform delete and search operation in Database.

<asp:Button ID="btnDelete" runat="server" OnClick="btnDelete_Click" Text="Delete" />

Code-Behind btnDelete:

protected void btnDelete_Click(object sender, EventArgs e)

SqlCommand cmd = db.CreateCommand();

cmd.CommandType = CommandType.Text;

cmd.CommandText = "DELETE FROM tbl_user WHERE Name='"+txtName.Text+"' ";

cmd.Connection = db;

cmd.ExecuteNonQuery();

23. Write a program to display the records from database using Data Reader Object.

24. Write a program to demonstrate the various methods of Dataset Object.

You might also like