Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Composite control with dynamic composite controls

Reply
Thread Tools

Composite control with dynamic composite controls

 
 
sleigh
Guest
Posts: n/a
 
      02-12-2004
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.Cellpa dding,this.m_TableCellpadding.ToString());
writer.AddAttribute(HtmlTextWriterAttribute.Cellsp acing,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
 
Reply With Quote
 
 
 
 
sleigh
Guest
Posts: n/a
 
      02-12-2004
Issue solved.


Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Finding the values of Dynamic Controls in a Composite Control Bryan ASP .Net Building Controls 2 06-14-2006 08:44 PM
Losing Composite Control property that another Composite Control ... Chad ASP .Net Building Controls 0 02-01-2005 09:01 PM
Composite control with dynamic controls depending on a property value Matt Sollars ASP .Net Building Controls 7 07-30-2004 12:49 PM
Possible to create a composite control that has a child control that is a validator that validates the composite control itself? Jonathan Eric Miller ASP .Net Building Controls 2 07-22-2004 10:58 PM
Dynamic Composite Controls And Their Child Control Collection Harry ASP .Net 2 04-16-2004 07:15 PM



Advertisments