Cannot reference UserControl using C Sharp, please help.

  • Thread starter Thread starter JBLi
  • Start date Start date
J

JBLi

Hi All,

Just trying to convert my asp.net website from using VB.net to
CSharp.net and running into UserControl referencing issue. Please help.

In my page, originally using vb.net, I can do the following:


Dim MyControl as Object = Page.FindControl(".....")

MyControl.TestMethod(...)


That, is declaring the variable as Object and "it knows the methods".


How can I do the samiliar stuff using if using CSharp? I tried to
trace-output the class type by first Page.LoadControl() the ascx file and
cased into a UserControl type, the output shown the usercontrol in the ASP.
namespace; but when I try to declare a variable in ASP.MyControl_ascx, it
complains about the ASP namespace or it does not contain MyControl_ascx.

Again, I am sure either I missed some basic learning or so. But I
cannot find help anymore nor "google-able". Please help.



Regards,

JB
 
You were doing it wrong(or at least u should not have done it that way) ...
you should have used:
Dim MyControl as Control = Page.FindControl(".....")

Anyways, now in C# (even in VB, u could have used CType probably) u need to
box the control:

//if name of the class for ur user control is MyTestControl
Control ctl = Page.FindControl(".....");
if ((ctl!=null)&&(ctl is MyTestControl)) {
MyTestControl myControl = (MyTestControl)ctl;
ctl.TestMethod(...);
}

Abhijeet Dev
 
Hi Abhijeet,

Thanks for the advice.

I forgot to state that I am using SRC instead of CodeBehind in the aspx
file to indicate the (aspx.cs) code behind source ; thus I don't reply on
VS.net to compile into DLL.

Thanks for remind me that I should use Control as the type when using
the FindControl()....

I believe that your suggestion definitely work if my site is compiled
by VS.net into DLL. Again, since I use VS.net to do the compile, so my
whole website is dynamically compiled by IIS.

if ((ctl!=null)&&(ctl is MyTestControl)) {
MyTestControl myControl = (MyTestControl)ctl;

So for the above code, "I don't really know" MyTestControl class type.
It complains it does not know about MyTestControl. Trace output revealed
the control is type of: ASP.MyTestControl_ascx. But when I use
ASP.MyTestControl_ascx to declare the control variable, it complains that it
does not know about ASP.My.TestControl_ascx.

Any solution?


- JB
 
can you please attach the code block and the trace output... if its
ASP.MyTestControl_ascx, why should it search for ASP.My.TestControl_ascx?
 
Hi,


RECAP: what to reference/access the user controls methods when NOT
using CodeBehind and VS.net.


Anyway, the ASP.My.TestControl_ascx is just a typo. Sorry for the
extra dot after "My". I will try to post some code block.

Meanwhile, giving the fact that I only use SRC instead of CodeBehind
and the fact that my control's class type is automatically generated by the
IIS compiler, what namespace should I "using" when declare variables [of my
control's class type]?

Couple of "googling" show up one solution is to roll back to use
CodeBehind tag, but do a batch/commandline running of the VS.net compiler
(without actually using the IDE, of course).

Instead, I actually implement a silly solution: Use VS.net to generate
some Interfaces / base classes with Virtual methods and compile into an
assembly. Then, in the actual control's class source (aspx.cs file), make
the control class to implement the interface or to derive from the base
class. Now, I can type-case my control into the "known interfaces" and call
the methods.

Welcome more suggestions.


Thanks,

JB
 
Back
Top