0% found this document useful (0 votes)
20 views10 pages

Demonstrate Dropdown Event Concept

Uploaded by

pubgmobilesd23
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)
20 views10 pages

Demonstrate Dropdown Event Concept

Uploaded by

pubgmobilesd23
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/ 10

1. Design a ASP.

NET program to
demonstrate DropDown event concept.
Using System;

Using System.Web.UI;

Using System.Web.UI.WebControls;

Public partial class _default : System.Web.UI.Page

Protected void Page_Load(object sender, EventArgs e)

If (!IsPostBack)

// Populate dropdown list on initial page load

ddlSelection.Items.Add(new ListItem(“Apple”, “1”));

ddlSelection.Items.Add(new ListItem(“Banana”, “2”));

ddlSelection.Items.Add(new ListItem(“Orange”, “3”));

Protected void ddlSelection_SelectedIndexChanged(object sender, EventArgs e)

// Get the selected item text and value

String selectedText = ddlSelection.SelectedItem.Text;

String selectedValue = ddlSelection.SelectedValue;

// Update label to display the selection

lblSelection.Text = $”You selected: {selectedText} (Value: {selectedValue})”;

}
<!DOCTYPE html>

<html xmlns=http://www.w3.org/1999/xhtml>

<head runat=”server”>

<title>Dropdown Event Example</title>

</head>

<body>

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

<asp:DropDownList ID=”ddlSelection” runat=”server” AutoPostBack=”True”


OnSelectedIndexChanged=”ddlSelection_SelectedIndexChanged”>

</asp:DropDownList>

<br />

<asp:Label ID=”lblSelection” runat=”server”></asp:Label>

</form>

</body>

</html>

2. Write a ASP.NET program to design a


ListBox event.
Using System;
Using System.Collections.Generic;
Using System.Web.UI;
Using System.Web.UI.WebControls;

Public partial class _default : System.Web.UI.Page


{
Protected void Page_Load(object sender, EventArgs e)
{
If (!IsPostBack)
{
// Populate listbox on initial page load
List<string> fruits = new List<string>() { “Apple”, “Banana”, “Orange”,
“Mango” };
Foreach (string fruit in fruits)
{
lbxFruits.Items.Add(new ListItem(fruit));
}
}
}

Protected void lbxFruits_SelectedIndexChanged(object sender, EventArgs e)


{
// Get the selected items
List<string> selectedItems = new List<string>();
Foreach (ListItem item in lbxFruits.SelectedItems)
{
selectedItems.Add(item.Text);
}

// Update label to display selected items


String selectionText = selectedItems.Count > 0 ? string.Join(“, “,
selectedItems) : “No items selected”;
lblSelection.Text = $”Selected Items: {selectionText}”;
}

Protected void btnGetSelectedItems_Click(object sender, EventArgs e)


{
// Get all selected items on button click
List<string> selectedItems = new List<string>();
Foreach (ListItem item in lbxFruits.SelectedItems)
{
selectedItems.Add(item.Text);
}

// Update label to display selected items


String selectionText = selectedItems.Count > 0 ? string.Join(“, “,
selectedItems) : “No items selected”;
lblSelection.Text = $”Selected Items (Button Click): {selectionText}”;
}
}
3.Write a C# program to demonstrate string
Compare() and Concat() function

Using System;
Class Program
{
Static void Main()
{
// String Compare() method
String str1 = “Hello”;
String str2 = “World”;

Int comparisonResult = string.Compare(str1, str2);

If (comparisonResult == 0)
{
Console.WriteLine(“str1 and str2 are equal.”);
}
Else if (comparisonResult < 0)
{
Console.WriteLine(“str1 comes before str2 alphabetically.”);
}
Else
{
Console.WriteLine(“str1 comes after str2 alphabetically.”);
}

// String Concat() method


String concatenatedString = string.Concat(str1, “ “, str2);
Console.WriteLine(“Concatenated string: “ + concatenatedString);
}
}

4.Write a ASP.NET program to demonstrate


the RadioButton concept.
Using System;
Using System.Web.UI;
Using System.Web.UI.WebControls;

Public partial class _default : System.Web.UI.Page


{
Protected void Page_Load(object sender, EventArgs e)
{
// No specific actions required on initial page load for this example.
}

Protected void rbtnSelection_CheckedChanged(object sender, EventArgs e)


{
RadioButton selectedRadioButton = (RadioButton)sender;

// Check if the selected radio button is checked


If (selectedRadioButton.Checked)
{
lblSelection.Text = $”You selected: {selectedRadioButton.Text}”;
}
}
}
<!DOCTYPE html>
<html xmlns=http://www.w3.org/1999/xhtml>
<head runat=”server”>
<title>RadioButton Example</title>
</head>
<body>
<form id=”form1” runat=”server”>
<asp:RadioButtonList ID=”rbtnSelection” runat=”server”>
<asp:ListItem Text=”Option 1” Value=”1”></asp:ListItem>
<asp:ListItem Text=”Option 2” Value=”2”></asp:ListItem>
<asp:ListItem Text=”Option 3” Value=”3”></asp:ListItem>
</asp:RadioButtonList>
<br />
<asp:Label ID=”lblSelection” runat=”server”></asp:Label>
</form>
</body>
</html>
5.Write a C# program to demonstrate string
Copy() and Equals() function

Using System;

Class Program
{
Static void Main()
{
// Using Copy() method
String originalString = “Hello, World!”;
String copiedString = string.Copy(originalString);

Console.WriteLine(“Original String: “ + originalString);


Console.WriteLine(“Copied String: “ + copiedString);

// Using Equals() method


Bool areEqual = string.Equals(originalString, copiedString);
Console.WriteLine(“\nAre both strings equal? “ + areEqual);

// Modify the copied string


copiedString = “Hello, Universe!”;

Console.WriteLine(“\nModified Copied String: “ + copiedString);


Console.WriteLine(“Original String after modification: “ + originalString);

// Check equality again


areEqual = string.Equals(originalString, copiedString);
Console.WriteLine(“\nAre both strings equal after modification? “ +
areEqual);
}
}

OUTPUT
Original String: Hello, World!

Copied String: Hello, World!


Are both strings equal? True

Modified Copied String: Hello, Universe!


Original String after modification: Hello, World!

Are both strings equal after modification? False

6.Write a ASP.NET program to display the


current month Calendar in web form

Here’s an ASP.NET Web Forms application that displays the current month’s
calendar using the `Calendar` control:

### Steps:
1. Create a new ASP.NET Web Forms project in Visual Studio.
2. Add a `WebForm` page (e.g., `Default.aspx`).
3. Use the following code for the design (`.aspx`) and the code-behind
(`.aspx.cs`).

### Default.aspx (Design)

```html
<%@ Page Language=”C#” AutoEventWireup=”true” CodeFile=”Default.aspx.cs”
Inherits=”Default” %>

<!DOCTYPE html>
<html xmlns=http://www.w3.org/1999/xhtml>
<head runat=”server”>
<title>Current Month Calendar</title>
</head>
<body>
<form id=”form1” runat=”server”>
<div style=”text-align: center;”>
<h2>Current Month Calendar</h2>
<asp:Calendar ID=”Calendar1” runat=”server”></asp:Calendar>
</div>
</form>
</body>
</html>
```

### Default.aspx.cs (Code-behind)

```csharp
Using System;

Public partial class Default : System.Web.UI.Page


{
Protected void Page_Load(object sender, EventArgs e)
{
If (!IsPostBack)
{
// Set the Calendar to the current month
Calendar1.VisibleDate = DateTime.Today;
}
}
}
```

### Explanation:
- The **Calendar Control** in ASP.NET is used to display the calendar in a web
form.
- In the `Page_Load` event, the `Calendar1.VisibleDate` property is set to the
current date (`DateTime.Today`) to display the current month.
- `!IsPostBack` ensures that the calendar is set to the current month only on the
initial page load and not on every postback.

7.Write a ASP.NET program to calculate two


numbers and the result display in the web
form.

<%@ Page Language=”C#” AutoEventWireup=”true” CodeFile=”Default.aspx.cs”


Inherits=”Default” %>
<!DOCTYPE html>
<html xmlns=http://www.w3.org/1999/xhtml>
<head runat=”server”>
<title>Simple Calculator</title>
</head>
<body>
<form id=”form1” runat=”server”>
<div style=”text-align: center;”>
<h2>Simple Calculator</h2>
<p>
Enter first number:
<asp:TextBox ID=”txtNumber1” runat=”server”></asp:TextBox>
</p>
<p>
Enter second number:
<asp:TextBox ID=”txtNumber2” runat=”server”></asp:TextBox>
</p>
<p>
<asp:Button ID=”btnCalculate” runat=”server” Text=”Calculate”
OnClick=”btnCalculate_Click” />
</p>
<p>
Result:
<asp:Label ID=”lblResult” runat=”server” Text=”Result will be displayed
here.” />
</p>
</div>
</form>
</body>
</html>
Using System;

Public partial class Default : System.Web.UI.Page


{
Protected void Page_Load(object sender, EventArgs e)
{

Protected void btnCalculate_Click(object sender, EventArgs e)


{
// Retrieve the input numbers
Double number1, number2;

// Check if the input values are valid numbers


If (double.TryParse(txtNumber1.Text, out number1) &&
double.TryParse(txtNumber2.Text, out number2))
{
// Perform the calculation (addition in this case)
Double result = number1 + number2;

// Display the result


lblResult.Text = “The result is: “ + result.ToString();
}
Else
{
// If input is invalid, display an error message
lblResult.Text = “Please enter valid numbers.”;
}
}
}

You might also like