Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Binding Asp.net drop down list using collection

Posted on 2006-05-18
12
Medium Priority
?
503 Views
Last Modified: 2010-05-18
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
                                    }
0
Comment
Question by:sadat_shaik
  • 6
  • 6
12 Comments
 
LVL 11

Accepted Solution

by:
TornadoV earned 2000 total points
ID: 16709450
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
 

Author Comment

by:sadat_shaik
ID: 16709699
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
 
LVL 11

Expert Comment

by:TornadoV
ID: 16709880
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
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.

 

Author Comment

by:sadat_shaik
ID: 16710438
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
 
LVL 11

Expert Comment

by:TornadoV
ID: 16710490
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
 

Author Comment

by:sadat_shaik
ID: 16710548
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
 
LVL 11

Expert Comment

by:TornadoV
ID: 16710573
Anytime.
0
 

Author Comment

by:sadat_shaik
ID: 16712338
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
 
LVL 11

Expert Comment

by:TornadoV
ID: 16712366
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
 

Author Comment

by:sadat_shaik
ID: 16713050
Hey Tornado, can u tell me how do i check if the collection  is populated or not?
0
 
LVL 11

Expert Comment

by:TornadoV
ID: 16714665
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
 

Author Comment

by:sadat_shaik
ID: 16719889
hey tornado  i posted another question can u check on that plz Enabling and disabling on buttons
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question