Search code examples
c#jqueryasp.nettextboxauto-generate

How to get value from auto generated multiple texboxes in asp.net?


I created auto textboxes with jquery codes. But I can't get values from them. Here is my code to generate textboxes

$('#btn').click(function () {
$(container).append('<tr id="tba' + iCnt + '"> <td class="sol" style="width:326px;">
<b>' + iCnt + '.</b>Dates (from - to)</td><td class="sag">
<input type=text class="input" id=txtdates' + iCnt + ' ' + ' /></td>
</tr>');
}

I can add textbox with this code. How can I get value from these codes ?


Solution

  • First import this namespace

    using System.Web.Script.Serialization;
    

    Next add a property called name to your dynamically created textbox as below

    $('#btn').click(function () {
           $(container).append('<tr id="tba' + iCnt + '"> <td class="sol" style="width:326px;">
                <b>' + iCnt + '.</b>Dates (from - to)</td><td class="sag">
                <input type="text" name="DynamicTextBox" class="input" id="txtdates"' + iCnt + ' ' + ' />
               </td>
               </tr>');
    }
    

    in your server method you can access it as below

    public void Post(object sender, EventArgs e)
    {
        string[] textboxValues = Request.Form.GetValues("DynamicTextBox");
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        this.Values = serializer.Serialize(textboxValues);
        string message = "";
        foreach (string textboxValue in textboxValues)
        {
            message += textboxValue + "\\n";
        }
    }
    

    Source