Taking values from user and inserting to a listbox and displaying it
Here in the above diagram we can see
There are Three labels used Enter the roll number- Label1 Enter the name-Label2 lblmsg- Label3- this label is used during runtime for displaying result DropDownList1- vlaues taken from user is inserted to in this listbox And lastly and most importantly the buttons named Button1(“ADD”)-> used to add the values taken from the user in to the list box. Button2 (“COUNT”)->used to count how many items are there in the listbox Button3(“Delete”)-> used to delete the selected item from dropdownlist box Button4(“Show”)->used to display the selected item from the dropdownlist box All the program source codes are written under these buttons.The colour of all the buttons ,font size,type and all other design can be done using the properties window(discussed earlier). Program Source Code:using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class Default2 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { //loop for entire dropdownlist for (int i = 0; i < DropDownList1.Items.Count; i++) { // get the value of each index item string itemtext = DropDownList1.Items[i].Text; //split the itemtext value by "-" String[] itemtext1 = itemtext.Split('-'); //trim() removes all the extra spaces if (itemtext1[0].Equals(TextBox1.Text.Trim())) { lblmsg.Text = "Roll No "+itemtext1[0] + " Already exists"; return; } } //to add new item in dropdownlistbox DropDownList1.Items.Add(TextBox1.Text +"-"+ TextBox2.Text); itemCount(); } protected void Button2_Click(object sender, EventArgs e) { //call the function itemCount() itemCount(); } private void itemCount() { //to count the no of items in the dropdownlistbox int c = DropDownList1.Items.Count;//returns integer lblmsg.Text = "Total Items in Dropdown List Box:" + c.ToString(); } protected void Button3_Click(object sender, EventArgs e) { //delete the selected item from the listbox int i = DropDownList1.SelectedIndex; // returns the selected item index no //if no item is selected returns -1 if (i >= 0) { DropDownList1.Items.RemoveAt(i); // Removes the items at the given index no } // refresh the item count itemCount(); } protected void Button4_Click(object sender, EventArgs e) { lblmsg.Text = DropDownList1.SelectedItem.Text; } } OutPut |
|