Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Drop Down List - Can this be done?

Status
Not open for further replies.

rb74

Programmer
May 5, 2002
64
US
Hi all,

I am migrating my asp code to asp.net and I am attempting to use an asp.net drop down list for a evaluation form. When I coded in classic ASP, I populated a drop down list via a recordset. I am attempting to do the same with an ASP.net drop down list, but here is my problem. The text that is shown in the list needs to be the combination of two fields from the query e.g. The drop down list item for the instructor needs to have the instructor's first name and last name for each instructor. In classic asp I would do the following -
[tt]
Do Until objRSInstructors.EOF
Response.Write(&quot;<OPTION VALUE=&quot;&quot;&quot; & objRSInstructors(&quot;FacultyID&quot;) & &quot;&quot;&quot;>&quot; & objRSInstructors(&quot;FirstName&quot;) & &quot; &quot; & objRSInstructors(&quot;LastName&quot;) & &quot;</OPTION>&quot; & vbCrLf)
objRSInstructors.MoveNext
Loop
[/tt]

I can't find anything regarding this on the web or the asp.net books I own. Can this be done using an asp.net drop down list?

Tanks,

Rob
 
It looks like you are using an HTML element and a record set. If you want to use an ASP.NET server control, you can use a data reader and then read from that. Something like:
Code:
... set the connection and command objects....
' Set the DataReader
Dim dr As Sqldatareader = cm.executeReader()

' Bind
While dr.Read
  cboFaculty.Items.Add( _
    New ListItem(dr(&quot;FirstName&quot;) & &quot; &quot; & dr(&quot;LastName&quot;), _
                 dr(&quot;FacultyID&quot;)))
End While
I'm going from memory here but this should be close.
 
RichS's example is probably the best way for now, but an alternative method would be to return a column from SQL formatted how you need it, as in
Code:
SELECT 
    FacultyID,
    LastName + ', ' + FirstName AS ListName
FROM
    Table...

then you can bind the ListName as the DataTextField. Sometime you might want to start binding to objects rather than readers or datasets, so I would recommend having your data ready to go by the time you get to the presentation.
Code:
public class FacultyMember{

    private string firstName;
    private string lastName;

    public string FirstName{
        get{return firstName;}
        set{firstName=value;}
    }
    
    public string LastName{
        get{return lastName;}
        set{lastName=value;}
        
    }

    public string ListName{
        
         get{
             return LastName + &quot;, &quot; + FirstName;
            }
    }

}

HTH



David
[pipe]
 
Thanks guys, I really appreciate it. I was following the instructions in one book by binding the DataTextField and the DataValueField. I am currently using RichS' suggestion, but I think in the future, I will just adjust my View to compound the two fields into one.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top