Problem with dependant drop down

Discussion in 'ASP General' started by PT, Oct 14, 2004.

  1. PT

    PT Guest

    I got a form with many text boxes, checkboxes and 3 drop downs. From
    that 3, 2 are dependant. I can choose one drop down, and the next drop
    down should display the dependant values of the first drop down
    chosed. And I have a submite button to submit all these values to DB.

    First problem: I cant keep the value of the selected text in the first
    drop down. It always goes back to the first value. (but the asp site
    extension changes accordingly).

    Second problem: when the value of second drop down is updated, then
    the text I inserted in those text boxes, and the checkboxes i checked
    disappears!

    I know its because the site is submitted again to update the second
    drop down. But how could I keep the values of other elements even
    though the drop down is updated?

    Thank you. I'll post the code too, if anyone wants to look at it. (but
    its a big one!!)
     
    PT, Oct 14, 2004
    #1
    1. Advertising

  2. I thought you better provides the source to us then we can know it in depth
     
    Utada P.W. SIU, Oct 14, 2004
    #2
    1. Advertising

  3. PT wrote:
    > I got a form with many text boxes, checkboxes and 3 drop downs. From
    > that 3, 2 are dependant. I can choose one drop down, and the next drop
    > down should display the dependant values of the first drop down
    > chosed. And I have a submite button to submit all these values to DB.
    >
    > First problem: I cant keep the value of the selected text in the first
    > drop down. It always goes back to the first value. (but the asp site
    > extension changes accordingly).
    >
    > Second problem: when the value of second drop down is updated, then
    > the text I inserted in those text boxes, and the checkboxes i checked
    > disappears!
    >
    > I know its because the site is submitted again to update the second
    > drop down. But how could I keep the values of other elements even
    > though the drop down is updated?
    >
    > Thank you. I'll post the code too, if anyone wants to look at it. (but
    > its a big one!!)



    When you post the code, snip out everything that does not help us see the
    problem.

    My advice in cases like this is to start small: don't try to implement your
    desired behavior in the huge page tat you are actually going to be using.
    Create a small test page with only the stuff needed to recreate the problem
    and play with that one until you figure out how to make it happen.


    Here is an example of maintaining a control's state after a submit:

    <html><body><form method="post">
    <input name="txtData" value="<%=request.form("txtData")%>">
    <input type="submit">
    </form></body></html>

    Bob Barrows
    --
    Microsoft MVP - ASP/ASP.NET
    Please reply to the newsgroup. This email account is my spam trap so I
    don't check it very often. If you must reply off-line, then remove the
    "NO SPAM"
     
    Bob Barrows [MVP], Oct 14, 2004
    #3
  4. Like has been mentioned you need to get the submitted form values and put
    them back into the appropriate control.

    If it is a text box then what bob told you is the code to use.

    If it is a select then use

    <option value="<%=RS("Value")%>"<% If RS("value") = request.form("Select Box
    Value") then response.write " selected"%>><%=RS("Value")%></option>

    The check box would be <input type="checkbox" name="chkbox" <% if
    request.form("chkbox") <> "" then response.write " checked" %>>

    that should keep you going

    "PT" <> wrote in message
    news:...
    > I got a form with many text boxes, checkboxes and 3 drop downs. From
    > that 3, 2 are dependant. I can choose one drop down, and the next drop
    > down should display the dependant values of the first drop down
    > chosed. And I have a submite button to submit all these values to DB.
    >
    > First problem: I cant keep the value of the selected text in the first
    > drop down. It always goes back to the first value. (but the asp site
    > extension changes accordingly).
    >
    > Second problem: when the value of second drop down is updated, then
    > the text I inserted in those text boxes, and the checkboxes i checked
    > disappears!
    >
    > I know its because the site is submitted again to update the second
    > drop down. But how could I keep the values of other elements even
    > though the drop down is updated?
    >
    > Thank you. I'll post the code too, if anyone wants to look at it. (but
    > its a big one!!)
     
    Steven Scaife, Oct 14, 2004
    #4
  5. PT

    TomB Guest

    Personally I prefer to use a variable to keep things a little tidier (IMO)
    and limit the calls to Request.Form

    <%
    sPostedValue=trim(Request.Form("Select Box Value"))
    Do While not RS.EOF
    sSelected=""
    sValue=RS.Fields("Value")
    if sPostedValue=sValue then sSelected=" selected"
    %>
    <option value="<%=sValue%>"<%=sSelected%>><%=sValue%></option>
    <%
    RS.MoveNext
    Loop
    %>


    "Steven Scaife" <> wrote in message
    news:%...
    > Like has been mentioned you need to get the submitted form values and put
    > them back into the appropriate control.
    >
    > If it is a text box then what bob told you is the code to use.
    >
    > If it is a select then use
    >
    > <option value="<%=RS("Value")%>"<% If RS("value") = request.form("Select

    Box
    > Value") then response.write " selected"%>><%=RS("Value")%></option>
    >
    > The check box would be <input type="checkbox" name="chkbox" <% if
    > request.form("chkbox") <> "" then response.write " checked" %>>
    >
    > that should keep you going
    >
    > "PT" <> wrote in message
    > news:...
    > > I got a form with many text boxes, checkboxes and 3 drop downs. From
    > > that 3, 2 are dependant. I can choose one drop down, and the next drop
    > > down should display the dependant values of the first drop down
    > > chosed. And I have a submite button to submit all these values to DB.
    > >
    > > First problem: I cant keep the value of the selected text in the first
    > > drop down. It always goes back to the first value. (but the asp site
    > > extension changes accordingly).
    > >
    > > Second problem: when the value of second drop down is updated, then
    > > the text I inserted in those text boxes, and the checkboxes i checked
    > > disappears!
    > >
    > > I know its because the site is submitted again to update the second
    > > drop down. But how could I keep the values of other elements even
    > > though the drop down is updated?
    > >
    > > Thank you. I'll post the code too, if anyone wants to look at it. (but
    > > its a big one!!)

    >
    >
     
    TomB, Oct 14, 2004
    #5
  6. PT

    Larry Bud Guest

    (PT) wrote in message news:<>...
    > I got a form with many text boxes, checkboxes and 3 drop downs. From
    > that 3, 2 are dependant. I can choose one drop down, and the next drop
    > down should display the dependant values of the first drop down
    > chosed. And I have a submite button to submit all these values to DB.
    >
    > First problem: I cant keep the value of the selected text in the first
    > drop down. It always goes back to the first value. (but the asp site
    > extension changes accordingly).


    You need to retrieve the value of the drop down, and put a "SELECTED"
    tag next to the item which is currently active. I.e., let's say you
    have a drop down with values 1, 2, and 3. 2 is chosen. The ASP would
    say something like:

    <select name="period">
    <option value="1" <%if period="1" then
    response.write("selected")%>>One</option>
    <option value="2" <%if period="2" then
    response.write("selected")%>>Two</option>
    <option value="3" <%if period="3" then
    response.write("selected")%>>Three</option>
    </select>





    > Second problem: when the value of second drop down is updated, then
    > the text I inserted in those text boxes, and the checkboxes i checked
    > disappears!
    >
    > I know its because the site is submitted again to update the second
    > drop down. But how could I keep the values of other elements even
    > though the drop down is updated?


    You have to retrieve the values of EVERY element on your page, and set
    the initial value of those text boxes, check boxes, etc, to those
    values retrieve when the page is refreshed.
     
    Larry Bud, Oct 14, 2004
    #6
  7. PT

    PT Guest

    Hello all, Thanx for the inputs. But I think I should give u this
    code, and the picture will be much more clear. First of all I created
    this site in Dreamweaver and I deleted so many lines which I thought
    wont do any good to solve the things I mentioned.

    When I change the drop-down 'cboAbteilungID', it should filter the
    drop-down 'cboArbeitsplatzID'. I tried to keep the selected value in
    'cboAbteilungID', but its not working!

    ---------------------------------------------------------------------------------------

    <%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
    <%
    ' *** Edit Operations: declare variables
    ' *** DELETED variables declared by Dreamweaver

    ' Get value from select box
    iAbteilungID = Trim(Request.QueryString("cboAbteilungID"))

    MM_editAction = CStr(Request.ServerVariables("SCRIPT_NAME"))
    If (Request.QueryString <> "") Then
    MM_editAction = MM_editAction & "?" & Request.QueryString
    End If

    ' boolean to abort record edit
    MM_abortEdit = false

    ' query string to execute
    MM_editQuery = ""
    %>
    <%
    ' *** Insert Record: set variables
    ' ******* DELETED *******
    %>
    <%
    Dim rsPersonSoftware
    Dim rsPersonSoftware_numRows

    Set rsPersonSoftware = Server.CreateObject("ADODB.Recordset")
    rsPersonSoftware.ActiveConnection = MM_userverwaltung_STRING
    rsPersonSoftware.Source = "SELECT tblPersonID, tblSoftwareID FROM
    dbo.tblPersonSoftware"
    rsPersonSoftware.CursorType = 2
    rsPersonSoftware.CursorLocation = 2
    rsPersonSoftware.LockType = 3
    rsPersonSoftware.Open()

    rsPersonSoftware_numRows = 0
    %>

    <%
    ' *** Insert Record: construct a sql insert statement and execute it

    Dim MM_tableValues
    Dim MM_dbValues

    If (CStr(Request("MM_insert")) <> "") Then

    ' create the sql insert statement
    ' **** DELETED *****
    MM_editQuery = "SET NOCOUNT ON; insert into " & MM_editTable & " ("
    & MM_tableValues & ") values (" & MM_dbValues & ");"&_
    "SELECT SCOPE_IDENTITY() AS LastPersonID;"

    If (Not MM_abortEdit) Then
    ' execute the insert
    Dim iLastPersonID
    Set MM_editCmd = Server.CreateObject("ADODB.Command")
    MM_editCmd.ActiveConnection = MM_editConnection
    MM_editCmd.CommandText = MM_editQuery
    'Get the last PersonID added, and insert datas
    MM_editCmd.CommandType = 1 'adCmdText
    Set rsPID = MM_editCmd.Execute
    iLastPersonID = rsPID.Fields("LastPersonID").Value
    ' insert checked checkboxes values to BD
    For i = 1 to 100 ' need to put in an upperbound that corresponds to
    the total recordset count
    If Request.Form("chkSoftware" & i) Then
    'strSql1 = "INSERT into tblPersonSoftware (tblPersonID,
    tblSoftwareID) values (" & iLastPersonID & ", " & i & ")"
    rsPersonSoftware.Addnew
    rsPersonSoftware("tblSoftwareID") = i
    rsPersonSoftware("tblPersonID") = iLastPersonID
    rsPersonSoftware.Update
    End If
    Next

    MM_editCmd.ActiveConnection.Close
    Set MM_editCmd = Nothing

    If (MM_editRedirectUrl <> "") Then
    Response.Redirect(MM_editRedirectUrl)
    End If
    End If

    End If
    %>
    <%
    Dim PersonStatus
    Dim PersonStatus_numRows

    Set PersonStatus = Server.CreateObject("ADODB.Recordset")
    PersonStatus.ActiveConnection = MM_userverwaltung_STRING
    PersonStatus.Source = "SELECT StatusID, Status FROM dbo.tblStatus
    ORDER BY Status ASC"
    PersonStatus.CursorType = 0
    PersonStatus.CursorLocation = 2
    PersonStatus.LockType = 1
    PersonStatus.Open()

    PersonStatus_numRows = 0
    %>
    <%
    Dim Arbeitsplatz
    Dim Arbeitsplatz_numRows

    Set Arbeitsplatz = Server.CreateObject("ADODB.Recordset")
    Arbeitsplatz.ActiveConnection = MM_userverwaltung_STRING
    Arbeitsplatz.Source = "SELECT ArbeitsplaetzeID, PCNummer FROM
    dbo.tblArbeitsplatz WHERE tblAbteilungID='" & iAbteilungID & "'
    ORDER BY PCNummer ASC"
    Arbeitsplatz.CursorType = 0
    Arbeitsplatz.CursorLocation = 2
    Arbeitsplatz.LockType = 1
    Arbeitsplatz.Open()

    Arbeitsplatz_numRows = 0
    %>
    <%
    Dim Abteilungen
    Dim Abteilungen_numRows

    Set Abteilungen = Server.CreateObject("ADODB.Recordset")
    Abteilungen.ActiveConnection = MM_userverwaltung_STRING
    Abteilungen.Source = "SELECT AbteilungID, Abteilung, Standort,
    tblStandortID FROM dbo.Abteilungen ORDER BY Abteilung ASC, Standort
    ASC "
    Abteilungen.CursorType = 0
    Abteilungen.CursorLocation = 2
    Abteilungen.LockType = 1
    Abteilungen.Open()

    Abteilungen_numRows = 0
    %>
    <%
    Dim rsSoftware
    Dim rsSoftware_numRows

    Set rsSoftware = Server.CreateObject("ADODB.Recordset")
    rsSoftware.ActiveConnection = MM_userverwaltung_STRING
    rsSoftware.Source = "SELECT SoftwareID, SoftwareName FROM
    dbo.tblSoftware ORDER BY SoftwareName ASC"
    rsSoftware.CursorType = 0
    rsSoftware.CursorLocation = 2
    rsSoftware.LockType = 1
    rsSoftware.Open()

    rsSoftware_numRows = 0
    %>
    <%
    Dim rsPerson
    Dim rsPerson_numRows

    Set rsPerson = Server.CreateObject("ADODB.Recordset")
    rsPerson.ActiveConnection = MM_userverwaltung_STRING
    rsPerson.Source = "SELECT PersonID FROM dbo.tblPersonen"
    rsPerson.CursorType = 0
    rsPerson.CursorLocation = 2
    rsPerson.LockType = 1
    rsPerson.Open()

    rsPerson_numRows = 0
    %>
    <%
    Dim Repeat1__numRows
    Dim Repeat1__index

    Repeat1__numRows = -1
    Repeat1__index = 0
    rsSoftware_numRows = rsSoftware_numRows + Repeat1__numRows
    %>
    <html>
    <head>
    <title>New Personen - Userverwaltung</title>
    <meta http-equiv="Content-Type" content="text/html;
    charset=iso-8859-1">

    <script language="JavaScript" type="text/JavaScript">
    <!--
    ** deleted the code for form validation
    //-->
    </script>
    </head>

    <body>

    <form action="<%=MM_editAction%>" method="POST" name="frmNewPerson"
    onSubmit="MM_validateForm('Vorname','','R','Name','','R','Email','','NisEmail');return
    document.MM_returnValue">
    <table>
    <tr valign="baseline">
    <td nowrap align="right">Name:</td>
    <td> <input type="text" name="Name" value="" size="32"> </td>
    </tr>
    <tr valign="baseline">
    <td nowrap align="right" valign="top">Zusatzsoftware:</td>
    <td> <%
    While ((Repeat1__numRows <> 0) AND (NOT rsSoftware.EOF))
    %>
    <input type="checkbox"
    name="chkSoftware<%=(rsSoftware.Fields.Item("SoftwareID").Value)%>"
    value="<%=(rsSoftware.Fields.Item("SoftwareID").Value)%>">
    <%=(rsSoftware.Fields.Item("SoftwareName").Value)%>
    <% Repeat1__index=Repeat1__index+1
    Repeat1__numRows=Repeat1__numRows-1
    rsSoftware.MoveNext()
    Wend
    %>
    </table></td>
    </tr>
    <tr valign="baseline">
    <td nowrap align="right">Status:</td>
    <td> <select name="cboStatusID">
    <%
    While (NOT PersonStatus.EOF)
    %>
    <option value="<%=(PersonStatus.Fields.Item("StatusID").Value)%>"
    <%If (Not isNull((PersonStatus_first))) Then If
    (CStr(PersonStatus.Fields.Item("StatusID").Value) =
    CStr((PersonStatus_first))) Then Response.Write("SELECTED") :
    Response.Write("")%>
    ><%=(PersonStatus.Fields.Item("Status").Value)%></option>

    <%
    PersonStatus.MoveNext()
    Wend
    If (PersonStatus.CursorType > 0) Then
    PersonStatus.MoveFirst
    Else
    PersonStatus.Requery
    End If
    %>
    </select> </td>
    <tr valign="baseline">
    <td nowrap align="right">Abteilung:</td>
    <td> <select name="cboAbteilungID"
    onChange="window.location='<%=Request.Servervariables("Script_Name")%>?cboAbteilungID='+this.value;">
    <%
    While (NOT Abteilungen.EOF)
    %>
    <option <% If Abteilungen("AbteilungID") =
    Request.QueryString("cboAbteilungID") Then Response.Write("selected")
    End If %> value="<%=(Abteilungen.Fields.Item("AbteilungID").Value)%>">
    <%=(Abteilungen.Fields.Item("Abteilung").Value)%>,
    <%=(Abteilungen.Fields.Item("Standort").Value)%>
    </option>
    <%
    Abteilungen.MoveNext()
    Wend
    If (Abteilungen.CursorType > 0) Then
    Abteilungen.MoveFirst
    Else
    Abteilungen.Requery
    End If
    %>
    </select> </td>
    </tr>
    </tr>
    <tr valign="baseline">
    <td nowrap align="right">PC-Nummer:</td>
    <td> <select name="cboArbeitsplatzID">
    <%
    If Arbeitsplatz.EOF Then
    Response.Write("<option>(Kein PC zugeordnet)</option>")
    Else
    While (NOT Arbeitsplatz.EOF)
    %>
    <option value="<%=(Arbeitsplatz.Fields.Item("ArbeitsplaetzeID").Value)%>"
    <%If (Not isNull((PersonStatus_first))) Then If
    (CStr(Arbeitsplatz.Fields.Item("ArbeitsplaetzeID").Value) =
    CStr((PersonStatus_first))) Then Response.Write("SELECTED") :
    Response.Write("")%>
    ><%=(Arbeitsplatz.Fields.Item("PCNummer").Value)%></option>

    <%
    Arbeitsplatz.MoveNext()
    Wend
    If (Arbeitsplatz.CursorType > 0) Then
    Arbeitsplatz.MoveFirst
    Else
    Arbeitsplatz.Requery
    End If
    End If
    %>
    </select> </td>
    </tr>
    <tr valign="baseline">
    <td nowrap align="right">&nbsp;</td>
    <td> <input type="submit" value="Datensatz einf&uuml;gen"> </td>
    </tr>
    </table>
    <input type="hidden" name="MM_insert" value="frmNewPerson">
    </form>

    PersonStatus.Close()
    Set PersonStatus = Nothing
    %>
    <%
    Arbeitsplatz.Close()
    Set Arbeitsplatz = Nothing
    %>
    <%
    Abteilungen.Close()
    Set Abteilungen = Nothing
    %>
    <%
    rsSoftware.Close()
    Set rsSoftware = Nothing
    %>
    <%
    rsPerson.Close()
    Set rsPerson = Nothing
    %>
     
    PT, Oct 15, 2004
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. SirPoonga
    Replies:
    2
    Views:
    818
    Ben Strackany
    Jan 7, 2005
  2. weiwei
    Replies:
    0
    Views:
    1,073
    weiwei
    Jan 5, 2007
  3. msimmons
    Replies:
    0
    Views:
    549
    msimmons
    Jul 16, 2009
  4. Replies:
    5
    Views:
    317
  5. Replies:
    3
    Views:
    323
Loading...

Share This Page