1. ホーム
  2. Web プログラミング
  3. ASP プログラミング
  4. アプリケーションのヒント

asp バッチの追加・変更・削除操作のサンプルコード

2022-01-16 10:59:59

コアとなるコードです。

<title>asp batch add modify delete operation example</title>
<%
if request.Form("op")="update" then'form submit
 ids=request.Form("ids")
 if ids<>"" then
  response.Write "Collection of data ids to delete: "&ids&"<br>"
  '========= database delete operation conn.execute("delete from xxx where id in("&ids&")")'own attention to do security verification, assuming that the ids are a collection of numbers, their own regular RegExp to determine the validity of pattern for ^\d+(,\d+)*$
 end if
 rows=request.Form("name").count'Submitted data line data, including added for i=1 to rows'Iterate through each row of data
  id=request.Form("id").item(i)&""
  name=request.Form("name").item(i)
  sex=request.Form("sex").item(i)
  age=request.Form("age").item(i)
  addr=request.Form("addr").item(i)
  if id<>"" then'modify the operation, if id is a number plus isnumeric judgment
   response.Write "To modify the data line: "&id&"|"&name&"|"&sex&"|"&age&" |"&addr&"<br>"
   'Modify operation
  else 'Add operation
   response.Write "To add data row: "&name&"|"&sex&"|"&age&"|"&addr&& quot;<br>"
   'Add operation
  end if
 next
end if
   %>
<form method="post" onsubmit="return check(this)">
<input type="hidden" name="ids" /><! -- used to store the set of ids of the records to be deleted -- >
<input type="hidden" name="op" value="update" />
<table border="1" id="tb" >
<tr><th>Name</th><th>Gender</th><th>Age</th><th>Address</th><th>Delete</th></ tr>
<! ------- the data to be modified, read the database to generate it yourself, <input type="hidden" name="id" value=""/> store id -------->
<tr>
<td><input type="text" value="name1" name="name" /></td>
<td><input type="text" value="gender1" name="sex" /></td>
<td><input type="text" value="age1" name="age" /></td>
<td><input type="text" value="address1" name="addr" /></td>
<td><input type="button" value="delete" onclick="removeRow(this)" /><<input type="hidden& quot; name="id" value="1"/></td>
</tr>
<tr>
<td><input type="text" value="name2" name="name" /></td>
<td><input type="text" value="gender2" name="sex" /></td>
<td><input type="text" value="age2" name="age" /></td>
<td><input type="text" value="address2" name="addr" /></td>
<td><input type="button" value="delete" onclick="removeRow(this)" /><<input type="hidden& quot; name="id" value="2"/></td>
</tr>
<! Example of data to be modified by ------- ends -------->
<tr><td colspan="5" align="center"><input type="submit" value="submit"/> < input type="button" value="Add new row of data" onclick="addRow()" /></td></tr>
</table>
</form>
<script type="text/javascript">
  function removeRow(btn){
    if (confirm('Confirm delete?!')) {
      var tr=btn.parentNode;
      var id = btn.nextSibling;// note that there should be no space between the delete button and id the hidden control, otherwise nextSibling is a blank node in standard browsers
      if (id.value ! = '') {//Delete the line that exists instead of adding it, then the id is stored in ids
        btn.form.ids.value += (btn.form.ids.value == '' ? '' : ',') + id.value;
      }
      tr.parentNode.removeChild(tr);
    }
  }
  function addRow() {
    var tb = document.getElementById('tb'), tr = tb.insertRow(tb.rows.length - 1),td=tr.insertCell(0);
    td.innerHTML = '<input type="text" name="name" />';
    td = tr.insertCell(1); td.innerHTML = '<input type="text" name="sex" />';
    td = tr.insertCell(2); td.innerHTML = '<input type="text" name="age" />';
    td = tr.insertCell(3); td.innerHTML = '<input type="text" name="addr" />';
    td = tr.insertCell(4); td.innerHTML = '<input type="button" value="delete" onclick="removeRow(this)" />< input type="hidden" name="id" />';//add data row id is empty
  }
  function check(f) {
    var tb = document.getElementById('tb'), ipts;
    for (var i = 1, j = tb.rows.length - 1; i < j; i++) {//Input validation, remove the first row header and last row operation
      ipts = tb.rows[i].getElementsByTagName('input');
      if (ipts[0].value == '') { alert('Please enter your name!') ; ipts[0].focus(); return false; }
      if (ipts[1].value == '') { alert('Please enter gender!') ; ipts[1].focus(); return false; }
      if (ipts[2].value == '')