Major problem understanding how to do user control in 2.0

A

Alan Silver

Hello,

I have a user control that has one .cs file, with several .ascx files
that call it. The reason for this is that I can pick whichever of the
..ascx files I want (based on a site owner setting), and I know the
code-behind is the same in all cases. That way the .ascx file can be
picked dynamically, allowing the appearance of the page to be changed
based on a user setting.

This was working fine in 1.1, but had the code-behind compiled into a
DLL. I'm trying to convert the user control(s) to use the new 2.0
code-behind model, and would like to avoid having to compile the DLL
each time.

The page that uses the control has a placeholder called
plcProductDetails, and has code like...

string fileName = "ShowProductStyle1.ascx"; // normally from user
setting
Control ctlShowProduct = LoadControl(fileName);
((ShowProduct)ctlShowProduct).DisplayProduct();
plcProductDetails.Controls.Add(ctlShowProduct);

Now this worked fine when using the old 1.1 code-behind model. I could
compile ShowProduct.dll on its own and the code above would find it OK.

Now I have deleted the DLL, and changed the top of each of the .ascx
files to look like...

<%@ Control CodeFile="ShowProduct.cs" Inherits="ShowProduct" %>

and the top of the code-behind file looks like...

// various "using" statements omitted for clarity
public partial class ShowProduct : UserControl {


If I try to load the page now, I get an error...

"CS0246: The type or namespace name 'ShowProduct' could not be found"

on the line...

((ShowProduct)ctlShowProduct).DisplayProduct();

I don't know how to get around this. When LoadControl was called, it
referenced the .ascx file, and that referenced the code-behind file that
contains the class definition for ShowProduct. Why then can't the
framework find the class?

I hope this is clear. I'm really lost here and would appreciate any clue
as to how I am supposed to do this. The weird thing is that I tried
creating a user control in VWD, and I could have two .ascx files
referencing the same .cs file and they both work fine without me needing
to compile anything. I'm obviously doing something wrong, but I can't
see what.

TIA
 
B

Bruce Barker

you can not share partial classes between asxc. you need to stick to your
old coding style. move the common code to a file in the app_code dir, so all
pages can see it. as you will not be able to use a partial classes, turn
autowireup events off, and use the 1.1 coding style.

you would proably be better off switching to themes to change the look.

-- bruce (sqlwork.com)
 
A

Alan Silver

you can not share partial classes between asxc. you need to stick to your
old coding style. move the common code to a file in the app_code dir, so all
pages can see it. as you will not be able to use a partial classes, turn
autowireup events off, and use the 1.1 coding style.

OK, thanks. At least I know I wasn't doing anything stupid (for once!!)
you would proably be better off switching to themes to change the look.

I don't think Themes wouldn't help in this situation. Don't they only
apply to full pages? I need the rest of the page to stay as it is, and
just have this one bit display according to the style set by the site
owner.

Thanks anyway. I have been looking at themes, and they do look good, but
you have to know when they are applicable.

Ta ra
-- bruce (sqlwork.com)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top