Using Sorted List Object in ASP.NET

Using Sorted List Object in ASP.NET

The SortedList object contains items in key/value pairs. A SortedList object automatically sort the items in alphabetic or numeric order.

Items are added to the SortedList with the Add() method. A SortedList can be sized to its final size with the TrimToSize() method.

The following code creates a SortedList named books and three elements are added:

SortedList books =New SortedList
  books .Add(”b1″,”book1″)
  books .Add(”b2″,”book2″)
  books .Add(”b3″,”book3″)

Data Binding:

A SortedList object may automatically generate the text and values to the following controls:

  • asp:RadioButtonList
  • asp:CheckBoxList
  • asp:DropDownList
  • asp:Listbox

To bind data to a RadioButtonList control, first create a RadioButtonList control (without any asp:ListItem elements) in an .aspx page:

<form runat=”server”>
<asp:RadioButtonList id=”rb” runat=”server”
AutoPostBack=”True” /></form>

Then add the code that builds the list

SortedList books =New SortedList
  books .Add(”b1″,”book1″)
  books .Add(”b2″,”book2″)
  books .Add(”b3″,”book3″)

  rb.DataSource=books
  rb.DataValueField=”Key”
  rb.DataTextField=”Value”
  rb.DataBind()

Then we add a sub routine to be executed when the user clicks on an item in the RadioButtonList control. When a radio button is clicked, a text will appear in a label:

public void displayMessage()

{

lbl1.text=�Your favorite book is: � + rb.SelectedItem.Text

}

 

Leave a Reply

You must be logged in to post a comment.


All material @ copyrighted by chrisranjana.com. If you want to link to this article you are welcome to do so. Unauthorized publication is strictly prohibited. This developer tutorial website contains articles by Php programmers , Software developers, Mysql programmers and asp c# programmers. This website also contains ajax tutorials and advanced mysql sql stored procedures and functions tutorials and sample codes.