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.
– Marciano.Andrade