Get value from dynamic input text html in Codebehind asp net












1















this is the html table generated based on an input by the user. I'm trying to get the values from the inputs but no luck. I even added runat="server" tag on them but nothing



$(function () {
$("[id*=txtNumComponentes]").keyup(function () {

$('#tblComponentesContainer').empty();
var txt = $("[id*=txtNumComponentes]").val();
debugger
if (txt != "" || txt != 0) {
var table = '<table id="tblComponentes" class="table table-bordered text-center mx-auto" style="width: 80%; background: #d9deed73; min-width:700px; border: 2px solid #212529;">' +
'<caption>Lista de componentes</caption>' +
'<thead class="thead-dark">' +
'<tr>' +
'<th scope="col" style="width:10px;">#</th>' +
'<th scope="col" style="width:100px;">Componente *</th>' +
'<th scope="col" style="width:100px;">Base *</th>' +
'<th scope="col" style="width:175px;">Comprimento *</th>' +
'</tr>' +
'</thead>' +
'<tbody>';



for (var i = 0; i < txt; i++) {
table += '<tr>' +
'<th scope="row">' + (i + 1) + '</th>' +
'<td><input type="text" runat="server" class="form-control form-control-sm rounded border border-dark" id="txtComponente' + (i + 1) + '" placeholder="Componente" style="width:200px;" /></td>' +
'<td><input type="text" runat="server" class="form-control form-control-sm rounded border border-dark" id="txtBase' + (i + 1) + '" placeholder="Base" style="width:200px;" /></td>' +
'<td>' +
'<div class="input-group input-group-sm">' +
'<input type="text" runat="server" class="form-control form-control-sm rounded border border-dark" id="txtComprimento' + (i + 1) + '" placeholder="Comprimento" style="width:50px;" />' +
'<div class="input-group-append">' +
'<span class="input-group-text border border-dark text-dark"><strong>mm</strong></span>' +
'</div>' +
'</div>'
'</td>' +
'</tr>';
}

table += '</tbody>' +
'</table>';

$('#tblComponentesContainer').html(table);
}
else
{
('#tblComponentesContainer').html();
}
})


});



here's what i have tested so far on codebehind and it returns null..



 protected void btnCriarArtigo_Click(object sender, EventArgs e)
{
if(Page.IsValid)
{
string n = Request.Form["txtComponente1"];

}
else
{

}
}


I have done something similar but it was all on server side using a repeater but I wanted to do it on client side cause it's faster to generate the fields. I have addded method="POST" to my form on masterpage too but didn't help



EDIT:



I also added name attribute but still getting null value



string n = Request.Form["Componente1"];
string a = Request.Form["Base1"];
string b = Request.Form["Comprimento1"];


I just added name like this to each input



name="Base' + (i + 1) + '"


Also i tried to get the input values with javascript and they do appear in there, even inside the form so it's weird why it doesn't happen on codebehind... Maybe i have to access the form through ID or something?



EDIT2:



Here's another update, I tried to create a panel around the DIV where the table with the input fields is generated. So while it doesn't detect the input fields on the control list, it detects the table tags only. I think I have to access them through the table somehow










share|improve this question

























  • Try putting "name" attribute in the input element, I believe that should work.

    – dime2lo
    Jan 1 at 21:42











  • Updated my answer. I did this and still getting null. I can't even find them on the form usign debug

    – Jackal
    Jan 1 at 21:46


















1















this is the html table generated based on an input by the user. I'm trying to get the values from the inputs but no luck. I even added runat="server" tag on them but nothing



$(function () {
$("[id*=txtNumComponentes]").keyup(function () {

$('#tblComponentesContainer').empty();
var txt = $("[id*=txtNumComponentes]").val();
debugger
if (txt != "" || txt != 0) {
var table = '<table id="tblComponentes" class="table table-bordered text-center mx-auto" style="width: 80%; background: #d9deed73; min-width:700px; border: 2px solid #212529;">' +
'<caption>Lista de componentes</caption>' +
'<thead class="thead-dark">' +
'<tr>' +
'<th scope="col" style="width:10px;">#</th>' +
'<th scope="col" style="width:100px;">Componente *</th>' +
'<th scope="col" style="width:100px;">Base *</th>' +
'<th scope="col" style="width:175px;">Comprimento *</th>' +
'</tr>' +
'</thead>' +
'<tbody>';



for (var i = 0; i < txt; i++) {
table += '<tr>' +
'<th scope="row">' + (i + 1) + '</th>' +
'<td><input type="text" runat="server" class="form-control form-control-sm rounded border border-dark" id="txtComponente' + (i + 1) + '" placeholder="Componente" style="width:200px;" /></td>' +
'<td><input type="text" runat="server" class="form-control form-control-sm rounded border border-dark" id="txtBase' + (i + 1) + '" placeholder="Base" style="width:200px;" /></td>' +
'<td>' +
'<div class="input-group input-group-sm">' +
'<input type="text" runat="server" class="form-control form-control-sm rounded border border-dark" id="txtComprimento' + (i + 1) + '" placeholder="Comprimento" style="width:50px;" />' +
'<div class="input-group-append">' +
'<span class="input-group-text border border-dark text-dark"><strong>mm</strong></span>' +
'</div>' +
'</div>'
'</td>' +
'</tr>';
}

table += '</tbody>' +
'</table>';

$('#tblComponentesContainer').html(table);
}
else
{
('#tblComponentesContainer').html();
}
})


});



here's what i have tested so far on codebehind and it returns null..



 protected void btnCriarArtigo_Click(object sender, EventArgs e)
{
if(Page.IsValid)
{
string n = Request.Form["txtComponente1"];

}
else
{

}
}


I have done something similar but it was all on server side using a repeater but I wanted to do it on client side cause it's faster to generate the fields. I have addded method="POST" to my form on masterpage too but didn't help



EDIT:



I also added name attribute but still getting null value



string n = Request.Form["Componente1"];
string a = Request.Form["Base1"];
string b = Request.Form["Comprimento1"];


I just added name like this to each input



name="Base' + (i + 1) + '"


Also i tried to get the input values with javascript and they do appear in there, even inside the form so it's weird why it doesn't happen on codebehind... Maybe i have to access the form through ID or something?



EDIT2:



Here's another update, I tried to create a panel around the DIV where the table with the input fields is generated. So while it doesn't detect the input fields on the control list, it detects the table tags only. I think I have to access them through the table somehow










share|improve this question

























  • Try putting "name" attribute in the input element, I believe that should work.

    – dime2lo
    Jan 1 at 21:42











  • Updated my answer. I did this and still getting null. I can't even find them on the form usign debug

    – Jackal
    Jan 1 at 21:46
















1












1








1








this is the html table generated based on an input by the user. I'm trying to get the values from the inputs but no luck. I even added runat="server" tag on them but nothing



$(function () {
$("[id*=txtNumComponentes]").keyup(function () {

$('#tblComponentesContainer').empty();
var txt = $("[id*=txtNumComponentes]").val();
debugger
if (txt != "" || txt != 0) {
var table = '<table id="tblComponentes" class="table table-bordered text-center mx-auto" style="width: 80%; background: #d9deed73; min-width:700px; border: 2px solid #212529;">' +
'<caption>Lista de componentes</caption>' +
'<thead class="thead-dark">' +
'<tr>' +
'<th scope="col" style="width:10px;">#</th>' +
'<th scope="col" style="width:100px;">Componente *</th>' +
'<th scope="col" style="width:100px;">Base *</th>' +
'<th scope="col" style="width:175px;">Comprimento *</th>' +
'</tr>' +
'</thead>' +
'<tbody>';



for (var i = 0; i < txt; i++) {
table += '<tr>' +
'<th scope="row">' + (i + 1) + '</th>' +
'<td><input type="text" runat="server" class="form-control form-control-sm rounded border border-dark" id="txtComponente' + (i + 1) + '" placeholder="Componente" style="width:200px;" /></td>' +
'<td><input type="text" runat="server" class="form-control form-control-sm rounded border border-dark" id="txtBase' + (i + 1) + '" placeholder="Base" style="width:200px;" /></td>' +
'<td>' +
'<div class="input-group input-group-sm">' +
'<input type="text" runat="server" class="form-control form-control-sm rounded border border-dark" id="txtComprimento' + (i + 1) + '" placeholder="Comprimento" style="width:50px;" />' +
'<div class="input-group-append">' +
'<span class="input-group-text border border-dark text-dark"><strong>mm</strong></span>' +
'</div>' +
'</div>'
'</td>' +
'</tr>';
}

table += '</tbody>' +
'</table>';

$('#tblComponentesContainer').html(table);
}
else
{
('#tblComponentesContainer').html();
}
})


});



here's what i have tested so far on codebehind and it returns null..



 protected void btnCriarArtigo_Click(object sender, EventArgs e)
{
if(Page.IsValid)
{
string n = Request.Form["txtComponente1"];

}
else
{

}
}


I have done something similar but it was all on server side using a repeater but I wanted to do it on client side cause it's faster to generate the fields. I have addded method="POST" to my form on masterpage too but didn't help



EDIT:



I also added name attribute but still getting null value



string n = Request.Form["Componente1"];
string a = Request.Form["Base1"];
string b = Request.Form["Comprimento1"];


I just added name like this to each input



name="Base' + (i + 1) + '"


Also i tried to get the input values with javascript and they do appear in there, even inside the form so it's weird why it doesn't happen on codebehind... Maybe i have to access the form through ID or something?



EDIT2:



Here's another update, I tried to create a panel around the DIV where the table with the input fields is generated. So while it doesn't detect the input fields on the control list, it detects the table tags only. I think I have to access them through the table somehow










share|improve this question
















this is the html table generated based on an input by the user. I'm trying to get the values from the inputs but no luck. I even added runat="server" tag on them but nothing



$(function () {
$("[id*=txtNumComponentes]").keyup(function () {

$('#tblComponentesContainer').empty();
var txt = $("[id*=txtNumComponentes]").val();
debugger
if (txt != "" || txt != 0) {
var table = '<table id="tblComponentes" class="table table-bordered text-center mx-auto" style="width: 80%; background: #d9deed73; min-width:700px; border: 2px solid #212529;">' +
'<caption>Lista de componentes</caption>' +
'<thead class="thead-dark">' +
'<tr>' +
'<th scope="col" style="width:10px;">#</th>' +
'<th scope="col" style="width:100px;">Componente *</th>' +
'<th scope="col" style="width:100px;">Base *</th>' +
'<th scope="col" style="width:175px;">Comprimento *</th>' +
'</tr>' +
'</thead>' +
'<tbody>';



for (var i = 0; i < txt; i++) {
table += '<tr>' +
'<th scope="row">' + (i + 1) + '</th>' +
'<td><input type="text" runat="server" class="form-control form-control-sm rounded border border-dark" id="txtComponente' + (i + 1) + '" placeholder="Componente" style="width:200px;" /></td>' +
'<td><input type="text" runat="server" class="form-control form-control-sm rounded border border-dark" id="txtBase' + (i + 1) + '" placeholder="Base" style="width:200px;" /></td>' +
'<td>' +
'<div class="input-group input-group-sm">' +
'<input type="text" runat="server" class="form-control form-control-sm rounded border border-dark" id="txtComprimento' + (i + 1) + '" placeholder="Comprimento" style="width:50px;" />' +
'<div class="input-group-append">' +
'<span class="input-group-text border border-dark text-dark"><strong>mm</strong></span>' +
'</div>' +
'</div>'
'</td>' +
'</tr>';
}

table += '</tbody>' +
'</table>';

$('#tblComponentesContainer').html(table);
}
else
{
('#tblComponentesContainer').html();
}
})


});



here's what i have tested so far on codebehind and it returns null..



 protected void btnCriarArtigo_Click(object sender, EventArgs e)
{
if(Page.IsValid)
{
string n = Request.Form["txtComponente1"];

}
else
{

}
}


I have done something similar but it was all on server side using a repeater but I wanted to do it on client side cause it's faster to generate the fields. I have addded method="POST" to my form on masterpage too but didn't help



EDIT:



I also added name attribute but still getting null value



string n = Request.Form["Componente1"];
string a = Request.Form["Base1"];
string b = Request.Form["Comprimento1"];


I just added name like this to each input



name="Base' + (i + 1) + '"


Also i tried to get the input values with javascript and they do appear in there, even inside the form so it's weird why it doesn't happen on codebehind... Maybe i have to access the form through ID or something?



EDIT2:



Here's another update, I tried to create a panel around the DIV where the table with the input fields is generated. So while it doesn't detect the input fields on the control list, it detects the table tags only. I think I have to access them through the table somehow







javascript asp.net






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 1 at 22:08







Jackal

















asked Jan 1 at 21:26









JackalJackal

11410




11410













  • Try putting "name" attribute in the input element, I believe that should work.

    – dime2lo
    Jan 1 at 21:42











  • Updated my answer. I did this and still getting null. I can't even find them on the form usign debug

    – Jackal
    Jan 1 at 21:46





















  • Try putting "name" attribute in the input element, I believe that should work.

    – dime2lo
    Jan 1 at 21:42











  • Updated my answer. I did this and still getting null. I can't even find them on the form usign debug

    – Jackal
    Jan 1 at 21:46



















Try putting "name" attribute in the input element, I believe that should work.

– dime2lo
Jan 1 at 21:42





Try putting "name" attribute in the input element, I believe that should work.

– dime2lo
Jan 1 at 21:42













Updated my answer. I did this and still getting null. I can't even find them on the form usign debug

– Jackal
Jan 1 at 21:46







Updated my answer. I did this and still getting null. I can't even find them on the form usign debug

– Jackal
Jan 1 at 21:46














1 Answer
1






active

oldest

votes


















0














I managed to solve the problem but it took a bit to figure out what was the cause. Apparently you need a container that you can render on the server side, like an asp net table already declared on the page and that's what I did rather than generate a html table with an ID which i don't think it's possible to detect and access the inside in code behind...



So if anyone is trying to get dynamic input inside a table like me, this is the way to go.



<asp:Table  ID="tblComponentes" runat="server" Caption="Tabela de Componentes" 
Width="60%" CssClass="mx-auto componentes-table-invisible
ComponentesContainer table table-bordered text-center"
BorderColor="#212529" BorderWidth="2" BorderStyle="Solid" BackColor="#eaecf1" Style="min-width:700px;">

<asp:TableHeaderRow runat="server" CssClass="thead-dark" TableSection="TableHeader">
<asp:TableHeaderCell Scope="Column" Width="5%" runat="server">#</asp:TableHeaderCell>
<asp:TableHeaderCell Scope="Column" Width="25%" runat="server">Componente *</asp:TableHeaderCell>
<asp:TableHeaderCell Scope="Column" Width="25%" runat="server">Base *</asp:TableHeaderCell>
<asp:TableHeaderCell Scope="Column" Width="20%" runat="server">Comprimento *</asp:TableHeaderCell>
</asp:TableHeaderRow>
<asp:TableRow TableSection="TableBody" runat="server">
// Dynamic content goes here
</asp:TableRow>

</asp:Table>


Here is the changed javascript file to generate the table rows and cells with input fields



