• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 210
  • Last Modified:

Combox's value field

I have an Ajax combo box. Call the database and add items to it. Then I look at the source code and the value should have Ids in it but it has text in them...

<option selected="selected" value="Select HCprovider">Select HCprovider</option>
      <option value="Bob1 Jones">Bob1 Jones</option>
      <option value="pp333 ppp">pp333 ppp</option>
      <option value="officer 1 llll">officer 1 llll</option>


So, I bind, then remove duplicates from the combo box. I have the code below. Not sure why it ends up with the text ...instead of the value...
--- in aspx
 <asp:ComboBox ID="cbProvider" DataTextField="HCproviders" DataValueField="hcIds" AutoCompleteMode="SuggestAppend" CaseSensitive="false" DropDownStyle="DropDownList"  runat="server">
             </asp:ComboBox>

--- adding items to the combo box

 var results = pd.LoadOfficeLocationHCprovidersByState(UserSession,int.Parse(ddlState.SelectedValue));

        foreach (var p in results)
        {
            if (p.HCproviders.Contains(","))
            {
                String[] providerValues = p.HCproviders.Split(new Char[] { ',' });
                String[] idValues = p.hcids.ToString().Split(new Char[] { ',' });
                foreach (String i in providerValues)
                {
                    foreach (string j in idValues)
                    {
                       // rdProvider.Items.Add(new RadComboBoxItem(i, j));
                        cbProvider.Items.Add(new ListItem(i, j));
                    }

                }
            }
            else
            {
                 cbProvider.Items.Add(new ListItem(p.HCproviders, p.hcids.ToString()));
            }
cbProvider = common.RemoveDuplicates(cbProvider); //remove dups
--- removing duplicates

 public ComboBox RemoveDuplicates(ComboBox comboBox)
        {

            //remove duplicate items from list
            List<String> Removed = new List<String>();
            for (int I = 0; I < comboBox.Items.Count; I++)
            {
                if (!Removed.Contains(comboBox.Items[I].ToString()))
                {
                    Removed.Add(comboBox.Items[I].ToString());
                }
            }
            comboBox.Items.Clear();
            foreach (String S in Removed)
            {
                comboBox.Items.Add(S.Trim());
            }

            return comboBox;
        }

Open in new window

0
Camillia
Asked:
Camillia
  • 2
2 Solutions
 
Easwaran ParamasivamCommented:
 Debug your code. Place the breakpoint at the line 20 and 27 in your code and see what the value is getting stored in the combobox. It should be in the format (id,value).

  I suspect that the id value is not getting captured properly.
0
 
dexterrajeshCommented:
hi,

the ListItem constructor you have used is of format new ListItem(text, value)

in you code below might be j having the text and i having the value

cbProvider.Items.Add(new ListItem(i, j));

if so.. then set them properly...
0
 
CamilliaAuthor Commented:
I put a debug step (below) ...the values of j and i are correct but when added to the combo, the "value" has text in it!

This is what I see:

in loop i -- Bob1 Jones
in loop j -- 1
text--Bob1 Jones
value--Bob1 Jones

I even added this to the code:
cbProvider.DataTextField = "i";
 cbProvider.DataValueField = "j";
foreach (String i in providerValues)
                {
                    foreach (string j in idValues)
                    {
                       // rdProvider.Items.Add(new RadComboBoxItem(i, j));
                        cbProvider.DataTextField = "i";
                        cbProvider.DataValueField = "j";
                        cbProvider.Items.Add(new ListItem(i, j));
                        
                        Response.Write("in loop i -- " + i + "<br/>");
                        Response.Write("in loop j -- " + j + "<br/>");
                        Response.Write("text--" + cbProvider.Items[0].Text + "<br/>");
                        Response.Write("value--" + cbProvider.Items[0].Value + "<br/>");
                    }

                }

Open in new window

0
 
CamilliaAuthor Commented:
this is what i did, seems to be working

 ListItem item = new ListItem();
                        item.Value = j;
                        item.Text = i;
                        cbProvider.Items.Add(item);
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now