Problem accessing dynamically generated ASP.NET field data

Asked

Viewed 391 times

0

I am developing a website where there is an administrator for contact forms, IE, 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 code below:

frmContacto.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 form code-Behind:

frmContacto.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 just 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 didn’t work out so well.

Any assistance I thank!

  • I’m running out of time now, but there’s one thing with Asp.net that makes dynamically created controls not present in a postback, I have to do some research on it... tomorrow I post some relevant links.

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 it is necessary to identify the controls individually, it can be done by defining the property Name (or even ID) exclusive to each of them.

  • I tried to do as you put it, but in if it’s always returning false, am I missing something?

  • Maybe when referencing Forms, try switching to this.Controls in the foreach.

  • I tried, but also did not give...I think I’m missing something, I was reading about the life cycle of Asp.NET pages but I do not know if it has much to do with this...when I use the foreach it simply brings the field as null

Browser other questions tagged

You are not signed in. Login or sign up in order to post.