Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.1
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
ASP.NET 1.1 As of 10/6/2005, this forum is locked as part of the reorganization described here: http://p2p.wrox.com/topic.asp?TOPIC_ID=35394. No posts have been deleted. Open ongoing discussions from the last week have been moved to either ASP.NET 1.0 and 1.1 Beginners http://p2p.wrox.com/asp-net-1-0-1-1-basics-60/ or ASP.NET 1.0 and 1.1 Professional. http://p2p.wrox.com/forum.asp?FORUM_ID=50. See my sticky post inside for more.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.1 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Display Modes
  #1 (permalink)  
Old July 14th, 2005, 08:46 AM
Authorized User
 
Join Date: May 2005
Location: Stafford, , United Kingdom.
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Default ASP.Net Drop Down Menu Problem

I have 2 drop down menus, I select a value from the 1st menu and the 2nd drop down menu correctly snaps to the correct values based on the foreign key of the 1st drop down menu. I then select a value from the 2nd drop down but the value does not stay selected.

<asp:DropDownList id="ddlCategory" runat="server" AutoPostBack="True" />

Sub LoadDropDown()

    Dim strConnection As String = MM_Sherry_String
    Dim strSQLforListBox As String

    If NOT IsPostBack Then

    Dim objConnection2 As New SqlConnection(MM_Sherry_String)
      Dim strSQL As String = "SELECT * FROM Category ORDER BY CatName DESC"

      Dim objAdapter As New SqlDataAdapter(strSQL, objConnection2)

      Dim objDataSet As New DataSet()
      objAdapter.Fill(objDataSet, "Category")

     Dim strResultsHolder As String

      Dim r As DataRow

      For Each r In objDataSet.Tables("Category").Rows

    strSQLforListBox = "SELECT SubCat_Name, SubCatID " & _
                           "FROM Sub_Category WHERE SubCat_CatID = " & r("CatID") & " ORDER BY SubCat_Name"
    Next

    Else

    strSQLforListBox = "SELECT SubCat_Name, SubCatID " & _
                           "FROM Sub_Category WHERE SubCat_CatID = " & Request.Form("ddlCategory2") & " ORDER BY SubCat_Name"

    End If

    Dim objConnection As New SqlConnection(strConnection)
    Dim objCommand As New SqlCommand(strSQLforListBox, objConnection)

    objConnection.Open()
    ddlCategory.DataSource = objCommand.ExecuteReader()
    ddlCategory.DataTextField = "SubCat_Name"
    ddlCategory.DataValueField = "SubCatID"
    ddlCategory.DataBind()
    objConnection.Close()

End Sub

  #2 (permalink)  
Old July 14th, 2005, 09:41 AM
Authorized User
 
Join Date: May 2005
Location: Stafford, , United Kingdom.
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Another way around the problem would be to have two DataTextField's from a view in a single drop down menu? Anyone know how to do that?

Sub LoadDropDown2()
  If Not IsPostBack Then
    Dim strConnection As String = MM_Sherry_String

    Dim strSQLforListBox As String = "SELECT CatName, CatID " & _
                           "FROM Category ORDER BY CatName"

    Dim objConnection As New SqlConnection(strConnection)
    Dim objCommand As New SqlCommand(strSQLforListBox, objConnection)

    objConnection.Open()
    ddlCategory2.DataSource = objCommand.ExecuteReader()
    ddlCategory2.DataTextField = "CatName"
    ddlCategory2.DataValueField = "CatID"
    ddlCategory2.DataBind()
    objConnection.Close()
  End If
End Sub

  #3 (permalink)  
Old July 14th, 2005, 03:39 PM
Friend of Wrox
Points: 4,332, Level: 27
Points: 4,332, Level: 27 Points: 4,332, Level: 27 Points: 4,332, Level: 27
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2003
Location: , NJ, USA.
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

is EnableViewState set to TRUE on the second drop down?

  #4 (permalink)  
Old July 20th, 2005, 07:02 AM
Authorized User
 
