Solved

Bind string array to a drop down list

Posted on 2006-06-13
12
766 Views
Last Modified: 2012-05-05
What is the proper syntax for what I'm trying to do?  LinkUser returns a string array which I'm trying to bind to a dropdown list..

private void PopulateAgencies(){
      // populate dropdown list
      LinkUser me = new LinkUser();
      
      ddlAgencyCode.DataValueField      = me.agencyCodes[0];
      ddlAgencyCode.DataTextField      = me.agencyName[0].ToString().Trim();
      
      ddlAgencyCode.DataSource = me;
      ddlAgencyCode.DataBind();
}
0
Comment
Question by:russomr
  • 4
  • 4
  • 4
12 Comments
 
LVL 4

Expert Comment

by:svy
ID: 16896631
private void PopulateAgencies(){
     ddlAgencyCode.DataSource = new LinkUser().agencyCodes;
     ddlAgencyCode.DataBind();
}
0
 
LVL 20

Accepted Solution

by:
REA_ANDREW earned 250 total points
ID: 16896655
I would do it like this

private void PopulateAgencies(){
for(int i = 0; i< agencyCodes.length;i++)
{
ListItem MyItem = new ListItem();
MyItem.Text = agencyCodes[i].ToString();
MyItem.Value = agencyCodes[i];
ddlAgencyCode.Items.Add(MyItem);
}

}
0
 
LVL 20

Expert Comment

by:REA_ANDREW
ID: 16896662
svy, how would you differentiate between a string value and an int value for the text and value properties? i.e. russomr seems to want an int for the value and a string for the text.

:-)

Andrew
0
 
LVL 20

Expert Comment

by:REA_ANDREW
ID: 16896668
My method enables you to apply each format
0
 
LVL 20

Expert Comment

by:REA_ANDREW
ID: 16896673
sorry I mean each cast not format :-(
0
 
LVL 4

Expert Comment

by:svy
ID: 16897201
REA_ANDREW, ListItem.Value and ListItem.Text are both string type properties. I'm not sure what you're tryint to do with your code there...?

There's no need to specify DataTextField and DataValueField in this case.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:russomr
ID: 16897220
svy,

the code

     ddlAgencyCode.DataSource = new LinkUser().agencyCodes;
     ddlAgencyCode.DataBind();

seems to work, accept how do I specify it so that the ddl value is the agencyCodes and what is displayed in the ddl is the agencyName?  Right now it just displays the agencyCodes.

Thanks.

0
 
LVL 4

Expert Comment

by:svy
ID: 16897675
private IEnumerable GetPairs(agencyCodes, agencyNames)
{
for(int i = 0; i< agencyCodes.Length && i < agencyNames.Length;i++)
yield return new Pair(agencyCodes[i], agencyNames[i]);
}
private void PopulateAgencies(){
     string [] agencyCodes = new LinkUser().agencyCodes;
     string [] agencyNames = new LinkUser().agencyNames;
     ddlAgencyCode.DataTextField = "Second";
     ddlAgencyCode.DataValueField = "First";
     ddlAgencyCode.DataSource = GetPairs(agencyCodes, agencyNames);
     ddlAgencyCode.DataBind();
}
0
 

Author Comment

by:russomr
ID: 16904376
I get this error message:

Compiler Error Message: CS1001: Identifier expected

(line 31 is the bolded line)

Source Error:

Line 29: }
Line 30:
Line 31: private IEnumerable GetPairs(agencyCodes, agencyNames){
Line 32:       for(int i = 0; i< agencyCodes.Length && i < agencyNames.Length;i++)
Line 33:       yield return new Pair(agencyCodes[i], agencyNames[i]);
 
I'm doing in-line coding.  Not sure if that makes a difference.
0
 

Author Comment

by:russomr
ID: 16904482
REA_ANDDREW,

I have used your code and applied it like so:

*********************************************
private void PopulateAgencies(){
      // populate dropdown list
      LinkUser me = new LinkUser();
      
      for(int i = 0; i< me.agencyCodes.length;i++){
            ListItem MyItem = new ListItem();
            MyItem.Text = me.agencyNames[i].ToString();
            MyItem.Value = me.agencyCodes[i].ToString();
            ddlAgencyCode.Items.Add(MyItem);
      }

}
*********************************************

Unfortunately, I get the following error:

*********************************************
Compiler Error Message: CS0117: 'System.Array' does not contain a definition for 'length'

Source Error:

Line 48:       LinkUser me = new LinkUser();
Line 49:       
Line 50:       for(int i = 0; i< me.agencyCodes.length;i++){
Line 51:             ListItem MyItem = new ListItem();
Line 52:             MyItem.Text = me.agencyNames[i].ToString();
*********************************************
 
Any thoughts?
0
 

Author Comment

by:russomr
ID: 16904524
Aaaah.  Capital L would help.  I think I got it going.  Thanks for all your help.
0
 
LVL 4

Expert Comment

by:svy
ID: 16905547
private IEnumerable GetItems(string[] agencyCodes, string[] agencyNames)
      {
            for (int i = 0; i < agencyCodes.Length && i < agencyNames.Length; i++)
                  yield return new ListItem(agencyNames[i], agencyCodes[i]);
      }
      private void PopulateAgencies()
      {
            string [] agencyCodes = new LinkUser().agencyCodes;
            string [] agencyNames = new LinkUser().agencyNames;

            ddlAgencyCode.DataTextField = "Text";
            ddlAgencyCode.DataValueField = "Value";
            ddlAgencyCode.DataSource = GetItems(agencyCodes, agencyNames);
            ddlAgencyCode.DataBind();
      }
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

919 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now