473,414 Members | 1,703 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,414 software developers and data experts.

calling the constructor of an usercontrol

Hello,

Well, the problem is clear: How to call a constructor of a programmaticaly
loaded user control?

The LoadControl function obviously won't do it. And using this:
<%@ Register TagPrefix="uc1" TagName="newcontrol" Src="newcontrol.ascx" %>

code behind:

Panel1.Controls.Add(new newcontrol("abc"));

newcontrol.ascx (constructor code):

public newcontrol(string _text)

{

Label1.Text = _text;

}
Will give me this failure on runtime:

Compilerfailure: CS1501: No overload for the method 'newcontrol' requires
'0' arguments

Line 31: private static bool __initialized = false;
Line 32:
Line 33: public newcontrol_ascx() {
Line 34: if ((ASP.newcontrol_ascx.__initialized == false)) {
Line 35: ASP.newcontrol_ascx.__initialized = true;
Note: I'm not using an English .net framework, the above compiler failure
message was originaly in German, it's just my translation of the original
German Text. The same original text in English might be different.
Nov 19 '05 #1
4 6006
> The LoadControl function obviously won't do it.

Obviously, you're wrong. This is indeed how you load a user control
programmatically.

Of course, loading it doesn't add it to the Page, or whatever Control you
want to host it in. You have to do that as well.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Big things are made up of
lots of little things.

"the friendly display name"
<th********************@discussions.microsoft.co m> wrote in message
news:90**********************************@microsof t.com...
Hello,

Well, the problem is clear: How to call a constructor of a programmaticaly
loaded user control?

The LoadControl function obviously won't do it. And using this:
<%@ Register TagPrefix="uc1" TagName="newcontrol" Src="newcontrol.ascx" %>

code behind:

Panel1.Controls.Add(new newcontrol("abc"));

newcontrol.ascx (constructor code):

public newcontrol(string _text)

{

Label1.Text = _text;

}
Will give me this failure on runtime:

Compilerfailure: CS1501: No overload for the method 'newcontrol' requires
'0' arguments

Line 31: private static bool __initialized = false;
Line 32:
Line 33: public newcontrol_ascx() {
Line 34: if ((ASP.newcontrol_ascx.__initialized == false)) {
Line 35: ASP.newcontrol_ascx.__initialized = true;
Note: I'm not using an English .net framework, the above compiler failure
message was originaly in German, it's just my translation of the original
German Text. The same original text in English might be different.

Nov 19 '05 #2
..net doesn't support usercontrols with contructor args. move them to a
method you call after contruction.

-- bruce (sqlwork.com)
"the friendly display name"
<th********************@discussions.microsoft.co m> wrote in message
news:90**********************************@microsof t.com...
Hello,

Well, the problem is clear: How to call a constructor of a programmaticaly
loaded user control?

The LoadControl function obviously won't do it. And using this:
<%@ Register TagPrefix="uc1" TagName="newcontrol" Src="newcontrol.ascx" %>

code behind:

Panel1.Controls.Add(new newcontrol("abc"));

newcontrol.ascx (constructor code):

public newcontrol(string _text)

{

Label1.Text = _text;

}
Will give me this failure on runtime:

Compilerfailure: CS1501: No overload for the method 'newcontrol' requires
'0' arguments

Line 31: private static bool __initialized = false;
Line 32:
Line 33: public newcontrol_ascx() {
Line 34: if ((ASP.newcontrol_ascx.__initialized == false)) {
Line 35: ASP.newcontrol_ascx.__initialized = true;
Note: I'm not using an English .net framework, the above compiler failure
message was originaly in German, it's just my translation of the original
German Text. The same original text in English might be different.

Nov 19 '05 #3
My problem was (is) the constructor, not the loading.

"Kevin Spencer" wrote:
The LoadControl function obviously won't do it.


Obviously, you're wrong. This is indeed how you load a user control
programmatically.

Nov 19 '05 #4
The LoadControl method creates the instance. What do you need to call the
Constructor for?

The Constructor method you posted sets the text of a Label. Yes, I know, the
Label is not accessible. But you can make it accessible:

public string Label1Text
{
get { return Label1.Text; }
set { Label1.Text = value; }
}

UserControls are not designed to be instantiated by calling the Constructor.
They are made to be loaded declaratively (via inserting a tag in a Template)
or programmatically (using the LoadControl method). This is because they are
Templated Controls. If you want to create a Control that you can pass data
to in the Constructor, create a custom Server Control.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Big things are made up of
lots of little things.

"the friendly display name"
<th********************@discussions.microsoft.co m> wrote in message
news:0F**********************************@microsof t.com...
My problem was (is) the constructor, not the loading.

"Kevin Spencer" wrote:
> The LoadControl function obviously won't do it.


Obviously, you're wrong. This is indeed how you load a user control
programmatically.

Nov 19 '05 #5

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

Similar topics

3
by: Dave McCracken | last post by:
I am hosting .Net UserControls in HTML pages with Object Tags. These UserControls are clients of remotable objects that run on the same machine. The remote objects execute callbacks via sponsor...
6
by: Justin | last post by:
Hello, first time posting. If I have a base class and a derived class, is there only one way to call the base constructor? i.e. Is this the only way I can call the base constructor...
1
by: JezB | last post by:
In my Page_Load event of all my web app Pages and UserControls I instantiate the same library class "TextResources" (to take care of reading presentation text strings from localizable resource...
8
by: Sam Kuehn | last post by:
How do I accomplish the fallowing (is it even possible). Say I write a UserControl "MyControl.ascx". Now I use LoadControl("MyControl.ascx"). But I really want MyControl to require parameters in...
3
by: Peter Rilling | last post by:
Okay, I am probably missing something simple so here is my problem. I have a page. On this page I have a usercontrol. On this user control I have another usercontrol. On each usercontrol I...
6
by: daveb | last post by:
I'm trying to write some code that calls the constructors of STL containers explicitly, and I can't get it to compile. A sample program is below. One compiler complains about the last two lines...
2
by: fillic2002 | last post by:
Hi I have a query related to the VS2005 usercontrol. As i have two different usercontrol in a single master page say uc1 and uc2. Now i am calling a function of uc1 from uc2. what is the best...
1
by: newbie | last post by:
This is a snippet from C++ FAQs, which I have never done--- when I do such a thing, I would declare function used by constructor ( in this example, init() ) as static. But I do understand that it...
7
by: =?Utf-8?B?UVNJRGV2ZWxvcGVy?= | last post by:
I have a C# logging assembly with a static constructor and methods that is called from another C# Assembly that is used as a COM interface for a VB6 Application. Ideally I need to build a file...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.