$(function () {

$("[id*=txtNumComponentes]").keyup(function () {

debugger
var ComponentesTable = $('.ComponentesContainer');

$('.ComponentesContainer > tbody').empty();
var txt = $("[id*=txtNumComponentes]").val();

if (txt != "" || txt != 0) {

ComponentesTable.removeClass('componentes-table-invisible');
var table;

for (var i = 0; i < txt; i++) {
table += '<tr>' +
'<td >' + (i + 1) + '</td>' +
'<td><input type="text" runat="server" name="Componente' + (i + 1) + '" class="form-control form-control-sm rounded border border-dark" id="txtComponente' + (i + 1) + '" placeholder="Componente"/></td>' +
'<td><input type="text" runat="server" name="Base' + (i + 1) + '" class="form-control form-control-sm rounded border border-dark" id="txtBase' + (i + 1) + '" placeholder="Base" /></td>' +
'<td>' +
'<div class="input-group input-group-sm">' +
'<input type="text" runat="server" name="Comprimento' + (i + 1) + '" class="form-control form-control-sm rounded border border-dark" id="txtComprimento' + (i + 1) + '" placeholder="Comprimento" />' +
'<div class="input-group-append">' +
'<span class="input-group-text border border-dark text-dark"><strong>mm</strong></span>' +
'</div>' +
'</div>' +
'</td>' +
'</tr>';
}

$('.ComponentesContainer > tbody').append(table);
}
else {
ComponentesTable.addClass('componentes-table-invisible');
}
})


});



It's important to use the table and point to it's tbody as the jquery selector to inject the html cause doing it directly on the TableBody section will just add a row inside a row.



And now I can get the values of these



string n = Request.Form["Componente1"];
string a = Request.Form["Base1"];
string b = Request.Form["Comprimento1"];


