 |
| 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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

July 14th, 2005, 08:46 AM
|
|
Authorized User
|
|
Join Date: May 2005
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

July 14th, 2005, 09:41 AM
|
|
Authorized User
|
|
Join Date: May 2005
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

July 14th, 2005, 03:39 PM
|
|
Friend of Wrox
|
|
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
|
|
is EnableViewState set to TRUE on the second drop down?
|
|

July 20th, 2005, 07:02 AM
|
|
Authorized User
|
|
Join Date: May 2005
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

July 20th, 2005, 08:14 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
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
|
|

July 20th, 2005, 08:43 AM
|
|
Authorized User
|
|
Join Date: May 2005
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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>
|
|

July 20th, 2005, 09:50 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
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
|
|

July 20th, 2005, 10:12 AM
|
|
Authorized User
|
|
Join Date: May 2005
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|
 |