473,666 Members | 2,053 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

dynamically adding user controls

Hi,
I am trying to dynamically add user controls on to my web form but for
some reason my form isnt displaying the user control.

form1.cs:
using System;
using System.Drawing;
using System.Collecti ons;
using System.Componen tModel;
using System.Windows. Forms;
using System.Data;

namespace WorldClock
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows. Forms.Form
{
private Point ptr;
private WorldClock.User Control1[] _userctrls;
/// <summary>
/// Required designer variable.
/// </summary>
private System.Componen tModel.Containe r components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeCompo nent();

//
// TODO: Add any constructor code after InitializeCompo nent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Disp ose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeCompo nent()
{
//
// Form1
//
this.AutoScaleB aseSize = new System.Drawing. Size(5, 13);
this.ClientSize = new System.Drawing. Size(704, 334);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHan dler(this.Form1 _Load);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run (new Form1());
}

private void Form1_Load(obje ct sender, System.EventArg s e)
{
ptr = new Point(0,0);

Hashtable hash;
hash =
(Hashtable)Syst em.Configuratio n.Configuration Settings.GetCon fig("Regions");
IDictionaryEnum erator en = hash.GetEnumera tor();
_userctrls = new UserControl1[hash.Count];
int i=0;
while(en.MoveNe xt())
{
_userctrls[i] = new
UserControl1(en .Key.ToString() ,en.Value.ToStr ing());
//_userctrls[i].Location = ptr;
//userctrls[i].Name = "usc" + i.ToString();
//ptr.Offset(_use rctrls[i].Width,0);
//_userctrls[i].Visible = true;

this.Controls.A dd(this._userct rls[i]);
}

}
}
}

usercontrol1.cs :
using System;
using System.Collecti ons;
using System.Componen tModel;
using System.Drawing;
using System.Data;
using System.Windows. Forms;

namespace WorldClock
{
/// <summary>
/// Summary description for UserControl1.
/// </summary>
public class UserControl1 : System.Windows. Forms.UserContr ol
{
private string city="";
private string minsfromMelbour ne="";

private System.Windows. Forms.Label label1;
private System.Windows. Forms.Label label2;
private System.Windows. Forms.Label label3;
/// <summary>
/// Required designer variable.
/// </summary>
private System.Componen tModel.Containe r components = null;

public UserControl1()
{
// This call is required by the Windows.Forms Form Designer.
InitializeCompo nent();

// TODO: Add any initialization after the InitializeCompo nent call
}

public UserControl1(st ring City, string MinsFromMelbour ne)
{
city = City;
minsfromMelbour ne = MinsFromMelbour ne;
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Disp ose();
}
}
base.Dispose( disposing );
}

#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeCompo nent()
{
this.label1 = new System.Windows. Forms.Label();
this.label2 = new System.Windows. Forms.Label();
this.label3 = new System.Windows. Forms.Label();
this.SuspendLay out();
//
// label1
//
this.label1.Loc ation = new System.Drawing. Point(24, 24);
this.label1.Nam e = "label1";
this.label1.Tab Index = 0;
this.label1.Tex t = "label1";
//
// label2
//
this.label2.Loc ation = new System.Drawing. Point(24, 64);
this.label2.Nam e = "label2";
this.label2.Tab Index = 1;
this.label2.Tex t = "label2";
//
// label3
//
this.label3.Loc ation = new System.Drawing. Point(24, 104);
this.label3.Nam e = "label3";
this.label3.Tab Index = 2;
this.label3.Tex t = "label3";
//
// UserControl1
//
this.Controls.A dd(this.label3) ;
this.Controls.A dd(this.label2) ;
this.Controls.A dd(this.label1) ;
this.Name = "UserContro l1";
this.Load += new System.EventHan dler(this.UserC ontrol1_Load);
this.ResumeLayo ut(false);

}
#endregion

private void UserControl1_Lo ad(object sender, System.EventArg s e)
{
label1.Text = city;
TimeSpan ts = new TimeSpan(0,0,in t.Parse(minsfro mMelbourne),0,0 );
DateTime thisday = new DateTime();
thisday = DateTime.Now.Ad dMinutes(Double .Parse(minsfrom Melbourne));
label2.Text = thisday.ToStrin g();
label3.Text = thisday.DayOfWe ek.ToString();
}
}
}

if someone can help please do.

thanking you,
NJ

May 15 '06 #1
1 4073
You need to add a call to "InitializeComp onent" in your custom usercontrol
constructor.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"np*****@hotmai l.com" wrote:
Hi,
I am trying to dynamically add user controls on to my web form but for
some reason my form isnt displaying the user control.

form1.cs:
using System;
using System.Drawing;
using System.Collecti ons;
using System.Componen tModel;
using System.Windows. Forms;
using System.Data;

namespace WorldClock
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows. Forms.Form
{
private Point ptr;
private WorldClock.User Control1[] _userctrls;
/// <summary>
/// Required designer variable.
/// </summary>
private System.Componen tModel.Containe r components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeCompo nent();

//
// TODO: Add any constructor code after InitializeCompo nent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Disp ose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeCompo nent()
{
//
// Form1
//
this.AutoScaleB aseSize = new System.Drawing. Size(5, 13);
this.ClientSize = new System.Drawing. Size(704, 334);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHan dler(this.Form1 _Load);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run (new Form1());
}

private void Form1_Load(obje ct sender, System.EventArg s e)
{
ptr = new Point(0,0);

Hashtable hash;
hash =
(Hashtable)Syst em.Configuratio n.Configuration Settings.GetCon fig("Regions");
IDictionaryEnum erator en = hash.GetEnumera tor();
_userctrls = new UserControl1[hash.Count];
int i=0;
while(en.MoveNe xt())
{
_userctrls[i] = new
UserControl1(en .Key.ToString() ,en.Value.ToStr ing());
//_userctrls[i].Location = ptr;
//userctrls[i].Name = "usc" + i.ToString();
//ptr.Offset(_use rctrls[i].Width,0);
//_userctrls[i].Visible = true;

this.Controls.A dd(this._userct rls[i]);
}

}
}
}

usercontrol1.cs :
using System;
using System.Collecti ons;
using System.Componen tModel;
using System.Drawing;
using System.Data;
using System.Windows. Forms;

namespace WorldClock
{
/// <summary>
/// Summary description for UserControl1.
/// </summary>
public class UserControl1 : System.Windows. Forms.UserContr ol
{
private string city="";
private string minsfromMelbour ne="";

private System.Windows. Forms.Label label1;
private System.Windows. Forms.Label label2;
private System.Windows. Forms.Label label3;
/// <summary>
/// Required designer variable.
/// </summary>
private System.Componen tModel.Containe r components = null;

public UserControl1()
{
// This call is required by the Windows.Forms Form Designer.
InitializeCompo nent();

// TODO: Add any initialization after the InitializeCompo nent call
}

public UserControl1(st ring City, string MinsFromMelbour ne)
{
city = City;
minsfromMelbour ne = MinsFromMelbour ne;
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Disp ose();
}
}
base.Dispose( disposing );
}

#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeCompo nent()
{
this.label1 = new System.Windows. Forms.Label();
this.label2 = new System.Windows. Forms.Label();
this.label3 = new System.Windows. Forms.Label();
this.SuspendLay out();
//
// label1
//
this.label1.Loc ation = new System.Drawing. Point(24, 24);
this.label1.Nam e = "label1";
this.label1.Tab Index = 0;
this.label1.Tex t = "label1";
//
// label2
//
this.label2.Loc ation = new System.Drawing. Point(24, 64);
this.label2.Nam e = "label2";
this.label2.Tab Index = 1;
this.label2.Tex t = "label2";
//
// label3
//
this.label3.Loc ation = new System.Drawing. Point(24, 104);
this.label3.Nam e = "label3";
this.label3.Tab Index = 2;
this.label3.Tex t = "label3";
//
// UserControl1
//
this.Controls.A dd(this.label3) ;
this.Controls.A dd(this.label2) ;
this.Controls.A dd(this.label1) ;
this.Name = "UserContro l1";
this.Load += new System.EventHan dler(this.UserC ontrol1_Load);
this.ResumeLayo ut(false);

}
#endregion

private void UserControl1_Lo ad(object sender, System.EventArg s e)
{
label1.Text = city;
TimeSpan ts = new TimeSpan(0,0,in t.Parse(minsfro mMelbourne),0,0 );
DateTime thisday = new DateTime();
thisday = DateTime.Now.Ad dMinutes(Double .Parse(minsfrom Melbourne));
label2.Text = thisday.ToStrin g();
label3.Text = thisday.DayOfWe ek.ToString();
}
}
}

if someone can help please do.

thanking you,
NJ

May 15 '06 #2

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

Similar topics

0
2467
by: sameer mowade via .NET 247 | last post by:
Hello All, I have problem while dynamically removing row from the Datagrid which i have added dynamically as shown in the following code snippet. The problem is that while removing dynamically added row it also removes the row at the end along with the added row. Plz tell me if, I am missing any thing. Code </asp:datagrid>
4
2494
by: Bas Groeneveld | last post by:
I am developing an ASP.NET application part of which consists of a data entry wizard defined by entries in a data table - ie the controls on each page of the wizard are determined by definitions in the table. I know that I can dynamically add controls (eg a textbox) to the page controls collection of a web form in a server event which will then be rendered onto the form, as in the following snippet: System.Web.UI.WebControls.TextBox...
6
3369
by: Steve Booth | last post by:
I have a web form with a button and a placeholder, the button adds a user control to the placeholder (and removes any existing controls). The user control contains a single button. I have done all the usual stuff of recreating the usercontrol in the Page Init event. The 'failure' sequence is as follows: - select web form button to display the user control - select user control button, event fires - select web form button to display...
9
7035
by: netasp | last post by:
hi all, how can I populate one aspx form when page is loading based on page ID? for example: loading page A (to search for VB code) would display labels and texboxes, dropdown lists all related to VB, plus the address bar would show something like this: www.somethinghere?id=3 and if you change number 3 from the address bar to (for example) 34 or 71 you would get different page with the same formatting like I.e: www.somethinghere?id=34...
6
11068
by: | last post by:
I have made some user controls with custom properties. I can set those properties on instances of my user controls, and I have programmed my user control to do useful visual things in response to how those properties are set. I want to be able to do two other things: a) add User control instances to my page, filling in the place of placeholder controls, and b) programmatically setting custom properties on those dynamically spawned...
9
7920
by: Chris | last post by:
I am dynamically adding a user control to each row in a gridview. The reason I am doing it dynamically is the user control is different depending on certain data in the gridview. The gridview contains a placeholder and I add the control to it, the user control is a formview bound to an object datsource. This works great until I post back the page and the user control disappears. What am I doing wrong? Regards, Chris. Protected Sub...
1
7589
by: jelle.huygen | last post by:
Hello, I have a problem in ASP.NET 2.0 with the viewstate of my dynamically added user control. I have reproduced the problem with a very simple user control and a very simple page. On my usercontrol is a button and a label. Everytime the button is clicked a counter which is stored in the viewstate is increased and displayed in the label.
1
2149
by: Shraddha | last post by:
Hi, I am adding some ASP.Net user controls (.ascx file) dynamically on the button click. The user control will get added as many times userhits the button. Now on the click of the submit button, I want to do some form level validations. For example suppose my .ascx file contains a text box with id say "trialTextBox". Now if user hits the button thrice, 3 user controls will be there on the page and as a result 3 textboxes mentioned...
4
4475
by: Lewis Holmes | last post by:
Hi I have the following situation in one of my asp.net pages. The user can add multiple table rows to a form by selecting a button. These rows can contain asp.net controls. When this button is selected, the row is added using JavaScript. The script uses cloneNode to clone a hidden template row and all of its children and then adds the new row to the table, updates all of the child control Ids and sets visibility etc. The hidden...
1
4890
by: semomaniz | last post by:
I have a form where i have created the form dynamically. First i manually added a panel control to the web page. Then i added another panel dynamically and inside this panel i created tables. I have set text boxes and labels inside the table rows. I then added a button. All of these are done through code. The problem that i am having is i can get the value from a text box with resides inside the first panel (out side of panel that is...
0
8444
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8356
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8781
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8551
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6198
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4198
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4368
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2771
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.