The listbox control
The listbox control represents a vertical sequence of items displayed in a scrollable window. As shown in the following code, listbox allows single or multiple item selection and exposes its contents:
<asp:listbox runat=”server” id=”lstExample” rows=”10” selectionmode=”multiple”/>
You can determine the height of the control by using the rows property.
When it comes to data binding, the listbox control behaves like the other controls, it supports properties such as datasource, datamember, datatextfield, datavaluefield and datatextformatstring and it can be bound to a data source and show its contents, as follows:
lstStudent.datasource = ds.tables[“studenttable”];
lstStudent.datatextfield = “firstname”;
lstStudent.datavaluefield = “studentid”;
lstStudent.databind();
The next code illustrates how to write a comma-separated string with the values of the selected items.
public void showselecteditems(object sender, eventargs e)
{
stringbuilder sb = new stringbuilder();
for (int i=0; i<lstStudent.items.count; i++)
{
if (lstStudent.Items[i].selected)
{
sb.append(lstStudent.items[i].text);
sb.append(“, “);
}
}
lblresult.text = sb.tostring();
}
No hay comentarios:
Publicar un comentario