Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Regardless of Webforms or MVC, when a a form is submitted, it will contain all of the fields in the form post data.</p> <p>You have chosen to dynamically inject N number of elements all with the same name, "Intrebare[]".</p> <p>When you do this, the form post data will contain one element (actually a key-value pair) called "Intrebare[]", and it will contain all of the values of your dynamically injected textboxes, separated by commas.</p> <p>I've put together a very small sample of code that should help you get what you need; actually my code sample iterates through the entire form post so you can see all the fields:</p> <p>First, I created an empty ASP.net Web Forms Application.</p> <pre><code>&lt;script type="text/javascript"&gt; function generate() { var tot = document.getElementById("nrintrebari").value; var tbl = document.getElementById("sim"); for (var i = 1; i &lt;= tot; i++) { tbl.innerHTML = tbl.innerHTML + 'Intrebare nr.' + i + ' &lt;input type="text" size = "80" maxlength= "200" name="intrebare[]" style="height:30px; background-color:#B8B8B8; " &gt; &lt;br&gt;&lt;br&gt;&lt;br&gt; '; } } &lt;/script&gt; &lt;input id="nrintrebari" type="text" value="10" /&gt; &lt;div id="sim"&gt; &lt;/div&gt; &lt;input type="submit" value="submit" /&gt; Result: &lt;asp:Label ID="TestResultLabel" runat="server" /&gt; &lt;script type="text/javascript"&gt; generate(); &lt;/script&gt; </code></pre> <p>When the user clicks the "submit" button, the form will post with all of the dynamic textboxes included - I chose 10 for sanity...</p> <p>Here is what the code looks like to go through the form collection on the code-behind:</p> <pre><code>using System; using System.Web; using System.Text; namespace StackOverflow.Web.LoopThroughForm { public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (IsPostBack) { GetFormElements(); } } private void GetFormElements() { // HttpContext object contains information of the current request, // and the Form object contains all of the submitted form elements var form = HttpContext.Current.Request.Form; StringBuilder sb = new StringBuilder(); string resultFormat = "&lt;div&gt;Element: {0} - Value: {1}"; for (int i = 0; i &lt; form.Count; i++) { sb.AppendFormat(resultFormat, form.Keys[i], form[i]); } TestResultLabel.Text = sb.ToString(); } } } </code></pre> <p>This will include ALL form fields, including the ViewState field; From here you have many options, you can hardcode the "Intrebare[]" field and only get that one, and split the result by a comma:</p> <pre><code>private void GetIntrebare() { var form = HttpContext.Current.Request.Form; StringBuilder sb = new StringBuilder(); sb.Append("&lt;div&gt;Intrebare Values: &lt;br /&gt;"); for (int i = 0; i &lt; form.Count; i++) { if (form.Keys[i] == "intrebare[]") { string valuesFormat = "Value {0} : {1} &lt;br /&gt;"; string[] values = form[i].Split(','); for (int ii = 0; ii &lt; values.Length; ii++) { // Label the value index + 1 to match the 'actual' dynamic textbox sb.AppendFormat(valuesFormat, ii + 1, values[ii]); } } } sb.Append("&lt;/div&gt;"); TestResultLabel.Text = sb.ToString(); } </code></pre> <p>I hope this helps to get you going with retrieving values form form/post data. FWIW, I believe MVC is the same - Controllers should also have the HttpContext object available that will contain all of this data, because under the hood, HTTP POST/GET does not care if you are using Java, .net, MVC, or whatever in order to read the values submitted by a user.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload