Programmatically selecting multiple items in a ListBox Control

I’m working on a project in which there was an IndexOutOfBound exception coming. We needed to have multiple items selected in the ListBox. It had a table, TemplateMTemplateAttribute , in which there were multiple entries of the template attributes selected. It was actually the TemplateAttributeId that was to be matched with those in the ‘Value’ property of the ListBox, lbProductAttributes. So I wrote this code

TemplateMTemplateAttribute oPA = null;

oPA = new TemplateMTemplateAttribute();

int index = 0;
DataTable dt = oPA.Query.LoadDataTable();
if (dt != null)
foreach (DataRow dr in dt.Rows)
index = 0;
foreach (ListItem li in lbProductAttributes.Items)
if (li.Value.Equals(dr[“TemplateAttributeId”].ToString()))
lbProductAttributes.Items[index].Selected = true;



Author: Imran Akram

A .NET/SharePoint Consultant and a political enthusiast who believes in putting the feet of the elected and/or unelected officials to fire in order to get good governance.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s