473,386 Members | 1,674 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 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
Nov 18 '05 #1
1 3126
Issue solved.
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Nov 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: dx | last post by:
I have the Microsoft Press: Developing Microsoft ASP.NET Server Controls and Components book. It's starting to shine some light on control development but there is something about composite...
2
by: Harry | last post by:
Hello, I have a composite WebControl that I'm dynamically instantiating at runtime using Reflection. When I create a new instance of my control I immediately iterate through it's child control...
3
by: Martin | last post by:
Hi, I have created a composite control that has a number of standard asp.net controls on it that can themselves cause postbacks. What i need to do in my composite control is to determine which...
4
by: Mark Olbert | last post by:
This involves a family of related, databound ASPNET2 composite controls. I've managed to arrange things so that the composite controls restore themselves from ViewState on postback after they're...
3
by: Beavis | last post by:
I hate to repost a message, but I am still at the same point where I was when I originally posted, and hopefully someone else will see this one... Ok, so I have gone off and documented the...
6
by: shapper | last post by:
Hello, I am working in a class library with various custom controls. In which cases should a control inherit Control, WebControl and CompositeControl classes? And when should a custom...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.