There are 4 basic list type controls in ASP.Net, and within those 4, there are 2 selectable types of list controls. They’re all based in the System.Web.UI.WebControls Namespace. Here’s a list of them:
Control | Selectable Type |
DropDownList | Single Selectable |
RadioButtonList | Single Selectable |
ListBox | Multiple Selectable |
CheckBoxList | Multiple Selectable |
Obviously, this means that with some controls, you can only choose one item at a time, while with the other controls, you can choose multiple items from its list. Now this isn’t meant to be taken literally, there are subtle differences between all 4 of these controls, plus there are many other properties/methods/events, etc for them than what is mentioned in this tutorial. This is just to point out the basic similarities which are available in all of them – a beginner’s guide to list controls, if you will.
All 4 have much the same properties, while the Multiple Selectable lists have a few more. Each has a value (DataValueVield) and a text (DataTextField) property. The DataValueVield property is not really seen (it’s kept internally – like the primary key of a database for each item), while the DataTextField property is what is seen in the list. The all have a DataSource and DataSourceID property, which means they can be bound to a data source (database, xml file, etc). However, along with defining the data source, the DataTextField and the DataValue Field must be defined. For instance, if the table bound to the list is a Customer table, with CustomerID and CustomerName, you would need to define the DataTextField as using the CustomerName field of the table, along with the CustomerID for the DataValueField. Here’s a general example of how it would be accomplished in the tag itself:
<asp:DropDownList runat="server"
id="DropDownList1"
DataValueField="CustomerID"
DataTextField="CustomerName"
DataSourceID="SqlDataSource1" />
So, now we get down to selecting items from each of the list controls. If you want to ‘get’ the value of the selected item, you would add this to your code:
myListControl.SelectedItem.Value
If you wanted the displayed text of the selected item, you would add this:
myListControl.SelectedItem.Text
For the controls which can have multiple items selected, and you want to get all the selected items, you would need a little code – like this:
VB: Dim mySelectedList as String Dim Item As ListItem For Each Item In myListControl.Items If Item.Selected Then mySelectedList +- Item.Text & "," End If Next
C#: string mySelectedList; ListItem Item; foreach (Item in myListControl.Items) { if (Item.Selected) { (mySelectedList +- Item.Text & ","; } }
Here, the comma at the end just indicates a way of separating each of the items in the selected items. You are free to use whatever you’d like for your own purposes. However, keep in mind, that, when getting a string like this, it will have another comma at the very end, so you will need to adjust for that (remove the last comma) when using the data itself. You can get rid of it like this:
mySelectedList = mySelectedList.Substring(0, (mySelectedList.length-1))
Once again, this tutorial is meant to be just a basic overview of list type controls. With this, you can easily begin using any of the 4 controls listed here.
And, if you’re populating them manually at the time of page load, don’t forget to surround the population code with an if/then/postback block!
All Things DotNet Discussed – Winforms/ASP.Net/SharePoint/WPF
Leave a reply
You must be logged in to post a comment.