views:

21

answers:

2

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?

+2  A: 
public void Page_Load()
{
    ....
    Request.Params["myInputs[]"];
    ....
}
gustavogb
A: 

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.

antonlavey