Problem accessing data from dynamically generated ASP.NET fields

0

I'm developing a website where you have an administrator for the contact forms, that is, the client can create fields for this form. So far so good, the registration works well and I can draw the form correctly on the screen, as per the code below:

frmContact.aspx

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="frmContato.aspx.cs" Inherits="frmContato" EnableEventValidation="false" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<style>
    .lblNovo {
        font-family: Arial;
        font-weight: bold;
        font-size: 12px;
    }
</style>
<div id="forms" runat="server">
</div>

<table>
    <tr>
        <td>
            <asp:Label CssClass="lblNovo" ID="lblCurr" runat="server" Text="Anexe seu currículo: "></asp:Label></td>
        <td>
            <asp:FileUpload ID="fuCurriculo" runat="server" AllowMultiple="false" /></td>
    </tr>

    <tr>
        <td>
            <asp:Label CssClass="lblNovo" ID="lblFoto" runat="server" Text="Anexe sua foto: "></asp:Label></td>
        <td>
            <asp:FileUpload ID="fuFoto" runat="server" AllowMultiple="false" />
        </td>
    </tr>
</table>
<br />
<asp:Button ID="btnEnviaEmail" runat="server" Text="Enviar" OnClick="Button1_Click" />

Below is the code behind the form:

frmContact.aspx.cs

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Net.Mail;
using System.Threading;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class frmContato : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["tpSite"] != "3")
    {
        fuCurriculo.Visible = false;
        fuFoto.Visible = false;
        lblCurr.Visible = false;
        lblFoto.Visible = false;
    }

    if (!Page.IsPostBack)
    {
        SessaoAberta();
        SetInitialRow();
    }
}

private void SessaoAberta()
{
    try
    {
        if (Session["tpSite"] == null || Session["tpSite"] == "")
        {
            Response.Redirect("index.aspx");
        }
    }
    catch (Exception)
    {
        Response.Redirect("index.aspx");
    }
}

 private void SetInitialRow()
{
    string tpSite = Session["tpSite"].ToString();

    string tpcampo;
    string nomecampo;
    string assuntos;

    TextBox tx;
    RadioButton rdb;
    CheckBox chk;
    DropDownList ddl;
    Label lbl;



    string conString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

    string sql = "  SELECT idCampo,nomeCampo, tipoCampo, assuntos FROM [dbo].[camposFormulario] WHERE idTpSite = " + tpSite + " AND status = 1 ORDER BY ordem";

    SqlConnection cn = new SqlConnection(conString);
    SqlCommand cmd = new SqlCommand(sql, cn);
    SqlCommand cmd2 = new SqlCommand(sql, cn);

    cn.Open();

    SqlDataReader dreader = cmd.ExecuteReader();

    forms.Controls.Add(new LiteralControl("<table border='0'>"));

    while (dreader.Read())
    {
        tpcampo = dreader["tipoCampo"].ToString();
        nomecampo = dreader["nomeCampo"].ToString();
        assuntos = dreader["assuntos"].ToString();

        if (tpcampo == "1") //Textbox
        {
            lbl = new Label();
            lbl.Text = nomecampo;
            lbl.Style.Add("font-family", "Arial");

            tx = new TextBox();
            tx.ID = "txt";
            tx.Style.Add("font-family", "Arial");
            tx.Width = 300;

            forms.Controls.Add(new LiteralControl("<tr>"));
            forms.Controls.Add(new LiteralControl("<td align='right' width='300px'>"));
            forms.Controls.Add(lbl);
            forms.Controls.Add(new LiteralControl("</td>"));
            forms.Controls.Add(new LiteralControl("<td>"));
            forms.Controls.Add(tx);
            forms.Controls.Add(new LiteralControl("</td>"));
            forms.Controls.Add(new LiteralControl("</tr>"));

        }
        else if (tpcampo == "2") //Radio
        {
            string s = dreader["assuntos"].ToString();
            string[] itens = s.Split(',');

            lbl = new Label();
            lbl.Text = nomecampo;
            lbl.Style.Add("font-family", "Arial");

            rdb = new RadioButton();
            rdb.ID = "rdb";

            forms.Controls.Add(new LiteralControl("<tr>"));
            forms.Controls.Add(new LiteralControl("<td align='right'>"));
            forms.Controls.Add(lbl);
            forms.Controls.Add(new LiteralControl("</td>"));
            forms.Controls.Add(new LiteralControl("<td>"));
            foreach (string item in itens)
            {
                rdb = new RadioButton();
                rdb.Text = item;
                rdb.Style.Add("font-family", "Arial");
                forms.Controls.Add(rdb);
                forms.Controls.Add(new LiteralControl("<br />"));
            }
            forms.Controls.Add(new LiteralControl("</td>"));
            forms.Controls.Add(new LiteralControl("</tr>"));
        }
        else if (tpcampo == "3") //Check
        {
            string s = dreader["assuntos"].ToString();
            string[] itens = s.Split(',');

            lbl = new Label();
            lbl.Text = nomecampo;
            lbl.Style.Add("font-family", "Arial");

            chk = new CheckBox();
            chk.ID = "chk";


            forms.Controls.Add(new LiteralControl("<tr>"));
            forms.Controls.Add(new LiteralControl("<td align='right'>"));
            forms.Controls.Add(lbl);
            forms.Controls.Add(new LiteralControl("</td>"));
            forms.Controls.Add(new LiteralControl("<td>"));
            foreach (string item in itens)
            {
                chk = new CheckBox();
                chk.Text = item;
                chk.Style.Add("font-family", "Arial");
                forms.Controls.Add(chk);
                forms.Controls.Add(new LiteralControl("<br />"));
            }
            forms.Controls.Add(new LiteralControl("</td>"));
            forms.Controls.Add(new LiteralControl("</tr>"));
        } 
    }
    forms.Controls.Add(new LiteralControl("</table>"));
cn.Close();
}

The form is drawing right the way I need it, my question is how to capture this data after generation. I tried to do with a foreach by the controls but it did not work.

Any help I appreciate!

    
asked by anonymous 08.01.2015 / 15:46

1 answer

1

With foreach should work:

foreach (Control c in forms.Controls) 
{
    if (c is TextBox)
    {
        TextBox textBox = (TextBox)c;
        textBox.Text = "Texto"; //acesse aqui
    }
    else if (c is RadioButton)
    {
        RadioButton radioButton = (RadioButton)c;
    }
    else if (c is CheckBox)
    {
        CheckBox checkBox = (CheckBox)c;
    }
}

If you need to identify the controls individually, you can do this by setting the Name (or even ID ) property to each of them.

    
08.01.2015 / 17:10