CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2003
    Location
    Pakistan
    Posts
    15

    Exclamation How to transfer an array of data (retrived from database at server side) to client si

    I have two list boxes.
    First list box is filled with some enteries from database.

    Now when user select one entry from first list box(client-side event) then second list box is filled with values based on that first selection.

    Second list box enteries are retrived from database(server side action).

    Guide me how to do this.

  2. #2
    Join Date
    Jul 2002
    Location
    India
    Posts
    505
    Assuming that you are using ASP on the server side and Javascript on the client side, and that you know how to retrive HTML form values entered by users in your sever side script, here is how you would do it.

    1) user selects a value from ddlState (drop down list box showing values of states in some country. We now want to display the vaues of cities in that state in ddlCity - the second list box)

    2) when the uses selects the value, onchange event of the listbox is fired. Submit the form in this event. The action of the form is the same page. so it is posted to itself.


    3) On the server side, retrive the value of the ddlState list box. Considering that you have used the post method, you can retrive the value as Request.form(ddlState). Use this value to query your database and retrive teh values for cities in that state.

    4) Build the <option> list for the ddlCity list box by iterating thru the rows of the recordset returned by your query.

    <select name="ddlCity">
    <%
    do while not Rs.eof
    response.write "<option value=" + Rs.fields("city") + ">" + Rs.fields("city") + "</option>"
    Rs.movenext
    loop
    %>
    </select>

    You must have guessed that we would need the asp page to have two sections, one part that is executed when the page is called the first time and the other when the form is posted to it.

    I hope all this makes sense. It is difficul explaining concepts lke these. Anyhow, if you have any specific problems do let us know.

    Satish

  3. #3
    Join Date
    Jan 2003
    Location
    Pakistan
    Posts
    15
    Thanks for ur reply.
    Its my mistake i should be more specific in my question.

    You got it right all but only one thing should be clarified to you.

    For onSelect event of first list box i donot want to submit my form.

    I fetch all values of second listbox at the time first list box enteries are retrived. SO as i mentined in question

    I have values in server side variables, now at (client-side) onselect event i want to transfer these server-side values to fill second list box.
    Thanks

  4. #4
    Join Date
    Feb 2002
    Location
    Texas
    Posts
    51
    This is an example of how I had to do it for my project:

    Code:
    sub BuildProblemJava()
    	dim sSql, rsProblem, sType, iCnt
    	sSql = "SELECT * FROM tblProblems ORDER BY Type_Id, Problem_Desc"
    	
    	set rsProblem = Server.CreateObject("ADODB.Recordset")
    	rsProblem.Open sSql, ADOcn, adOpenForwardOnly, adLockReadOnly
    	
    	with Response
    		.Write "function loadProblems() {" & vbCr
    		.Write space(5) & "document.frmIncident.chkNewProblem.disabled = false;" & vbCr
    		.Write space(5) & "do {" & vbCr
    		.Write space(10) & "document.frmIncident.sltProblem.remove(0);" & vbCr
    		.Write space(5) & "} while(document.frmIncident.sltProblem.length > 0);" & vbCr
    		.Write space(5) & "switch (document.frmIncident.sltType.value) {" & vbCr
    		iCnt = 0
    		do until rsProblem.EOF
    			if sType <> rsProblem("Type_Id") then 
    				sType = rsProblem("Type_Id")
    				.Write space(5) & "case " & chr(34) & rsProblem("Type_Id") & chr(34) & ":" & vbCr
    				.Write space(10) & "{" & vbCr
    			end if
    				.Write space(10) & "myOption" & iCnt & " = document.createElement(" & _
    					   chr(34) & "OPTION" & chr(34) & ");" & vbCr
    				.Write space(10) & "myOption" & iCnt & ".value = '" & rsProblem("Problem_Id") & "';" & vbCr
    				.Write space(10) & "myOption" & iCnt & ".text = '" & rsProblem("Problem_Desc") & "';" & vbCr
    				.Write space(10) & "document.frmIncident.sltProblem.add(myOption" & iCnt & ");" & vbCr
    			rsProblem.MoveNext
    			if rsProblem.EOF then
    				.Write "//EOF" & vbCr
    				.Write space(10) & "document.frmIncident.chkNewProblem.checked = false;" & vbCr
    				.Write space(10) & "document.frmIncident.sltProblem.style.display = '';" & vbCr
    				.Write space(10) & "document.frmIncident.Problem.style.display = 'none';" & vbCr
    				.Write space(10) & "}" & vbCr
    				.Write space(10) & "break;" & vbCr
    			elseif sType <> rsProblem("Type_Id") then
    				.Write "//Type change" & vbCr
    				.Write space(10) & "document.frmIncident.chkNewProblem.checked = false;" & vbCr
    				.Write space(10) & "document.frmIncident.sltProblem.style.display = '';" & vbCr
    				.Write space(10) & "document.frmIncident.Problem.style.display = 'none';" & vbCr
    				.Write space(10) & "}" & vbCr
    				.Write space(10) & "break;" & vbCr
    			end if
    			iCnt = iCnt + 1
    		loop
    		.Write space(5) & "default:" & vbCr
    		.Write space(10) & "{" & vbCr
    		.Write space(10) & "document.frmIncident.sltProblem.value = 0;" & vbCr
    		.Write space(10) & "document.frmIncident.sltProblem.style.display = 'none';" & vbCr
    		.Write space(10) & "document.frmIncident.Problem.style.display = '';" & vbCr
    		.Write space(10) & "document.frmIncident.chkNewProblem.checked = true;" & vbCr
    		.Write space(10) & "document.frmIncident.chkNewProblem.disabled = true;" & vbCr
    		.Write space(10) & "}" & vbCr
    		.Write space(5) & "}" & vbCr
    		.Write "}" & vbCr
    	end with
    Here is a brief explanation of the code. First I have to remove any values that exist in the drop down list and that is what "document.frmIncident.sltProblem.remove(0)" does. It removes the 0th level out of the drop down list. I then have to create a new option element with a unique identifier, then set the attributes of the option element, and finally add the option to the drop down list.

    This code will do what you are looking for but it is probably more robust then you need.
    Jeremy Ames
    DP Support Specialist
    Ben E Keith Foods

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured