enderaric
enderaric

Reputation: 162

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 ?

Upvotes: 0

Views: 754

Answers (2)

Guruprasad J Rao
Guruprasad J Rao

Reputation: 29683

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

Upvotes: 1

renakre
renakre

Reputation: 8291

Here is my takeon on your issue:

$('#btn').click(function () {
    var iCnt = 1;
    $('#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>'));
});

$('#btnget').click(function () {
    alert($('#container').find('input:text').val());
});

<div id='container'></div>
<input type='button' id='btn' value='submit'/>
<input type='button' id='btnget' value='get value'/>

Demo: https://jsfiddle.net/hxq5cj0x/

Upvotes: 0

Related Questions