Binding Asp.net drop down list using collection

Hi,
ive a drop down list and i have to populate using a collection class

if(oCollection.Count > 0)
                                    {
                                          foreach(Agent oAgent in oCollection)
                                          {
                                                    oAgent.AgentCode=/*how bind it to a ddl*/
                                          
                                          }

as u can see im iterating using a foreach loop and getting all information in oAgent,now i got a ddl to which i have to bind so that i get all the codes on there in ddl.Thank u for ur time  waiting for reply asap
                                    }
sadat_shaikAsked:
Who is Participating?
 
TornadoVCommented:
if(oCollection.Count > 0)
{
          foreach(Agent oAgent in oCollection)
          {
                ListItem li = new ListItem();
                li.Text = oAgent.Any_Text_Field_Goes_Here_That_Will_Be_Displayed_In_Your_DropDownList;
                li.Value = oAgent.AgentCode;
                ddl.Items.Add(li);
          }
}
0
 
sadat_shaikAuthor Commented:
Agent code is the item that i have to display in my ddl thats why im using
oAgent.AgentCode,

now how do i bind this oAgent.AgentCode in my ddl.
0
 
TornadoVCommented:
Is there some kind of an ID field in your oAgent object?  If there is then you simply say the following:

li.Text = oAgent.AgentCode;
li.Value = oAgent.ID or any other field that is defined as your agent's identity, like agentID for example.
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
sadat_shaikAuthor Commented:
Hey Tornado,
i  did this ,is this correct doi have to give a data source too?cuz im iterating thru for each loop  and getting agent code and putting in
AgentList.Value then adding items to the ddl, is this correct ?
if(oCollection.Count > 0)
  {
                                          foreach(Agent oAgent in oCollection)
                                          {
                                                ListItem AgentList = new ListItem();
                                                AgentList.Value = oAgent.AgentCode;
                                                ddlAgentCode.Items.Add(AgentList);
                                                                                          
                                          }
                                    }
0
 
TornadoVCommented:
You can populate a dropdownlist one of two ways:

1. Bind to a datasource like this:
    ddlAgentCode.datasource = some data source;
    ddlAgentCode.databind();

2. Add one listitem at the time, that's what we've done.

Both are correct, to answer your question you do not have to give a data source.
0
 
sadat_shaikAuthor Commented:
Excellent ,thanks for your help and my answered my next question too,i was actually wondering how will data be populated in ddl but as u said its like we get single values from our foreach loop and we  put it in value and then we add that list item to ddl.
Thanks n be in touch cuz i really need ur help :)
0
 
TornadoVCommented:
Anytime.
0
 
sadat_shaikAuthor Commented:
Hey Tonado,
i got a question for ya,my application is able to compile but not debug due to some issues anyways when i click my page to be viewd in browser the ddl doesnot gets populated is it cuz my project is not able to debug or can there be anypossible pbm with the ddl.cuz i can build my app.
0
 
TornadoVCommented:
Could be a lot of different reasons, one of them is your oCollection does not have any items.  In any case, if you need another solution, please open another question and perhaps post your source code in there.  Thanks!
0
 
sadat_shaikAuthor Commented:
Hey Tornado, can u tell me how do i check if the collection  is populated or not?
0
 
TornadoVCommented:
Check oCollection.count.  

By the way, check 'Question and Answer tips' here: http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/ASP_DOT_NET/help.jsp#hs4 and look for a section :'Asking a number of questions in one question'.

Thanks.
0
 
sadat_shaikAuthor Commented:
hey tornado  i posted another question can u check on that plz Enabling and disabling on buttons
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.