2

I am using c#.

I am trying to select the listbox values from the data coming in querystring. Below is code.

if (!string.IsNullOrEmpty(Security.GetString(Page.Request.Params["VT"])))
        {            
            string[] strCntVisit = Security.GetString(Page.Request.Params["VT"]).Split(',');

            foreach (string i in strCntVisit)
            {
                lstContriesVisited1.SelectedValue = i;

            }                
        }

my problem is that the above code is traversing perfectly, however the issue is that in my listbox my last value of (i) is selected not all the values in querystring.

Please suggest.

3
  • Are you saying that you want all values selected from the listbox? Commented Nov 22, 2010 at 10:50
  • what u reauired? all values or one one value you selected.Pls specify Commented Nov 22, 2010 at 10:52
  • All the values that exists in Page.Request.Params["VT"] to be selected in my first list box Commented Nov 22, 2010 at 10:54

3 Answers 3

3

If your ListBox has its SelectionMode property set to Multiple, you can do:

string vtParam = Security.GetString(Page.Request.Params["VT"]);
if (!String.IsNullOrEmpty(vtParam)) {            
    string[] strCntVisit = vtParam.Split(',');
    foreach (string i in strCntVisit) {
        ListItem item = lstContriesVisited1.Items.FindByValue(i);
        if (item != null) {
            item.Selected = true;
        }
    }                
}

EDIT: The following should answer your comment:

foreach(ListItem item in lstContriesVisited1.Items) {
    lstContriesVisited2.Items.Add(item);
}
lstContriesVisited1.Items.Clear();
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Hamidi, one more question, now little change in question, what I want now that I will remove all the items from my first lstContriesVisited1 and will add them to lstContriesVisited2 listbox, please what changes I need to do for this.
1

SelectedValue is a single item only. Assuming you're using an ASP.NET ListControl, try setting the Selected property on items that match your criteria.

List<string> validItems = new List<string>(strCntVisit);
foreach (ListItem i in lstContriesVisited1.Items) {
    if (validItems.Contains(i.Text)) {
        i.Selected = true;
    }
}

Comments

0
string vt = Security.GetString(Page.Request.Params["VT"]);
if (!string.IsNullOrEmpty(vt))
{
    string[] valuesToSelect = vt.Split(',');
    foreach (ListItem li in lstContriesVisited1.Items)
    {
        li.Selected = Array.Contains(valuesToSelect, li.Value);
    }
}

(If the VT parameter could potentially contain a lot of values then it might be quicker to change valuesToSelect from an array to a HashSet<T>.)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.