Composite control with dynamic composite controls

S

sleigh

Hello,

I'm building a web application that will build a dynamic form based
upon questions in a database. This form will have several different
sections that consist of a panel containing one to many questions.

To keep it simple, I'll describe the basics of what I'm trying to
design.

I've created a TextBox composite control that consists of a label for
the question being asked, and a textbox for the input. (There's
validation as well, but I'm keeping this down to basics.)

I've also created a Panel composite control that consists of a label
for the header and the panel which will act as the container for the
questions that will be asked.

I've created a method in the Panel control that gets the questions
from the database and dynamically adds the new Composite TextBox
control to the Panel Controls Collection.

I've tried the following:

1) I've called this method from within CreateChildControls to try and
recreate the the dynamic controls since Controls.Clear() is called at
the top of this method anytime EnsureChild controls is called, but
this is not working properly.
2) I've called this method from the Render method which renders the
composite controls fine, but this doesn't seem like the proper place
to call this method from either, and this is also problematic.
3) Beat head on wall, tried many other panic tactics, but all I have
now is a headache.

Problem is, I can't see any of the dynamically created controls in the
Panel's controls collection so that I can get the values back out of
them upon submit.

Any help is appreciated.
Thanks.


using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web.UI;
using System.Web.UI.WebControls;
[assembly:TagPrefix("MYControls.ServerControls", "MYControls")]

namespace MYControls.ServerControls
{

public class ccPanel : WebControl, INamingContainer
{
#region Page Controls Declaration

private string sCon = "server=somerserver;database=somedbase;User
ID=auser;pwd=apassword";
private System.Data.SqlClient.SqlConnection con = new
SqlConnection();

private Panel _pnlContainer;
private Label _lblHeader;

private string m_SectionCd;
private int m_InstitutionID;

private string LabelCssClass = "FieldLabel";
private string TextBoxCssClass = "TextBoxTreatment";

#endregion

#region Properties delegated to child controls
[
Bindable(false),
Category("Appearance"),
DefaultValue(""),
Description("The Section of this panel for the application")
]
public string FormSectionCd
{
get
{
return m_SectionCd;
}
set
{
m_SectionCd = value;

}
}

[
Bindable(false),
Category("Appearance"),
DefaultValue(""),
Description("The InstitutionID for this application")
]
public int FormInstitutionID
{
get
{
return m_InstitutionID;
}
set
{
m_InstitutionID = value;
}
}

#endregion Properties delegated to child controls

#region Overriden methods
protected override void CreateChildControls()
{
Controls.Clear();

this._lblHeader = new Label();
this._lblHeader.Text = "Test Label";
this._lblHeader.CssClass = LabelCssClass;

this._pnlContainer = new Panel();
this._pnlContainer.CssClass = TextBoxCssClass;
this._pnlContainer.ID = "TestPanel";
this._pnlContainer.Width = Unit.Pixel(500);

this.Controls.Add(this._lblHeader);
this.Controls.Add(this._pnlContainer);
}

/// <summary>
/// Method that actually renders the control in a nicely formatted
table.
/// </summary>
/// <param name="writer">HtmlTextWriter</param>
protected override void Render(HtmlTextWriter writer)
{
//The beginning of the dynamic form building.
this.CreateIt();

AddAttributesToRender(writer);

writer.AddAttribute(HtmlTextWriterAttribute.Width, "500");
writer.AddAttribute(HtmlTextWriterAttribute.Class,this.m_TableCssClass);
writer.AddAttribute(HtmlTextWriterAttribute.Cellpadding,this.m_TableCellpadding.ToString());
writer.AddAttribute(HtmlTextWriterAttribute.Cellspacing,this.m_TableCellspacing.ToString());
writer.RenderBeginTag(HtmlTextWriterTag.Table);
writer.RenderBeginTag(HtmlTextWriterTag.Tr);

writer.AddAttribute(HtmlTextWriterAttribute.Align, "left");
writer.RenderBeginTag(HtmlTextWriterTag.Td);
this._lblHeader.RenderControl(writer);
writer.RenderEndTag(); // Td
writer.RenderEndTag(); // Tr

writer.RenderBeginTag(HtmlTextWriterTag.Tr);
writer.RenderBeginTag(HtmlTextWriterTag.Td);
this._pnlContainer.RenderControl(writer);
writer.RenderEndTag(); // Td

writer.RenderEndTag(); // Tr
writer.RenderEndTag(); // Table

con.Close();
}
#endregion Overriden methods

private void CreateIt()
{
string SectionName = this.GetSectionName();
this._lblHeader.Text = SectionName;
DataSet ds = this.GetSectionQuestions();

if (ds.Tables["Questions"].Rows.Count == 0)
{
// this._pnlContainer.Visible = false;
this._lblHeader.Text += " ----- No Questions. This is INVISIBLE";
}
else
{
foreach (DataRow dr in ds.Tables["Questions"].Rows)
{
switch (dr["QuestionTypeCd"].ToString().ToLower())
{
case "tb":
this.CreateCCTextBox(dr);
break;
}
}
}
}

private void CreateCCTextBox(DataRow dr)
{
ccTextBox ccTextBox = new ccTextBox();
ccTextBox.FieldTBID = dr["FieldName"].ToString();
ccTextBox.FieldTBCssClass = this.TextBoxCssClass;
ccTextBox.FieldLBL = dr["Question"].ToString();
ccTextBox.FieldLBLCssClass = this.LabelCssClass;

this._pnlContainer.Controls.Add(ccTextBox);
}

private DataSet GetSectionQuestions()
{
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter("sp_GetQuestions_ForForm",
con);
da.SelectCommand.CommandType = CommandType.StoredProcedure;

da.SelectCommand.Parameters.Add("@InstitutionID", SqlDbType.Int);
da.SelectCommand.Parameters["@InstitutionID"].Value =
this.m_InstitutionID;

da.SelectCommand.Parameters.Add("@SectionCd",
SqlDbType.VarChar,250);
da.SelectCommand.Parameters["@SectionCd"].Value =
"PRQ";//this.m_SectionCd;

da.Fill(ds, "Questions");
da.Dispose();

return ds;
}

private string GetSectionName()
{
con.ConnectionString = sCon;
con.Open();

SqlCommand cmd = new SqlCommand("sp_GetSectionName", con);
cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.Add("@SectionCd", SqlDbType.VarChar);
cmd.Parameters["@SectionCd"].Value = this.m_SectionCd;

SqlParameter SectionName = cmd.Parameters.Add("@SectionName",
SqlDbType.VarChar, 250);
SectionName.Direction = ParameterDirection.Output;

cmd.ExecuteNonQuery();

string Name = cmd.Parameters["@SectionName"].Value.ToString();

cmd.Dispose();
return Name;
}



#region Overriden properties
public override ControlCollection Controls
{
get
{
EnsureChildControls();
return base.Controls;
}
}
#endregion Overriden properties

}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top