Composite control with dynamic composite controls

Discussion in 'ASP .Net' started by sleigh, Feb 12, 2004.

  1. sleigh

    sleigh Guest

    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

    }
    }


    Posted Via Usenet.com Premium Usenet Newsgroup Services
    ----------------------------------------------------------
    ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
    ----------------------------------------------------------
    http://www.usenet.com
    sleigh, Feb 12, 2004
    #1
    1. Advertising

  2. sleigh

    sleigh Guest

    Never mind

    Issue solved.


    Posted Via Usenet.com Premium Usenet Newsgroup Services
    ----------------------------------------------------------
    ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
    ----------------------------------------------------------
    http://www.usenet.com
    sleigh, Feb 12, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Harry
    Replies:
    2
    Views:
    648
    Harry
    Apr 16, 2004
  2. Matt Sollars

    Composite control with dynamic controls depending on a property value

    Matt Sollars, Jul 27, 2004, in forum: ASP .Net Building Controls
    Replies:
    7
    Views:
    158
    Matt Sollars
    Jul 30, 2004
  3. Chad
    Replies:
    0
    Views:
    209
  4. Bryan
    Replies:
    2
    Views:
    205
    Bryan
    Jun 14, 2006
  5. Mike

    Composite vs non composite Controls

    Mike, Mar 10, 2005, in forum: ASP .Net Web Controls
    Replies:
    4
    Views:
    247
    Sundararajan
    Mar 11, 2005
Loading...

Share This Page