|
 |
asp_databases thread: Dynamic List Box
Message #1 by "Dale Wright" <dwright@c...> on Fri, 10 Jan 2003 11:37:23
|
|
Hey guys,.,,,
I have a list box that is populated by a database table.... The users
selects their choice from the list, clicks on the forms submit button and
the information posted to the same page..... I then have code that picks
up the ID of the record selected and i use this ID to get records using
SQL... ie
"SELECT * FROM bla bla Where Blah Blah = " & id & ""
Ok..... Right this works fine, but is it possible to also get the ID and
the value selected.... For instance, if a user selects FOOTBALL with and
ID of 1.... I want to be able to "grab" the value 1 and the Name
FOOTBALL.... I have tried using hidden fields etc etc, but i cant figure
it out.... i am having a mind block today.... I cant seem to think
straight....
Thanks for your help guys....
Regards
Dale
Here is the Code, just the dynamic list box!!!
Sorry i forgot to post the code that is getting on my nerves (ps this is
just the dynamic list box code.)
Dim oRs1
Set Ors1=Server.CreateObject("ADODB.Recordset")
SQLtxt1 = "SELECT * FROM FAQDepartments"
ors1.Open sqltxt1, "DSN=BTContact", adopenforwardonly
oRs1.MoveFirst
%>
<select name="Department" style="font-family:
Verdana,Arial,Helvetica,sans-
serif; font-size:12px; background-color: rgb(240,240,240); color: rgb
(0,0,0)">
<%
Do While Not oRS1.EOF
Response.Write "<option value=" & ors1("Record_Number") & ">"
oRs1.MoveNext
Loop
ors1.Close
set ors1 = Nothing
ASP beginner.....
Message #2 by "Ron Williams" <ronwilliams32@c...> on Fri, 10 Jan 2003 16:11:15
|
|
ok, you are already populating the List from the database, just one more
step to solve this one.
when you are building the list(i dont know how you are doing it, but i
will assume it is the basic way) add the ID as the value of the option
tag, for example...
strSelect = "<select name='mySelect'><option value='" & objRS("id") & "'>"
& objRS("fieldLikeFootballorWhatever") & "</option>"
iterate thru the recordset results and do that for each record. you can
also put a condition, for example, if you want to know what value they
cose and have that as the default selection. here is some same code from
a page that i use to edit Guest Book records (the spacing and breaks are
all screwed up)...
now when the user selects football, when you perform request.form
("theNameOfTheField"), you will get the ID value, and you can use it for
your query.
set objComm = server.CreateObject("adodb.command")
objComm.ActiveConnection = strConnect
objComm.CommandType = adCmdText
strSQL = "select id, name, date, time from guestbook order by id desc"
objComm.CommandText = strSQL
set objRS = objComm.Execute
set objComm = Nothing
strSelect = "<form method='post' action='guestadmin.asp'><input
type='hidden' name='loginname' value='"& strLoginName & "'></input><select
name='id'>"
do while not objRS.EOF
if strID = objRS("id") then
strSelectedRecord = "selected"
else
strSelectedRecord = ""
end if
strSelect = strSelect & "<option value='" & objRS("id") & "'" &
strSelectedRecord & ">"
strSelect = strSelect & objRS("id") & " : " & objRS("name") & " : " & objRS
("date") & " : "
strSelect = strSelect & objRS("time")
objRS.MoveNext
loop
objRS.close
set objRS = Nothing
strSelect = strSelect & "</select>"
we need better editing for these answers!!!
ron
> Hey guys,.,,,
> I have a list box that is populated by a database table.... The users
> selects their choice from the list, clicks on the forms submit button
and
> the information posted to the same page..... I then have code that
picks
> up the ID of the record selected and i use this ID to get records using
> SQL... ie
>
> "SELECT * FROM bla bla Where Blah Blah = " & id & ""
>
> Ok..... Right this works fine, but is it possible to also get the ID
and
> the value selected.... For instance, if a user selects FOOTBALL with
and
> ID of 1.... I want to be able to "grab" the value 1 and the Name
> FOOTBALL.... I have tried using hidden fields etc etc, but i cant
figure
> it out.... i am having a mind block today.... I cant seem to think
> straight....
>
> Thanks for your help guys....
>
> Regards
> Dale
> Here is the Code, just the dynamic list box!!!
> Sorry i forgot to post the code that is getting on my nerves (ps this is
> just the dynamic list box code.)
>
> Dim oRs1
>
> Set Ors1=Server.CreateObject("ADODB.Recordset")
>
> SQLtxt1 = "SELECT * FROM FAQDepartments"
>
> ors1.Open sqltxt1, "DSN=BTContact", adopenforwardonly
>
> oRs1.MoveFirst
> %>
> <select name="Department" style="font-family:
V> erdana,Arial,Helvetica,sans-
> serif; font-size:12px; background-color: rgb(240,240,240); color: rgb
> (0,0,0)">
>
> <%
>
> Do While Not oRS1.EOF
>
> Response.Write "<option value=" & ors1("Record_Number") & ">"
> oRs1.MoveNext
> Loop
> ors1.Close
> set ors1 = Nothing
>
> ASP beginner.....
Message #3 by "Ron Williams" <ronwilliams32@c...> on Fri, 10 Jan 2003 23:14:58
|
|
oops, i misunderstood your question. so you can get the ID with no
problem, but you want both. the only way i know how, is to have a select
case statement in the code after you retrieve the value that the user
chose...
someVariable = Request.Form("Department")
select case someVariable
case 1
anotherVariable = "Football"
case 2
anotherVariable = "Baseball"
end select
hope i got the question right this time.
ron
> Hey guys,.,,,
> I have a list box that is populated by a database table.... The users
> selects their choice from the list, clicks on the forms submit button
and
> the information posted to the same page..... I then have code that
picks
> up the ID of the record selected and i use this ID to get records using
> SQL... ie
>
> "SELECT * FROM bla bla Where Blah Blah = " & id & ""
>
> Ok..... Right this works fine, but is it possible to also get the ID
and
> the value selected.... For instance, if a user selects FOOTBALL with
and
> ID of 1.... I want to be able to "grab" the value 1 and the Name
> FOOTBALL.... I have tried using hidden fields etc etc, but i cant
figure
> it out.... i am having a mind block today.... I cant seem to think
> straight....
>
> Thanks for your help guys....
>
> Regards
> Dale
> Here is the Code, just the dynamic list box!!!
> Sorry i forgot to post the code that is getting on my nerves (ps this is
> just the dynamic list box code.)
>
> Dim oRs1
>
> Set Ors1=Server.CreateObject("ADODB.Recordset")
>
> SQLtxt1 = "SELECT * FROM FAQDepartments"
>
> ors1.Open sqltxt1, "DSN=BTContact", adopenforwardonly
>
> oRs1.MoveFirst
> %>
> <select name="Department" style="font-family:
V> erdana,Arial,Helvetica,sans-
> serif; font-size:12px; background-color: rgb(240,240,240); color: rgb
> (0,0,0)">
>
> <%
>
> Do While Not oRS1.EOF
>
> Response.Write "<option value=" & ors1("Record_Number") & ">"
> oRs1.MoveNext
> Loop
> ors1.Close
> set ors1 = Nothing
>
> ASP beginner.....
|
|
 |