Composite control with dynamic composite controls

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sleigh

    Composite control with dynamic composite controls

    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 CreateChildCont rols 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.Componen tModel;
    using System.Componen tModel.Design;
    using System.Data;
    using System.Data.Sql Client;
    using System.Drawing;
    using System.Web.UI;
    using System.Web.UI.W ebControls;
    [assembly:TagPre fix("MYControls .ServerControls ", "MYControls ")]

    namespace MYControls.Serv erControls
    {

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

    private string sCon = "server=somerse rver;database=s omedbase;User
    ID=auser;pwd=ap assword";
    private System.Data.Sql Client.SqlConne ction con = new
    SqlConnection() ;

    private Panel _pnlContainer;
    private Label _lblHeader;

    private string m_SectionCd;
    private int m_InstitutionID ;

    private string LabelCssClass = "FieldLabel ";
    private string TextBoxCssClass = "TextBoxTreatme nt";

    #endregion

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

    }
    }

    [
    Bindable(false) ,
    Category("Appea rance"),
    DefaultValue("" ),
    Description("Th e InstitutionID for this application")
    ]
    public int FormInstitution ID
    {
    get
    {
    return m_InstitutionID ;
    }
    set
    {
    m_InstitutionID = value;
    }
    }

    #endregion Properties delegated to child controls

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

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

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

    this.Controls.A dd(this._lblHea der);
    this.Controls.A dd(this._pnlCon tainer);
    }

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

    AddAttributesTo Render(writer);

    writer.AddAttri bute(HtmlTextWr iterAttribute.W idth, "500");
    writer.AddAttri bute(HtmlTextWr iterAttribute.C lass,this.m_Tab leCssClass);
    writer.AddAttri bute(HtmlTextWr iterAttribute.C ellpadding,this .m_TableCellpad ding.ToString() );
    writer.AddAttri bute(HtmlTextWr iterAttribute.C ellspacing,this .m_TableCellspa cing.ToString() );
    writer.RenderBe ginTag(HtmlText WriterTag.Table );
    writer.RenderBe ginTag(HtmlText WriterTag.Tr);

    writer.AddAttri bute(HtmlTextWr iterAttribute.A lign, "left");
    writer.RenderBe ginTag(HtmlText WriterTag.Td);
    this._lblHeader .RenderControl( writer);
    writer.RenderEn dTag(); // Td
    writer.RenderEn dTag(); // Tr

    writer.RenderBe ginTag(HtmlText WriterTag.Tr);
    writer.RenderBe ginTag(HtmlText WriterTag.Td);
    this._pnlContai ner.RenderContr ol(writer);
    writer.RenderEn dTag(); // Td

    writer.RenderEn dTag(); // Tr
    writer.RenderEn dTag(); // Table

    con.Close();
    }
    #endregion Overriden methods

    private void CreateIt()
    {
    string SectionName = this.GetSection Name();
    this._lblHeader .Text = SectionName;
    DataSet ds = this.GetSection Questions();

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

    private void CreateCCTextBox (DataRow dr)
    {
    ccTextBox ccTextBox = new ccTextBox();
    ccTextBox.Field TBID = dr["FieldName"].ToString();
    ccTextBox.Field TBCssClass = this.TextBoxCss Class;
    ccTextBox.Field LBL = dr["Question"].ToString();
    ccTextBox.Field LBLCssClass = this.LabelCssCl ass;

    this._pnlContai ner.Controls.Ad d(ccTextBox);
    }

    private DataSet GetSectionQuest ions()
    {
    DataSet ds = new DataSet();
    SqlDataAdapter da = new SqlDataAdapter( "sp_GetQuestion s_ForForm",
    con);
    da.SelectComman d.CommandType = CommandType.Sto redProcedure;

    da.SelectComman d.Parameters.Ad d("@Institution ID", SqlDbType.Int);
    da.SelectComman d.Parameters["@InstitutionID "].Value =
    this.m_Institut ionID;

    da.SelectComman d.Parameters.Ad d("@SectionCd ",
    SqlDbType.VarCh ar,250);
    da.SelectComman d.Parameters["@SectionCd "].Value =
    "PRQ";//this.m_SectionC d;

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

    return ds;
    }

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

    SqlCommand cmd = new SqlCommand("sp_ GetSectionName" , con);
    cmd.CommandType = CommandType.Sto redProcedure;

    cmd.Parameters. Add("@SectionCd ", SqlDbType.VarCh ar);
    cmd.Parameters["@SectionCd "].Value = this.m_SectionC d;

    SqlParameter SectionName = cmd.Parameters. Add("@SectionNa me",
    SqlDbType.VarCh ar, 250);
    SectionName.Dir ection = ParameterDirect ion.Output;

    cmd.ExecuteNonQ uery();

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

    cmd.Dispose();
    return Name;
    }



    #region Overriden properties
    public override ControlCollecti on Controls
    {
    get
    {
    EnsureChildCont rols();
    return base.Controls;
    }
    }
    #endregion Overriden properties

    }
    }


    Posted Via Usenet.com Premium Usenet Newsgroup Services
    ----------------------------------------------------------
    ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
    ----------------------------------------------------------
    Best Usenet Service Providers 2025 ranked by Newsgroup Access Newsservers, Usenet Search, Features & Free Trial. Add VPN for privacy.

  • sleigh

    #2
    Never mind

    Issue solved.


    Posted Via Usenet.com Premium Usenet Newsgroup Services
    ----------------------------------------------------------
    ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
    ----------------------------------------------------------
    Best Usenet Service Providers 2025 ranked by Newsgroup Access Newsservers, Usenet Search, Features & Free Trial. Add VPN for privacy.

    Comment

    Working...