Join Date: May 2005
Location: Stafford, , United Kingdom.
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Default

There must be a way to get two database fields in one drop down menu, please help!
---------------------------------------------------------------------

Another way around the problem would be to have two DataTextField's from a view in a single drop down menu? Anyone know how to do that?

Sub LoadDropDown2()
  If Not IsPostBack Then
    Dim strConnection As String = MM_Sherry_String

    Dim strSQLforListBox As String = "SELECT CatName, CatID " & _
                           "FROM Category ORDER BY CatName"

    Dim objConnection As New SqlConnection(strConnection)
    Dim objCommand As New SqlCommand(strSQLforListBox, objConnection)

    objConnection.Open()
    ddlCategory2.DataSource = objCommand.ExecuteReader()
    ddlCategory2.DataTextField = "CatName"
    ddlCategory2.DataValueField = "CatID"
    ddlCategory2.DataBind()
    objConnection.Close()
  End If
End Sub

  #5 (permalink)  
Old July 20th, 2005, 08:14 AM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Richard,

How are you calling the load for the second DDL? I'm not really sure what the code is in the original post because I can't see where it's being called from.

The rule of thumb with dependant DDLs is this:
1. Load first on initial page load.
2. Load second on a change of the first.

If you are calling the load for the second DDL on every page hit then you'll loose its selected value.

-Peter
  #6 (permalink)  
Old July 20th, 2005, 08:43 AM
Authorized User
 
Join Date: May 2005
Location: Stafford, , United Kingdom.
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I'm not going to use 2 DDL's now, but I want to have a DDL with 2 DataTextField's, in Classic ASP the code would look like this, notice Season_Start and Season_End are different database columns:

<select name="Season" id="Season">
                          <%
While (NOT RS_Season.EOF)
%>
                          <option value="online-snooker.asp?leagueid=<%=Request("leagueid")%>&Coun ty=<%=Request("County")%>&SeasonID=<%=RS_Season("S easonID")%>"><%= (RS_Season.Fields.Item("Season_Start").Value) %> - <%= (RS_Season.Fields.Item("Season_End").Value)%></option>
                          <%
  RS_Season.MoveNext()
Wend
If (RS_Season.CursorType > 0) Then
  RS_Season.MoveFirst
Else
  RS_Season.Requery
End If
%>
</select>

  #7 (permalink)  
Old July 20th, 2005, 09:50 AM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

You'll need to do two things:

1. Create a select query that includes the necessary data for display:
    SELECT Field1+' - '+Field2 AS CombinedFields ...
2. On selected index change, save the selected index of the DDL, rebind with new query, re-select the selected item.

-Peter
  #8 (permalink)  
Old July 20th, 2005, 10:12 AM
Authorized User
 
Join Date: May 2005
Location: Stafford, , United Kingdom.
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I hope your on a good salary Peter, thanks again! heres the code if anyone has the same problem.

Sub LoadDropDown()
  If Not IsPostBack Then
    Dim strConnection As String = MM_Sherry_String

    Dim strSQLforListBox As String = "SELECT ProdRangeID, CatName+' '+ProdRange_Name as CombinedFields FROM Cat_ProdRange ORDER BY CatName"

    Dim objConnection As New SqlConnection(strConnection)
    Dim objCommand As New SqlCommand(strSQLforListBox, objConnection)

    objConnection.Open()
    ddlCategory.DataSource = objCommand.ExecuteReader()
    ddlCategory.DataTextField = "CombinedFields"
    ddlCategory.DataValueField = "ProdRangeID"
    ddlCategory.DataBind()
    objConnection.Close()
  End If
End Sub

 


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Asp.Net 2 Horizontal Menu Display Problem ivotive ASP.NET 2.0 Professional 1 December 15th, 2006 03:28 AM
.asp.net dynamic drop down menu fitchic77 .NET Framework 2.0 2 August 12th, 2004 07:39 PM
drop down menu in asp mateenmohd Classic ASP Basics 3 June 7th, 2003 02:43 PM



All times are GMT -4. The time now is 04:43 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.