Solved

Forcing form refresh - classic ASP

Posted on 2016-09-19
7
45 Views
Last Modified: 2016-09-20
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.
0
Comment
Question by:YZlat
7 Comments
 
LVL 32

Accepted Solution

by:
Big Monty earned 250 total points
Comment Utility
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
 
LVL 35

Author Comment

by:YZlat
Comment Utility
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
 
LVL 52

Assisted Solution

by:Scott Fell, EE MVE
Scott Fell,  EE MVE earned 200 total points
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
Comment Utility
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
 
LVL 51

Assisted Solution

by:Julian Hansen
Julian Hansen earned 50 total points
Comment Utility
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
 
LVL 35

Author Comment

by:YZlat
Comment Utility
Julian Hansen, actually I will be processing data on the first form as well
0
 
LVL 35

Author Closing Comment

by:YZlat
Comment Utility
Thank you!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now