简体   繁体   中英

What might the ASP.NET code for capturing data from dynamically created text boxes (in Javascript) look like?

Let's say I have some HTML and Javascript which adds text fields dynamically to a form:

<script src="/wp-includes/js/addInput.js" language="Javascript" type="text/javascript"></script>
<form method="POST">
     <div id="dynamicInput">
          Entry 1<br><input type="text" name="myInputs[]">
     </div>
     <input type="button" value="Add another text input" onClick="addInput('dynamicInput');">
</form>

var counter = 1;
var limit = 3;
function addInput(divName){
     if (counter == limit)  {
          alert("You have reached the limit of adding " + counter + " inputs");
     }
     else {
          var newdiv = document.createElement('div');
          newdiv.innerHTML = "Entry " + (counter + 1) + " <br><input type='text' name='myInputs[]'>";
          document.getElementById(divName).appendChild(newdiv);
          counter++;
     }
}

What might the ASP.NET code for capturing the data from these dynamically created text boxes look like?

public void Page_Load()
{
    ....
    Request.Params["myInputs[]"];
    ....
}

What I have done in this situation is to iterate over the DOM with a javascript function grabbing the values of these fields and read them into an array then I serialize the array (I use json2 for this) and write it to a hidden ASP.Net TextBox. On the server side I grab the values out of the textbox and deserialize them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM