Hi there,

Can anyone help me out..

I have a page where in I have a table
(Iits related to the tradein offers to enter one by one)
when the new page is requested, all the rows in the table will be blank
and when i click on the new offrer button, I should be able to add text boxes in the table and do some modifications.

(I know its easy in asp.net, but i am stuck with asp, and my deliverable is coiming nearer)
Is there any way where we can create dynamic text boxes??

Also, when a hyperlink is clicked to get into this page, all the past details stored in the database should be shown in the respective text boxes and ready again for any modifications!


PLS PLS PLs do help me out ..
I know there are a lot of gurus working out there in these issues,
can any one help me???

Thanks in advance

Regards

Dani AI

Generated

Short, practical plan that builds on the existing replies from , and and shows a safe, maintainable way to add textboxes dynamically in classic ASP pages.

Use client-side DOM insertion for the UI, render stored rows from the database on the server, and submit everything together so the server can persist changes. Avoid full-page navigations just to add a field (that’s what caused ’s worry) unless you persist the state.

A minimal client-side pattern (no frameworks): create inputs with incremented names and update a hidden counter so the server knows how many fields arrived. Prefer DOM APIs like document.createElement / appendChild rather than string HTML insertion for clarity and fewer parsing quirks. See the DOM docs for details. (developer.mozilla.org)

<!-- page: somewhere inside the form -->
<table id="rows"></table>
<input type="hidden" id="totActivities" name="totActivities" value="0">

<script>
(function(){
  var nextIdx = 1;
  var container = document.getElementById('rows');
  var counter = document.getElementById('totActivities');

  function addActivity(value) {
    var tr = document.createElement('tr');
    var td = document.createElement('td');
    var input = document.createElement('input');
    input.type = 'text';
    input.name = 'activity_' + nextIdx;
    input.value = value || '';
    input.size = 60;
    td.appendChild(input);
    tr.appendChild(td);
    container.appendChild(tr);
    counter.value = nextIdx;
    nextIdx++;
  }
  window.addActivity = addActivity; // call from a button: onclick="addActivity()"
})();
</script>

Server-side: when the page is requested for editing, output existing DB rows as inputs named activity_1, activity_2, ... and set a hidden totActivities. On submit, loop from 1 to that counter and read each value. Alternatively, if you prefer identical names (e.g. many inputs named activity), classic ASP’s Request.Form can be iterated with .Count and indexed; it will also return a comma-delimited string when accessed without an index. Use these features to simplify processing. (learn.microsoft.com)

<%
' render saved rows
i = 0
Do While Not rs.EOF
  i = i + 1
%>
<tr><td>
  <input type="text" name="activity_<%=i%>" value="<%= Server.HTMLEncode(rs("activity")) %>">
</td></tr>
<%
  rs.MoveNext
Loop
%>
<input type="hidden" name="totActivities" value="<%=i%>">

<%
' on POST:
If Request.ServerVariables("REQUEST_METHOD") = "POST" Then
  tot = CLng(Request.Form("totActivities"))
  For j = 1 To tot
    val = Request.Form("activity_" & j)
    ' validate and save to DB
  Next
End If
%>

Notes and cautions

  • Always HTML-encode user/DB values when writing into value="..." to avoid XSS/broken HTML (use Server.HTMLEncode). (learn.microsoft.com)
  • Navigating (window.location or reload) discards dynamic DOM state; use sessionStorage/localStorage or post incremental changes with AJAX if you need to persist client-side additions before a full submit. (developer.mozilla.org)
  • Request.Form has limits (and security implications) — validate inputs and use parameterized DB queries. (learn.microsoft.com)

This approach combines the responsiveness of /’s client-side ideas with a reliable server-side pattern for showing and saving past data.

Recommended Answers

All 5 Replies

I would personally use Javascript to dynamically add new inputs, but if you must use ASP you can either pass parameters via the query string or hidden fields that tell your script to write out new text fields. Ex: the url of the page where the text field is to be written to the page could be ./mypage.asp. You could then set the onclick of a button to "window.location='./mypage.asp?NEWFIELD=Y'". Your script would then to check for a value for NEWFIELD, by using Request.QueryString("NEWFIELD"), and when it ="Y" you would create a new field. Obviusly there are many different ways to go about this, but this is one of them.

<Script>
//Expand form with a new File fields if needed.
var nfiles1 = 1;
var nfiles2 = 2;
function Expand()
{
var a,b;
b=document.f.totGoods.value;
a=parseInt(b) + 1 ; 
document.f.totGoods.value="";
document.f.totGoods.value=a; 
var adh1 = '<b><font face=arial color=red size=2>'+ nfiles2 +'. ';
var adh = '';nfiles1++nfiles2++ files.insertAdjacentHTML('BeforeEnd',adh1); files.insertAdjacentHTML('BeforeEnd',adh); 
return false;
} 
</Script>

<table border=3 cellpadding=15 cellspacing=10 width=80% bgcolor=#F3DCF8>
  <tr>
      <td>
               <table border=0 cellpadding=5 cellspacing=5>
               <tr>
                    <td>
                       <b><font face=arial color=red size=2>1.&nbsp;&nbsp;<input class="text" name="txtActivity" style="font-size: xx-small; height: 20; width: 420; border-width: 1px">
                         &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<Input Type=Image src=Images\click.gif  OnClick=return(Expand()) Style="border=0;background=#FFDEBF;cursor:hand">
   <Div ID=files>
</div> 
                    </td>
               </tr>
               </table>

      </td>
  </tr>
</table>

Please check your code before posting on the net or atlease explain what you have added.

<Script>
//Expand form with a new File fields if needed.
var nfiles1 = 1;
var nfiles2 = 2;
function Expand()
{
var a,b;
b=document.f.totGoods.value;
a=parseInt(b) + 1 ;
document.f.totGoods.value="";
document.f.totGoods.value=a;
var adh1 = '<b><font face=arial color=red size=2>'+ nfiles2 +'. ';
var adh = '';nfiles1++nfiles2++ files.insertAdjacentHTML('BeforeEnd',adh1); files.insertAdjacentHTML('BeforeEnd',adh);
return false;
}
</Script>

<table border=3 cellpadding=15 cellspacing=10 width=80% bgcolor=#F3DCF8>
<tr>
<td>
<table border=0 cellpadding=5 cellspacing=5>
<tr>
<td>
<b><font face=arial color=red size=2>1.&nbsp;&nbsp;<input class="text" name="txtActivity" style="font-size: xx-small; height: 20; width: 420; border-width: 1px">
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<Input Type=Image src=Images\click.gif OnClick=return(Expand()) Style="border=0;background=#FFDEBF;cursor:hand">
<Div ID=files>
</div>
</td>
</tr>
</table>

</td>
</tr>
</table>

Please check your code before posting on the net or atlease explain what you have added.

I would personally use Javascript to dynamically add new inputs, but if you must use ASP you can either pass parameters via the query string or hidden fields that tell your script to write out new text fields. Ex: the url of the page where the text field is to be written to the page could be ./mypage.asp. You could then set the onclick of a button to "window.location='./mypage.asp?NEWFIELD=Y'". Your script would then to check for a value for NEWFIELD, by using Request.QueryString("NEWFIELD"), and when it ="Y" you would create a new field. Obviusly there are many different ways to go about this, but this is one of them.

Dear Sir,
I just wanna know that if we click by the method you described, wont be all other written data vanished???
Awaiting of your quick reply.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.