Forcing form refresh - classic ASP

I have a table that displays records from a recordset on one form - `Form1`, and on the form below that - `Form2` - has a couple of textboxes and a button to add records to the recordset. if I print out recordset right after adding a new record, it shows that the record has been added correctly, yet the first form does not get refreshed to reflect a new change. How can I force that refresh usinfg classic ASP?


    <div is="content">
    <%
       Dim RS 
       set rs=Server.CreateObject("ADODB.recordset")
       'code to fill the recordset here
       '.....
    %>
       <form method="POST" name="Form1" id="Form1" action="Page1.asp">
          <table id="dataTbl">
             <tr>
                 <th>Column 1</th>
                 <th>Column 2</th>
             </tr>

	     <%If RS.EOF Then
			Response.Write("No records found")
		Else
			RS.MoveFirst
			While Not RS.EOF
			%>
			<tr>							
				<td><%=RS.Fields("Field1").value%></td>
				<td><%=RS.Fields("Field2").value%></td>		
			</tr>
			<%							
				RS.MoveNext()
			     Wend
		 	End If
			%>
	   </table>
	   <br/>
	   <input type="button" class="button" id="btnSubmit" name="btnSubmit" value="Submit" onClick="btnSubmit_Click()" />
        </form>
        <br/><br/>
            <form method="POST" action="Page1.asp" id="Form2" name="Form2">
								<table>
									<tr>
										<td>First Field:</td>
										<td><input type="text" id="Value1" name="Value1" /></td>
									</tr>
									<tr>
										<td>Second Field:</td>
										<td><input type="text" id="Value2" name="Value2" /></td>
									</tr>
									<tr>
										<td colspan="2"><input type="button" id="btnAdd" name="btnAdd" value="Add" onClick="btnAdd_Click()"/></td>
									</tr>
								</table>
								<%
								Dim val1, val2
								If Len(Request.Form) > 0 Then
								  val1 = request.Form("Value1")
								  val2 = request.Form("Value2")
								 
								  'add a record to recordset
								  With RS
									.AddNew
									.Fields("Field1")=val1
									.Fields("Field2")="val2
									
									.Update				
								End With
								Response.Write RS.RecordCount & "<br/>"
								
								RS.MoveFirst
								While Not RS.EOF
									Response.Write RS.Fields("Field1").value & ":" & RS.Fields("Field2").value & "<br/>"
									RS.Movenext()
								Wend
								End If
								%>
							</form>
							<%
					                ''Here update the recordset with whatever values selected in the table on form1
							RS.Close()
							Set RS = Nothing
							%>
					</div>

Open in new window


And here is my javascript:
    <script type="text/jscript">
			
			function btnSubmit_Click(){
				document.Form1.submit();
			}
			
			function btnAdd_Click(){
				document.Form2.submit();
			}
			
			
		</script>

Open in new window

So the above does add a new record to the Recordset object but the table on the Form1 does not reflect the change. How can I ensure it does?

P.S> The recordset at this point is not connected to an actual database, I just fill it wIth dummy records.
LVL 35
YZlatAsked:
Who is Participating?
 
Big MontyConnect With a Mentor Senior Web Developer / CEO of ExchangeTree.org Commented:
you are outputting the entire recordset BEFORE you add the form data to it, so it'll never be fully updated.

moving some code around, i think this'll work:

    <div is="content">
    <%
       Dim RS 
       set rs=Server.CreateObject("ADODB.recordset")
       'code to fill the recordset here
       '.....
    %>
       <form method="POST" name="Form1" id="Form1" action="Page1.asp">
          <table id="dataTbl">
             <tr>
                 <th>Column 1</th>
                 <th>Column 2</th>
             </tr>

	     <%If RS.EOF Then
			Response.Write("No records found")
		Else
			RS.MoveFirst
			While Not RS.EOF
			%>
			<tr>							
				<td><%=RS.Fields("Field1").value%></td>
				<td><%=RS.Fields("Field2").value%></td>		
			</tr>
			<%							
				RS.MoveNext()
			     Wend
		 	End If
			
			Dim val1, val2
			If Len(Request.Form) > 0 Then
			  val1 = request.Form("Value1")
			  val2 = request.Form("Value2")
			 
			  'add a record to recordset
			  With RS
				.AddNew
				.Fields("Field1")=val1
				.Fields("Field2")="val2
				
				.Update				
			End With
		'	Response.Write RS.RecordCount & "<br/>"
			
		'	RS.MoveFirst
			'While Not RS.EOF
			'	Response.Write RS.Fields("Field1").value & ":" & RS.Fields("Field2").value & "<br/>"
		'		RS.Movenext()
		'	Wend
			End If

			%>
	   </table>
	   <br/>
	   <input type="button" class="button" id="btnSubmit" name="btnSubmit" value="Submit" onClick="btnSubmit_Click()" />
        </form>
        <br/><br/>
            <form method="POST" action="Page1.asp" id="Form2" name="Form2">
								<table>
									<tr>
										<td>First Field:</td>
										<td><input type="text" id="Value1" name="Value1" /></td>
									</tr>
									<tr>
										<td>Second Field:</td>
										<td><input type="text" id="Value2" name="Value2" /></td>
									</tr>
									<tr>
										<td colspan="2"><input type="button" id="btnAdd" name="btnAdd" value="Add" onClick="btnAdd_Click()"/></td>
									</tr>
								</table>
							</form>
							<%
					                ''Here update the recordset with whatever values selected in the table on form1
							RS.Close()
							Set RS = Nothing
							%>
					</div>

Open in new window


** i commented out the code to that writes out the newly added data
0
 
YZlatAuthor Commented:
Thanks for you fast response!

I have tried moving the code like you have suggested but gotten the same result.

Not sure if I made it clear what I am doing. I have a table that displays data from the recordset that is already there. Then there is a second form that allows the user to add more fields to the recordset. It might or might not be used
0
 
Scott Fell, EE MVEConnect With a Mentor Developer & EE ModeratorCommented:
Below is real rough, but essentially  you want to put your logic at the top of the page where first you checkif the form is submitted. If it is, then add a record.  If not, don't add a record.

Now run your recordset and display at the top in your columns and at the bottom in your form fields.

Again, code below is not production, just illustration.

<%
If Len(Request.Form) > 0 Then
    ' add record to db
end if

' create recordset
field1 = rs("field1")
field2 = rs("field2")
%>

<html>
<table>
     <tr>
             <td>col1</td><td>col2</td>
      <tr>
     <tr>
             <td><%=field1%></td><td><%=field2%></td>
      <tr>
</table>
<form>
         <input name="field1" value="<%=field1%>">
        <input name="field2" value="<%=field2%>">
</form>
</html>

Open in new window

0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
To add, if you are expecting multiple rows of data, run your recordset in both places, but better to use getrows to place your data to an array, then access your array multiple times without hitting the db.
0
 
Julian HansenConnect With a Mentor Commented:
I believe Big monty and Scott have covered this - Big Monty just needed to move the code a little bit higher as Scott did.

I am interested to know why you have onclick handlers for your submit buttons - you are doing nothing in those functions except the default submit so why not just let the forms submit as they normally would?

A submit button is local to the form it is in - when you click it - it submits the parent form - ignoring any other forms on the page.

Also not sure why you need a submit button for the first form - you are not processing any data - you can achieve the same thing with an <a href> styled like a button that just reloads the page. Not saying you can't use a form - just interested to know the thinking behind it.
0
 
YZlatAuthor Commented:
Julian Hansen, actually I will be processing data on the first form as well
0
 
YZlatAuthor Commented:
Thank you!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.