For Programmers: Free Programming Magazines  


Home > Archive > C# > March 2004 > ASP.NET dynamicaly created custom control not visible









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author ASP.NET dynamicaly created custom control not visible
Michel

2004-03-28, 10:28 pm

Hi all,

I have created a custom control which contains dropdownlist. I
allready use this custom control on several places without a problem.
But when I try to dynamically create it in my code behind, it is not
displayed on my page.

What I do: I have a datagrid on my page, which contains a list of
settings which has to be displayed. In this list, one the settings
uses my custom control to set and display that setting :


-------------------------------------------------------------------------

public void InstantiateIn(System.Web.UI.Control container)
{

System.Web.UI.WebControls.PlaceHolder l= new
System.Web.UI.WebControls.PlaceHolder();
l.DataBinding += new EventHandler(this.DoDataBinding);
container.Controls.Add(l);
} //InstantiateIn

public void DoDataBinding(object sender, EventArgs e)
{

p = (System.Web.UI.WebControls.PlaceHolder)sender;
DataGridItem container = (DataGridItem)p.NamingContainer;
DataRowView drv = (DataRowView)container.DataItem;

if (drv["Type"] != System.DBNull.Value)
{
string s = (string)drv["Type"];
switch(s.ToUpper())
{
case "TEXT":
// Create textbox
TextBox text = new TextBox();
text.CssClass = "BackOfficeStyle_C";
text.MaxLength = (int) drv["Lenght"];
string t = (string) drv["Value"];
int l = (int) drv["Lenght"];
if (t.Length > l)
{text.Text = t.Remove(l,t.Length - l);}
else
{text.Text = t;}
text.Width = 300;
text.ID= "Setting_" + (string)drv["Name"];
// Add the textboc
p.Controls.Add(text);
break;
case "CUSTOMDROPDOWN":
// Create the custom control
CustomDropDownControl cddc = new CustomDropDownControl();
cddc.ID= "Setting_" + drv["Name"].ToString();
cddc.Visible = true;
// Add the CustomDropDownControl
p.Controls.Add(cddc);
break;
default:
// Create a literal
Literal literal = new Literal();
literal.Text = "Type not found: <b>" + (string)drv["Type"] +
"</b>";
literal.ID= "Setting_" + (string)drv["Name"];
// Add the literal
p.Controls.Add(literal);
break;
} //switch
} //if
} //DoDataBinding

-------------------------------------------------------------------------

Now when I add a setting of the type "TEXT", everything works fine,
without a glitch. But when I use the type "CUSTOMDROPDOWN", the
control is not displayed.
Strange thing is that I use the same control in my aspx, where it is
displayed correctly:

-------------------------------------------------------------------------

<%@ Register TagPrefix="uc2" TagName="CustomDropDownControl"
Src="../GenericModules/CustomDropDownControl.ascx" %>
<uc2:CustomDropDownControl id="CustomDropDownControl1" runat="server"
></uc2:CustomDropDownControl>


-------------------------------------------------------------------------


Anyone who has an idea why? All idea's are wellcome!

I'm looking at it for 2 day's now. And I need to get it to work as
soon as possible!

Thanks!


Michel
Dincer Ozturan

2004-03-28, 11:32 pm

Hi,

You can try to declare the custom control out of the method and in the
method just create a new instance. I think that'd work..


michel_mathijssen@hotmail.com (Michel) wrote in message news:<ab82e040.0403050843.5290156f@posting.google.com>...
> Hi all,
>
> I have created a custom control which contains dropdownlist. I
> allready use this custom control on several places without a problem.
> But when I try to dynamically create it in my code behind, it is not
> displayed on my page.
>
> What I do: I have a datagrid on my page, which contains a list of
> settings which has to be displayed. In this list, one the settings
> uses my custom control to set and display that setting :
>
>
> -------------------------------------------------------------------------
>
> public void InstantiateIn(System.Web.UI.Control container)
> {
>
> System.Web.UI.WebControls.PlaceHolder l= new
> System.Web.UI.WebControls.PlaceHolder();
> l.DataBinding += new EventHandler(this.DoDataBinding);
> container.Controls.Add(l);
> } //InstantiateIn
>
> public void DoDataBinding(object sender, EventArgs e)
> {
>
> p = (System.Web.UI.WebControls.PlaceHolder)sender;
> DataGridItem container = (DataGridItem)p.NamingContainer;
> DataRowView drv = (DataRowView)container.DataItem;
>
> if (drv["Type"] != System.DBNull.Value)
> {
> string s = (string)drv["Type"];
> switch(s.ToUpper())
> {
> case "TEXT":
> // Create textbox
> TextBox text = new TextBox();
> text.CssClass = "BackOfficeStyle_C";
> text.MaxLength = (int) drv["Lenght"];
> string t = (string) drv["Value"];
> int l = (int) drv["Lenght"];
> if (t.Length > l)
> {text.Text = t.Remove(l,t.Length - l);}
> else
> {text.Text = t;}
> text.Width = 300;
> text.ID= "Setting_" + (string)drv["Name"];
> // Add the textboc
> p.Controls.Add(text);
> break;
> case "CUSTOMDROPDOWN":
> // Create the custom control
> CustomDropDownControl cddc = new CustomDropDownControl();
> cddc.ID= "Setting_" + drv["Name"].ToString();
> cddc.Visible = true;
> // Add the CustomDropDownControl
> p.Controls.Add(cddc);
> break;
> default:
> // Create a literal
> Literal literal = new Literal();
> literal.Text = "Type not found: <b>" + (string)drv["Type"] +
> "</b>";
> literal.ID= "Setting_" + (string)drv["Name"];
> // Add the literal
> p.Controls.Add(literal);
> break;
> } //switch
> } //if
> } //DoDataBinding
>
> -------------------------------------------------------------------------
>
> Now when I add a setting of the type "TEXT", everything works fine,
> without a glitch. But when I use the type "CUSTOMDROPDOWN", the
> control is not displayed.
> Strange thing is that I use the same control in my aspx, where it is
> displayed correctly:
>
> -------------------------------------------------------------------------
>
> <%@ Register TagPrefix="uc2" TagName="CustomDropDownControl"
> Src="../GenericModules/CustomDropDownControl.ascx" %>
> <uc2:CustomDropDownControl id="CustomDropDownControl1" runat="server"
>
> -------------------------------------------------------------------------
>
>
> Anyone who has an idea why? All idea's are wellcome!
>
> I'm looking at it for 2 day's now. And I need to get it to work as
> soon as possible!
>
> Thanks!
>
>
> Michel

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com