How do I get the data typed in the TextBox that was dynamically created?

2

I have created some TextBox controls dynamically in my Code Behind and need to retrieve the values in a new method, however I can not use% changed% in a new method.

        try
        {
            DataTable tbDadosAux =// Método para obter dados;
            DataTable tbDados = //filtra dados

            qtd.Value = tbDados.Rows.Count.ToString();

            table1.HorizontalAlign = HorizontalAlign.Center;
            table1.CssClass = ("table table-bordered table-hover table-striped");


            if (tbDados.Rows.Count !=0)
            {
                 tabela1.Visible = true;
            }

            for (int i = 0; i < tbDados.Rows.Count; i++)
            {

                TableRow linha = new TableRow();
                TableCell c1 = new TableCell();
                TableCell c2 = new TableCell();
                TableCell c3 = new TableCell();

                Label lblPesNm = new Label();
                TextBox txtSeqNum = new TextBox();
                HiddenField hdPesCodComp = new HiddenField();

                c1.Text = tbDados.Rows[i]["RETORNO"].ToString();
                c1.HorizontalAlign = HorizontalAlign.Left;

                txtSeqNum.ID = "txtSeqNum" + (i + 1);
                txtSeqNum.Text = tbDados.Rows[i]["SEQUENCIAL"].ToString();
                c2.Controls.Add(txtSeqNum);
                c2.HorizontalAlign = HorizontalAlign.Left;

                table1.HorizontalAlign = HorizontalAlign.Center;
                table1.CssClass = ("table table-bordered table-hover table-striped");

                linha.Cells.Add(c1);
                linha.Cells.Add(c2);


                table1.Rows.Add(linha);
            }


        }
        catch (Exception ex)
        {
            if (tran.Connection != null)
            {
                tran.Rollback();
                conn.Close();
               // Erro.InnerHtml = "Ocorreu o seguinte erro: " + ex.Message;
            }
        }
        finally
        {
            if (tran.Connection != null)
            {
                tran.Commit();
                conn.Close();
            }
        }
    
asked by anonymous 13.10.2015 / 18:28

2 answers

0

As reported in your comment with Request.Form you can recover, but it is not the right way for features like this, use components like Repeater that are lightweight and can bring much more functionality.

The way you use the question will not succeed, because you will not be able to persist the data dynamically like this. You know what happens when you hit a Submit button, that data will disappear (although I do not see your code is a curry error), so I decided to create a very simple example.

Example:

Create a Repeater like this within an ASPX page:

<%@ Page Language="C#" AutoEventWireup="true" 
       CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication3.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:Repeater runat="server" ID="RptDados">
            <HeaderTemplate>
                <table>
                    <tr>
                        <th>Id</th>
                        <th>Nome</th>
                    </tr>
            </HeaderTemplate>
            <ItemTemplate>
                <tr>
                    <td>
                        <asp:HiddenField ID="HId" Value='<%#Eval("Id") %>' runat="server" />
                        <asp:Literal runat="server" Text='<%#Eval("Id") %>'></asp:Literal>
                    </td>
                    <td>
                        <asp:TextBox ID="TNome" runat="server" Text='<%#Eval("Nome") %>'></asp:TextBox>
                    </td>
                </tr>
            </ItemTemplate>
            <FooterTemplate>
                </table>
            </FooterTemplate>
        </asp:Repeater>
        <asp:Button Text="Enviar" runat="server" OnClick="BtnEnviar_Click"  ID="BtnEnviar" />
        <asp:Label Text="" runat="server" ID="LblResposta" />
    </form>
</body>
</html>

Note that in% w / o% I have the fields that will receive the values dynamically, that is, the items will be repeated according to the amount of elements I'm going to send ItemTemplate and Repeater will load the items. After you create it on% wrapper Send it will trigger an event that will load the changes in this% wrapper wrapper . Check this in the code below:

In the code:

using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication3
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {                
                Carregar_Repeater();                
            }
        }

        public void Carregar_Repeater()
        {
            DataTable _DataTable = new DataTable();
            _DataTable.Columns.Add("Id", typeof(int));
            _DataTable.Columns.Add("Nome", typeof(string));

            DataRow _Row = null;
            _Row = _DataTable.NewRow();
            _Row["Id"] = 1;
            _Row["Nome"] = "Nome 1";
            _DataTable.Rows.Add(_Row);

            _Row = _DataTable.NewRow();
            _Row["Id"] = 2;
            _Row["Nome"] = "Nome 2";
            _DataTable.Rows.Add(_Row);


            RptDados.DataSource = _DataTable;
            RptDados.DataBind();
        }

        protected void BtnEnviar_Click(object sender, EventArgs e)
        {
            LblResposta.Text = string.Empty;
            foreach (RepeaterItem _Item in RptDados.Items)
            {
                HiddenField HId = _Item.FindControl("HId") as HiddenField;
                TextBox TNome = _Item.FindControl("TNome") as TextBox;

                LblResposta.Text += string.Format("<p>{0} {1}</p>", HId.Value, TNome.Text);
            }
        }
    }
}

In the LoadRepeater method, I only pass the data to DataTable of Button and I command loading the data up (can be any type of Collection ( Label , DataSource , etc , but in your case it was Repeater ))

With this example you will be able to understand I believe the process itself ...

    
13.10.2015 / 19:15
0

There is this way that does not scan the repeater and goes straight to the line that the user selects.

protected void Button_Atualizar_OnClick(object sender, EventArgs e) 
{
    Control botao = (Control)sender; 
    RepeaterItem item =(RepeaterItem)botao.Parent;

    TextBox txt = (TextBox)item.FindControl("TextBox2");
    string titulo = txt.Text;

    TextBox txt1 = (TextBox)item.FindControl("TextBox3");
    string descricao = txt1.Text;

    Label lbl1 = (Label)item.FindControl("Label33");
    string cod_item = lbl1.Text; 
    ...
}
    
16.04.2018 / 14:48