Also if table has no data it will show the header, so just make a class with display:none and add to it and remove it with Jquery when there's input on the table generator field






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53999063%2fget-value-from-dynamic-input-text-html-in-codebehind-asp-net%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    I managed to solve the problem but it took a bit to figure out what was the cause. Apparently you need a container that you can render on the server side, like an asp net table already declared on the page and that's what I did rather than generate a html table with an ID which i don't think it's possible to detect and access the inside in code behind...



    So if anyone is trying to get dynamic input inside a table like me, this is the way to go.



    <asp:Table  ID="tblComponentes" runat="server" Caption="Tabela de Componentes" 
    Width="60%" CssClass="mx-auto componentes-table-invisible
    ComponentesContainer table table-bordered text-center"
    BorderColor="#212529" BorderWidth="2" BorderStyle="Solid" BackColor="#eaecf1" Style="min-width:700px;">

    <asp:TableHeaderRow runat="server" CssClass="thead-dark" TableSection="TableHeader">
    <asp:TableHeaderCell Scope="Column" Width="5%" runat="server">#</asp:TableHeaderCell>
    <asp:TableHeaderCell Scope="Column" Width="25%" runat="server">Componente *</asp:TableHeaderCell>
    <asp:TableHeaderCell Scope="Column" Width="25%" runat="server">Base *</asp:TableHeaderCell>
    <asp:TableHeaderCell Scope="Column" Width="20%" runat="server">Comprimento *</asp:TableHeaderCell>
    </asp:TableHeaderRow>
    <asp:TableRow TableSection="TableBody" runat="server">
    // Dynamic content goes here
    </asp:TableRow>

    </asp:Table>


    Here is the changed javascript file to generate the table rows and cells with input fields



    $(function () {

    $("[id*=txtNumComponentes]").keyup(function () {

    debugger
    var ComponentesTable = $('.ComponentesContainer');

    $('.ComponentesContainer > tbody').empty();
    var txt = $("[id*=txtNumComponentes]").val();

    if (txt != "" || txt != 0) {

    ComponentesTable.removeClass('componentes-table-invisible');
    var table;

    for (var i = 0; i < txt; i++) {
    table += '<tr>' +
    '<td >' + (i + 1) + '</td>' +
    '<td><input type="text" runat="server" name="Componente' + (i + 1) + '" class="form-control form-control-sm rounded border border-dark" id="txtComponente' + (i + 1) + '" placeholder="Componente"/></td>' +
    '<td><input type="text" runat="server" name="Base' + (i + 1) + '" class="form-control form-control-sm rounded border border-dark" id="txtBase' + (i + 1) + '" placeholder="Base" /></td>' +
    '<td>' +
    '<div class="input-group input-group-sm">' +
    '<input type="text" runat="server" name="Comprimento' + (i + 1) + '" class="form-control form-control-sm rounded border border-dark" id="txtComprimento' + (i + 1) + '" placeholder="Comprimento" />' +
    '<div class="input-group-append">' +
    '<span class="input-group-text border border-dark text-dark"><strong>mm</strong></span>' +
    '</div>' +
    '</div>' +
    '</td>' +
    '</tr>';
    }

    $('.ComponentesContainer > tbody').append(table);
    }
    else {
    ComponentesTable.addClass('componentes-table-invisible');
    }
    })


    });



    It's important to use the table and point to it's tbody as the jquery selector to inject the html cause doing it directly on the TableBody section will just add a row inside a row.



    And now I can get the values of these



    string n = Request.Form["Componente1"];
    string a = Request.Form["Base1"];
    string b = Request.Form["Comprimento1"];


    Also if table has no data it will show the header, so just make a class with display:none and add to it and remove it with Jquery when there's input on the table generator field






    share|improve this answer




























      0














      I managed to solve the problem but it took a bit to figure out what was the cause. Apparently you need a container that you can render on the server side, like an asp net table already declared on the page and that's what I did rather than generate a html table with an ID which i don't think it's possible to detect and access the inside in code behind...



      So if anyone is trying to get dynamic input inside a table like me, this is the way to go.



      <asp:Table  ID="tblComponentes" runat="server" Caption="Tabela de Componentes" 
      Width="60%" CssClass="mx-auto componentes-table-invisible
      ComponentesContainer table table-bordered text-center"
      BorderColor="#212529" BorderWidth="2" BorderStyle="Solid" BackColor="#eaecf1" Style="min-width:700px;">

      <asp:TableHeaderRow runat="server" CssClass="thead-dark" TableSection="TableHeader">
      <asp:TableHeaderCell Scope="Column" Width="5%" runat="server">#</asp:TableHeaderCell>
      <asp:TableHeaderCell Scope="Column" Width="25%" runat="server">Componente *</asp:TableHeaderCell>
      <asp:TableHeaderCell Scope="Column" Width="25%" runat="server">Base *</asp:TableHeaderCell>
      <asp:TableHeaderCell Scope="Column" Width="20%" runat="server">Comprimento *</asp:TableHeaderCell>
      </asp:TableHeaderRow>
      <asp:TableRow TableSection="TableBody" runat="server">
      // Dynamic content goes here
      </asp:TableRow>

      </asp:Table>


      Here is the changed javascript file to generate the table rows and cells with input fields



      $(function () {

      $("[id*=txtNumComponentes]").keyup(function () {

      debugger
      var ComponentesTable = $('.ComponentesContainer');

      $('.ComponentesContainer > tbody').empty();
      var txt = $("[id*=txtNumComponentes]").val();

      if (txt != "" || txt != 0) {

      ComponentesTable.removeClass('componentes-table-invisible');
      var table;

      for (var i = 0; i < txt; i++) {
      table += '<tr>' +
      '<td >' + (i + 1) + '</td>' +
      '<td><input type="text" runat="server" name="Componente' + (i + 1) + '" class="form-control form-control-sm rounded border border-dark" id="txtComponente' + (i + 1) + '" placeholder="Componente"/></td>' +
      '<td><input type="text" runat="server" name="Base' + (i + 1) + '" class="form-control form-control-sm rounded border border-dark" id="txtBase' + (i + 1) + '" placeholder="Base" /></td>' +
      '<td>' +
      '<div class="input-group input-group-sm">' +
      '<input type="text" runat="server" name="Comprimento' + (i + 1) + '" class="form-control form-control-sm rounded border border-dark" id="txtComprimento' + (i + 1) + '" placeholder="Comprimento" />' +
      '<div class="input-group-append">' +
      '<span class="input-group-text border border-dark text-dark"><strong>mm</strong></span>' +
      '</div>' +
      '</div>' +
      '</td>' +
      '</tr>';
      }

      $('.ComponentesContainer > tbody').append(table);
      }
      else {
      ComponentesTable.addClass('componentes-table-invisible');
      }
      })


      });



      It's important to use the table and point to it's tbody as the jquery selector to inject the html cause doing it directly on the TableBody section will just add a row inside a row.



      And now I can get the values of these



      string n = Request.Form["Componente1"];
      string a = Request.Form["Base1"];
      string b = Request.Form["Comprimento1"];


      Also if table has no data it will show the header, so just make a class with display:none and add to it and remove it with Jquery when there's input on the table generator field






      share|improve this answer


























        0












        0








        0







        I managed to solve the problem but it took a bit to figure out what was the cause. Apparently you need a container that you can render on the server side, like an asp net table already declared on the page and that's what I did rather than generate a html table with an ID which i don't think it's possible to detect and access the inside in code behind...



        So if anyone is trying to get dynamic input inside a table like me, this is the way to go.



        <asp:Table  ID="tblComponentes" runat="server" Caption="Tabela de Componentes" 
        Width="60%" CssClass="mx-auto componentes-table-invisible
        ComponentesContainer table table-bordered text-center"
        BorderColor="#212529" BorderWidth="2" BorderStyle="Solid" BackColor="#eaecf1" Style="min-width:700px;">

        <asp:TableHeaderRow runat="server" CssClass="thead-dark" TableSection="TableHeader">
        <asp:TableHeaderCell Scope="Column" Width="5%" runat="server">#</asp:TableHeaderCell>
        <asp:TableHeaderCell Scope="Column" Width="25%" runat="server">Componente *</asp:TableHeaderCell>
        <asp:TableHeaderCell Scope="Column" Width="25%" runat="server">Base *</asp:TableHeaderCell>
        <asp:TableHeaderCell Scope="Column" Width="20%" runat="server">Comprimento *</asp:TableHeaderCell>
        </asp:TableHeaderRow>
        <asp:TableRow TableSection="TableBody" runat="server">
        // Dynamic content goes here
        </asp:TableRow>

        </asp:Table>


        Here is the changed javascript file to generate the table rows and cells with input fields



        $(function () {

        $("[id*=txtNumComponentes]").keyup(function () {

        debugger
        var ComponentesTable = $('.ComponentesContainer');

        $('.ComponentesContainer > tbody').empty();
        var txt = $("[id*=txtNumComponentes]").val();

        if (txt != "" || txt != 0) {

        ComponentesTable.removeClass('componentes-table-invisible');
        var table;

        for (var i = 0; i < txt; i++) {
        table += '<tr>' +
        '<td >' + (i + 1) + '</td>' +
        '<td><input type="text" runat="server" name="Componente' + (i + 1) + '" class="form-control form-control-sm rounded border border-dark" id="txtComponente' + (i + 1) + '" placeholder="Componente"/></td>' +
        '<td><input type="text" runat="server" name="Base' + (i + 1) + '" class="form-control form-control-sm rounded border border-dark" id="txtBase' + (i + 1) + '" placeholder="Base" /></td>' +
        '<td>' +
        '<div class="input-group input-group-sm">' +
        '<input type="text" runat="server" name="Comprimento' + (i + 1) + '" class="form-control form-control-sm rounded border border-dark" id="txtComprimento' + (i + 1) + '" placeholder="Comprimento" />' +
        '<div class="input-group-append">' +
        '<span class="input-group-text border border-dark text-dark"><strong>mm</strong></span>' +
        '</div>' +
        '</div>' +
        '</td>' +
        '</tr>';
        }

        $('.ComponentesContainer > tbody').append(table);
        }
        else {
        ComponentesTable.addClass('componentes-table-invisible');
        }
        })


        });



        It's important to use the table and point to it's tbody as the jquery selector to inject the html cause doing it directly on the TableBody section will just add a row inside a row.



        And now I can get the values of these



        string n = Request.Form["Componente1"];
        string a = Request.Form["Base1"];
        string b = Request.Form["Comprimento1"];


        Also if table has no data it will show the header, so just make a class with display:none and add to it and remove it with Jquery when there's input on the table generator field






        share|improve this answer













        I managed to solve the problem but it took a bit to figure out what was the cause. Apparently you need a container that you can render on the server side, like an asp net table already declared on the page and that's what I did rather than generate a html table with an ID which i don't think it's possible to detect and access the inside in code behind...



        So if anyone is trying to get dynamic input inside a table like me, this is the way to go.



        <asp:Table  ID="tblComponentes" runat="server" Caption="Tabela de Componentes" 
        Width="60%" CssClass="mx-auto componentes-table-invisible
        ComponentesContainer table table-bordered text-center"
        BorderColor="#212529" BorderWidth="2" BorderStyle="Solid" BackColor="#eaecf1" Style="min-width:700px;">

        <asp:TableHeaderRow runat="server" CssClass="thead-dark" TableSection="TableHeader">
        <asp:TableHeaderCell Scope="Column" Width="5%" runat="server">#</asp:TableHeaderCell>
        <asp:TableHeaderCell Scope="Column" Width="25%" runat="server">Componente *</asp:TableHeaderCell>
        <asp:TableHeaderCell Scope="Column" Width="25%" runat="server">Base *</asp:TableHeaderCell>
        <asp:TableHeaderCell Scope="Column" Width="20%" runat="server">Comprimento *</asp:TableHeaderCell>
        </asp:TableHeaderRow>
        <asp:TableRow TableSection="TableBody" runat="server">
        // Dynamic content goes here
        </asp:TableRow>

        </asp:Table>


        Here is the changed javascript file to generate the table rows and cells with input fields



        $(function () {

        $("[id*=txtNumComponentes]").keyup(function () {

        debugger
        var ComponentesTable = $('.ComponentesContainer');

        $('.ComponentesContainer > tbody').empty();
        var txt = $("[id*=txtNumComponentes]").val();

        if (txt != "" || txt != 0) {

        ComponentesTable.removeClass('componentes-table-invisible');
        var table;

        for (var i = 0; i < txt; i++) {
        table += '<tr>' +
        '<td >' + (i + 1) + '</td>' +
        '<td><input type="text" runat="server" name="Componente' + (i + 1) + '" class="form-control form-control-sm rounded border border-dark" id="txtComponente' + (i + 1) + '" placeholder="Componente"/></td>' +
        '<td><input type="text" runat="server" name="Base' + (i + 1) + '" class="form-control form-control-sm rounded border border-dark" id="txtBase' + (i + 1) + '" placeholder="Base" /></td>' +
        '<td>' +
        '<div class="input-group input-group-sm">' +
        '<input type="text" runat="server" name="Comprimento' + (i + 1) + '" class="form-control form-control-sm rounded border border-dark" id="txtComprimento' + (i + 1) + '" placeholder="Comprimento" />' +
        '<div class="input-group-append">' +
        '<span class="input-group-text border border-dark text-dark"><strong>mm</strong></span>' +
        '</div>' +
        '</div>' +
        '</td>' +
        '</tr>';
        }

        $('.ComponentesContainer > tbody').append(table);
        }
        else {
        ComponentesTable.addClass('componentes-table-invisible');
        }
        })


        });



        It's important to use the table and point to it's tbody as the jquery selector to inject the html cause doing it directly on the TableBody section will just add a row inside a row.



        And now I can get the values of these



        string n = Request.Form["Componente1"];
        string a = Request.Form["Base1"];
        string b = Request.Form["Comprimento1"];


        Also if table has no data it will show the header, so just make a class with display:none and add to it and remove it with Jquery when there's input on the table generator field







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 1 at 23:58









        JackalJackal

        11410




        11410
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53999063%2fget-value-from-dynamic-input-text-html-in-codebehind-asp-net%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Mossoró

            Error while reading .h5 file using the rhdf5 package in R

            Pushsharp Apns notification error: 'InvalidToken'