ASP.NET: Declare a User Control in codebehind

OK, what seems to be a simple thing is proving impossible to discover.

I want to declare and use a user control in conde-behind.

main.aspx:
<%@ Register TagPrefix="uc" tagname="MyUc" src="../../source/uccode.ascx" %>
[...]
<uc:MyUc id="ucControl" runat="server" />      

main.aspx.cs:
- Declare and access in code

scollegeAsked:
Who is Participating?
 
scollegeConnect With a Mentor Author Commented:
Well, with all the time it took trying to resolve this issue, I simply rewrote the scripted controls as a code-behind control.

I guess this is something you simply cannot do in .NET.
0
 
daveamourCommented:
I think if you just declare an instance of the control with the same ID (ucControl) at class level it should work ok.
0
 
vs1784Commented:
YOu can declare user control like below

Control c1;
c1 = LoadControl("../../source/uccode.ascx");


Hope it helps

Thanks
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
scollegeAuthor Commented:
Doesn't work. If you try that syntax, as below:
--------------------------------
Control MyUc ;
MyUc = LoadControl("../../source/uccode.ascx");

You get the following error:
--------------------------------------
Compiler Error Message: CS1519: Invalid token '=' in class, struct, or interface member declaration

Line 72:       Control MyUc = new Control();
Line 73:       MyUc = LoadControl("../../source/uccode.ascx");

If you try the code below, it will not trigger an error:
Control MyUc = LoadControl("../../source/uccode.ascx");

However, when you try to access a method or property for this control, you get the following error:

theText = MyUc .ReplaceWords(theText);

So no good.
 
0
 
scollegeAuthor Commented:
Ut, forgot the error!

Compiler Error Message: CS0117: 'System.Web.UI.Control' does not contain a definition for 'ReplaceWords'

0
 
daveamourCommented:
You may need to cast to your control type
0
 
scollegeAuthor Commented:
Can you provide an example?
0
 
daveamourCommented:
Like this:
YourControlClassType c1;
c1 = (YourControlClassType)LoadControl("../../source/uccode.ascx");

I think that's right.
0
 
scollegeAuthor Commented:
Not sure what "ControlClassType" should be.

The User Control I'm trying to declare and use is scripted - it's not code-behind.

Contains a simple function to parse a string and return, i.e.:
---------------------------------------------
<script language="C#" runat="server">
//Replaces charged words with approved alternatives
public string ReplaceWords(string strWords)
{
[...]
return thestring;
}
</script>
---------------------------------------------
0
 
daveamourCommented:
So where is that code - in your aspx page?

Also that isn't a user control - it's just a method.

I think maybe this whole question has become confusing.  Why don't you try and explain again what it is you are trying to do in as clear and precise terms as possible.
0
 
scollegeAuthor Commented:
No, the user control "Contains a simple function to parse a string and return".

OK, let's try this again:

1. I have a page in code-behind:
   - main.aspx
   - main.aspx.cs

2. I have a user control that is scripted:
   - ucControl.ascx

3.  Q: I want to declare the user control in my code-behind page and call a function within the user control.



0
 
daveamourCommented:
Ok I think I understand now.  Are you also saying that your user control does not use the code behind model - it has its code scripted?
0
 
scollegeAuthor Commented:
Yes, that's correct. We have too many of these things right now to go and covert each UC to code behind right now.

0
 
daveamourCommented:
Well normally You woudl just go:

NAmespace.Classname x = new Namespace.ClassName();

x.Method()

unless your method is static

I would normally look in the code behind to see what the namespace and classname are.  Does this help at all?
0
 
scollegeAuthor Commented:
Well, the user control is a scripted file. It is not compiled as a namespace or class.

Example time again.

1. Here is an example of the scripted user control:

ucControl.ascx:
---------------------------------------------
<script language="C#" runat="server">
//Replaces charged words with approved alternatives
public string ReplaceWords(string strWords)
{
[...]
return thestring;
}
</script>
---------------------------------------------

2. To declare and use in a *scripted* ASP.NET page, would do the following:
main.aspx:
<%@ Register TagPrefix="uc" tagname="MyUc" src="../../source/uccode.ascx" %>
[...]
<uc:MyUc id="ucControl" runat="server" />    

3. ...and In the code of "main.aspx", I would reference the function thus:

string tmoStrg = ucControl. ReplaceWords(thestring);

Now "main.ascx" is in code-behind, but I would still want to reference the same scripted user control and function.







0
 
scollegeAuthor Commented:
Well, all this time looking and no solution. Just can't be done.

If you decide to convert a project to codebehind, you'll have to convert every UserControl the codebehind page includes to codebehind as well.

Microsoft really dropped the ball with this one, I think.

Although I'm still hoping someone will come along and prove me wrong. But it doesn't look likely.
0
 
feddasCommented:
Maybe there's a way to get the runtime to take care of the datatype when you use FindControl.  Then you can blindly use the properties of MyUc.

Control variablename = Page.FindControl("ucControl")
variablename.ReplaceWords("")

Open in new window

0
All Courses

From novice to tech pro — start learning today.