Wrox Programmer Forums
|
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
 
Old May 17th, 2005, 09:55 AM
Authorized User
 
Join Date: May 2005
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Default ASP.Net Drop Down List

I want to know how to do the following which I have done in Classic ASP in ASP.Net VB.

I pass a parameter though the URL string and I want for example a drop down list of departments, with the correct department already selected in the drop down menu based on a foreign key of a personID.

<select name="Team_Jump" id="Team_Jump">
                            <%
While (NOT RS_Team.EOF)
%>
                            <option value="Edit_team.asp?seasonID=<%=Request("seasonID ")%>&LeagueID=<%=Request("LeagueID")%>&ClubID=<%=( RS_Team.Fields.Item("ClubID").Value)%>"<% If Request("TeamID")= CStr(RS_Team.Fields.Item("TeamID").Value) Then Response.Write ("Selected") %>><%=(RS_Team.Fields.Item("Team_Name").Value)%>
                            - <%=(RS_Team.Fields.Item("Division_Number").Value)% ></option>
                            <%
  RS_Team.MoveNext()
Wend
If (RS_Team.CursorType > 0) Then
  RS_Team.MoveFirst
Else
  RS_Team.Requery
End If
%>
                          </select>

Here is my ASP.net code that I need to snap to the correct user level value from the database

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

    Dim strSQLforListBox As String = "SELECT User_Level " & _
                           "FROM User_Level"

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

    objConnection.Open()
    ddlEmployees.DataSource = objCommand.ExecuteReader()
    ddlEmployees.DataValueField = "User_Level"
    ddlEmployees.DataBind()
    objConnection.Close()
  End If
End Sub

Any help would be greatly appreciated

 
Old May 17th, 2005, 10:31 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

After you populate your dropdownlist with the databind, you can set the desired item as selected. Try something like this:

Dim objItem As ListItem
objItem = ddlDropDownList.Items.FindByValue("valueFromQueryS tring")
If Not objItem Is Nothing Then
   ddlDropDownList.SelectedIndex = ddlDropDownList.Items.IndexOf(objItem)
Else
   ddlDropDownList.SelectedIndex = -1
End If

Note that if .FindByValue() can't find the item, it will return Nothing/null. If you pass the result directly to .IndexOf(), the call will break so you need to test for the found item first.

There is a FindByText() method as well that finds an item based on the displayed text versus the option value.

-Peter
 
Old May 17th, 2005, 10:50 AM
Authorized User
 
Join Date: May 2005
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Peter,

I have introduced your code into my sub routine, but without any success, but you must bear with me as I have only been working on asp.net for 2 weeks. Concise instructions are needed at this stage of my asp.net training. Thanks for your quick reply.

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

    Dim strSQLforListBox As String = "SELECT User_Level " & _
                           "FROM User_Level"

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

    objConnection.Open()
    ddlEmployees.DataSource = objCommand.ExecuteReader()
    ddlEmployees.DataValueField = "User_Level"
    ddlEmployees.DataBind()

    objConnection.Close()

    Dim objItem As ListItem
        objItem = ddlEmployees.Items.FindByValue(Request("personID") )
    If Not objItem Is Nothing Then
        ddlEmployees.SelectedIndex = ddlEmployees.Items.IndexOf(objItem)
    Else
           ddlEmployees.SelectedIndex = -1
    End If
  End If
End Sub

 
Old May 17th, 2005, 12:40 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

What is happening? Are you getting an error message or is it just not doing what you expect? Are you sure you are using a value that should be found in the list?

-Peter
 
Old May 18th, 2005, 04:23 AM
Authorized User
 
Join Date: May 2005
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The drop down menu has values 1,2,3 in its list, I can change the value and the update works correctly (I know this by checking that the value changes in SQL Server). But when I go back to the back the value is not set, it always defaults to 1.

There is no error message

Richard

 
Old May 18th, 2005, 08:10 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

What you mean when you say "when I go back"? Are you using the back button? If so, you might not see the correct control state because of the way the browser behaves.

-Peter
 
Old May 18th, 2005, 08:48 AM
Authorized User
 
Join Date: May 2005
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Sorry, I mean, when i go to the detail page after clicking a link which passes the PersonID as a parameter the drop down menu does not have the correct value selected, it always defaults to the value 1.

The code that you suggest, have I placed it in the correct position within the sub routine?

Richard

 
Old May 18th, 2005, 09:27 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Yes, it looks good there. As long as you bind and preset the value only once (on the first page load) this should work fine. I assume you are calling LoadDropDown in Page_Load.

You'll have to verify that the value from the querystring is getting in correctly (case shouldn't matter).
Then verify that FindByValue is returning something.
Also verify that the item in your DDL are populating correctly (the field you want to fill the value attribute is correct).

-Peter
 
Old May 18th, 2005, 10:30 AM
Authorized User
 
Join Date: May 2005
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Peter, its sorted now, just needed to match up the primary key UserLevelID with ddlEmployees.Items.FindByValue(Request("UserLevelI D"))

Much appreciated!

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

    Dim strSQLforListBox As String = "SELECT UserLevelID, User_Level_Desc " & _
                           "FROM User_Level"

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

    objConnection.Open()
    ddlEmployees.DataSource = objCommand.ExecuteReader()
    ddlEmployees.DataTextField = "User_Level_Desc"
    ddlEmployees.DataValueField = "UserLevelID"
    ddlEmployees.DataBind()



    objConnection.Close()

    Dim objItem As ListItem
        objItem = ddlEmployees.Items.FindByValue(Request("UserLevelI D"))
    If Not objItem Is Nothing Then
        ddlEmployees.SelectedIndex = ddlEmployees.Items.IndexOf(objItem)
    Else
           ddlEmployees.SelectedIndex = -1
    End If
  End If
End Sub






Similar Threads
Thread Thread Starter Forum Replies Last Post
How to cast ASP Drop Down List to HtmlSelect? bekim ASP.NET 1.0 and 1.1 Basics 3 February 13th, 2006 11:04 PM
drop down list values based on another drop down noor ASP.NET 1.0 and 1.1 Basics 3 July 5th, 2005 09:57 AM
.asp.net dynamic drop down menu fitchic77 .NET Framework 2.0 2 August 12th, 2004 07:39 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.