Exforsys.com
 

Sponsored Links

ASP.NET Tutorials

 
Home Tutorials ASP.NET
 

ASP.NET Web Forms Controls

 

ASP.NET List Controls

Page 4 of 4

List controls provide the user with a number of options to choose from. A user has a choice to select a single option or list of options. The ListBox control provided by Asp.net controls collection. The listbox control has a collection property which is known as items. You can click on the property and populate the ListBox with the data and the value you desire.


While populating the ListBox remember that the Text property of the ListBox is the text which will appear inside the it and the value is simple value. Usually we set the value to an integer which maps to some column in the database. Lets see how we can select a simple item in the ListBox control.


Sample Code
  1. private void ListBox2_SelectedIndexChanged(object sender, System.EventArgs e)
  2.  {
  3.  string selectedItemTextFromListBox = ListBox1.SelectedItem; // Returns the text string
  4.  selectedItemValueFromListBox = ListBox1.SelectedValue; // returns the value
  5.  }
Copyright exforsys.com


The listbox exposes the property of SelectedItem and SelectedValue which can be used to find the selected item or selected value. You can also set the SelectionMode of the ListBox to multiple which allows the user to select multiple items with the ctrl key being pressed while selecting. Here is a small piece of code you can use to find out all the selected items in the listbox.


Sample Code
  1. private void ListBox2_SelectedIndexChanged(object sender, System.EventArgs e)
  2.  {
  3.   foreach(ListItem l in ListBox1.Items)
  4.   {
  5.   if(l.Selected)
  6.   {
  7.   selectedItems += l.Text;
  8.   selectedValue += l.Value;
  9.   }
  10.   }
  11.   }
Copyright exforsys.com


The foreach loop will iterate the collection and if it finds the selected item it will assign to the string.



In the next tutorial we will see more advanced controls. 
 


public eventClick Occurs when the Button control is clicked.
public eventCommand Occurs when the Button control is clicked.
public eventDataBinding (inherited from Control) Occurs when the server control binds to a data source.
public eventDisposed (inherited from Control) Occurs when a server control is released from memory, which is the last stage of the server control lifecycle when an ASP.NET page is requested.
public eventInit (inherited from Control) Occurs when the server control is initialized, which is the first step in the its lifecycle.
public eventLoad (inherited from Control) Occurs when the server control is loaded into the Page object.
public eventPreRender (inherited from Control) Occurs when the server control is about to render to its containing Page object.
public eventUnload (inherited from Control) Occurs when the server control is unloaded from memory.

Changes Text in the TextBox when the Button is clicked:

Lets make a small application that will change the text in the TextBox when the button is clicked.

Sample Code
  1. private void Button2_Click(object sender, System.EventArgs e)
  2. { TextBox1.Text = "This is my new Text";
  3. }
Copyright exforsys.com


As you see its very simple to raise and capture, capture and take action on the event.

First Page: ASP.NET Web Forms Controls


Read Next: ASP .NET: Validating User Input with C#



 

 

Comments


wasiur_rahman said:

  Excellent article on Working with Web Forms Controls and C#. I really benefitted with the contents and was amazed to see this kind of extensive explanation on the topic which is quite rare on Net.......

Wasiur Rahman
April 22, 2005, 3:24 am

Ramaa said:

  good explanation ...thanks
November 3, 2006, 1:01 pm

nandamramu said:

  iam new learner of this topic.in
my view it is in very useful for selflearning students.
the author's way of illustration is appreciable.thanks for your efforts and helping.
November 9, 2006, 1:44 am

kavan said:

  I express thank you !
December 9, 2006, 5:19 am

Ganesh G said:

  :) Excellent article on Working with Web Forms Controls and C#.
February 28, 2007, 12:30 am

sksamantaray said:

  i m having 2 forms in c#.
Eachone is having a textbox control.
i want to retreive data of textbox of form1 to textbox of form2.
How do i do that,Can anyone help me?
April 30, 2007, 11:56 am

just a test article said:

  you can also take a look at this one
good article
May 9, 2007, 5:49 am

chithuraj said:

  very good easy to learn
May 13, 2007, 10:17 am

.netdeveloper said:

  very effective & usefull
June 7, 2007, 4:17 am

sherin raj said:

  Its really very useful for me to gain a lot of knowledge
December 19, 2007, 2:15 am

Samiullah said:

  Its really xlent for .net novice people...
April 3, 2008, 5:42 am

preethy mohan said:

  thank you for your valid information
December 4, 2008, 12:17 pm

Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    

Sponsored Links

 

Subscribe via RSS


Get Daily Updates via Subscribe to Exforsys Free Training via email


Get Latest Free Training Updates delivered directly to your Inbox...

Enter your email address:


 

Subscribe to Exforsys Free Training via RSS
 

 
Partners -  Privacy and Legal Policy -  Site News -  Contact   Sitemap  

Copyright © 2000 - 2009 exforsys.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape