System.Web.UI.WebControls Namespace DataList Class
Returns the selected item in a DataList control.
Script |
[ DataListItem variable = ] DataList.SelectedItem |
This property can only be used programmatically; it cannot be set when declaring the control.
The property is read only with no default value.
Use the SelectedItem property to get a DataListItem object that represents the item selected in the DataList control. This object can then be used to access the methods and properties of the selected item.
The following example demonstrates using the SelectedItem property to determine the selected item in a DataList control, display its contents and modify its properties.
void getSelected ( Object src, DataListCommandEventArgs e ) {
myList.SelectedIndex = e.Item.ItemIndex;
populateList ( );
msg.Text = "The item selected is <b>" +
( ( Label ) myList.SelectedItem.FindControl ( "title" ) ).Text + "</b>";
}
void chgBackColor ( Object src, EventArgs e ) {
if ( myList.SelectedItem != null ) {
myList.SelectedItem.BackColor = System.Drawing.Color.Khaki;
}
}
Sub getSelected ( src As Object, e As DataListCommandEventArgs )
myList.SelectedIndex = e.Item.ItemIndex
populateList ( )
msg.Text = "The item selected is <b>" + _
CType ( myList.SelectedItem.FindControl ( "title" ), Label ).Text + "</b>"
End Sub
Sub chgBackColor ( src As Object, e As EventArgs )
If Not ( myList.SelectedItem Is Nothing ) Then
myList.SelectedItem.BackColor = System.Drawing.Color.Khaki
End If
End Sub |
|
C# |
VB |
Show me
DataList Members SelectedIndex SelectedIndexChanged SelectedItemTemplate SelectedItemStyle Allowing Users to Select Items in a DataList Control