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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ListItemCollection question..... 2

Status
Not open for further replies.

Sheffield

Programmer
Jun 1, 2001
180
US
Greetings,

PROBLEM: I'm unable to 'automatically' have a DropDownList item 'selected' while adding multiple items directly to the ListItemCollection.

In the example below, I basically need to display the correct department in a DropDownList (as the SelectedItem while also listing all other departments) based on a corresponding text/value from another DropDownList.

Here is my code:
Code:
<%@ Import Namespace=&quot;System.Data.OleDb %>
<script runat=&quot;server&quot;>

Sub Page_Load()

    Dim cn As OleDbConnection
    Dim strSQL As String    
    Dim cmd As OleDbCommand
    Dim dtr As OleDbDataReader
                    
    cn = New OleDbConnection( &quot;PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA Source=c:\data.mdb&quot; )                         
        
    strSQL = &quot;SELECT d_ID, d_Name &quot; & _
             &quot;FROM tblDepartment &quot; & _
             &quot;ORDER BY d_Name&quot;
                
    cmd = New OleDbCommand( strSQL, cn )                
    cn.Open()                                
        
    dtr = cmd.ExecuteReader()

While dtr.Read()
drpd_ID.Items.Add( New ListItem( dtr( &quot;d_Name&quot; ), dtr( &quot;d_ID&quot; ) ) )
End While
Code:
    dtr.Close()                           
    cn.Close()                       
           
End Sub

</script>



<HTML>
<HEAD>
<TITLE></TITLE>
</HEAD>
     
<BODY>
		
<form Runat=&quot;Server&quot;>

<asp:DropDownList id=&quot;drp_Name&quot; Runat=&quot;Server&quot;>
   <asp:ListItem Text=&quot;Curly&quot; Value=&quot;1&quot; />  
   <asp:ListItem Text=&quot;Larry&quot; Selected=&quot;True&quot; Value=&quot;2&quot; />  
   <asp:ListItem Text=&quot;Moe&quot; Value=&quot;3&quot; />  
</asp:DropDownList>

<br>

<asp:DropDownList id=&quot;drpd_ID&quot; Runat=&quot;Server&quot;>
</asp:DropDownList>

</form>
        
</body>
</html>

I was thinking I could simply insert
Code:
Selected=&quot;True&quot;
in the blue section above, but it didn't work.

I greatly appreciate any help you can provide:)
 
C# sample, please convert.
Code:
while (dtr.Read()) 
{
 ListItem li = new ListItem(dtr[&quot;d_Name&quot;].ToString(),dtr[&quot;d_ID&quot;].ToString());
 if(drp_Name.SelectedValue == dtr[&quot;d_ID&quot;].ToString())
 {
   li.Selected = true;
 }
 drpd_ID.Items.Add(li);           
}
 
I would suggest taking advantage of DataBinding, an awesome and powerful feature of ASP.NET (no looping necessary).

drpd_ID.DataTextField = &quot;d_Name&quot;
drpd_ID.DavaValueField = &quot;d_ID&quot;
drpd_ID.DataSource = cmd.ExecuteReader()
drpd_ID.DataBind()
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top