protected withevents shadows - how do I use this?

B

Brian Lowe

I have several aspx pages that include a couple of asp:placeholder controls,
an example is myPage.aspx.

The page inherits from a custom class 'myClass' which inherits
System.Web.UI.Page which lets me ad my own properties and methods to a bunch
of pages in my app.

I want code in myClass to set the contents of the placeholders in myPage.

If I insert the mark-up <asp:placeholder id="myPlaceholder" runat="server"
/> in myPage.aspx and I declare the placeholders in myClass as Protected
WithEvents myPlaceholder As System.Web.UI.WebControls.PlaceHolder
my application runs fine but...

As soon as I edit anything in Visual Studio it sees the placeholder markup
in the .aspx and generates a corresponding
Protected WithEvents myPlaceholder As System.Web.UI.WebControls.PlaceHolder
in myPage.aspx.vb

This is bad! Now the application won't run because myPlaceholder is declared
twice - once in the page code behind and once in the myClass which it
inherits.

If I delete the declaration from the underlying class the code in that class
can't access the placeholder in the page. So I must delete the
auto-generated lines form the .aspx.vb code to get it to run.

Now, when the offending lines are in place the systax checker highlights the
declaration (in myPage.aspx.vb) as conflicting (with that in myClass.vb) and
says I should be using 'shadows' - but where?

Do I declare the placeholder in myClass as shadowing the one in the page, or
do I declare the one in the page as shadowing the one in myClass? What is
the correct syntax, and will it stop Visual Studio from autogenerating
invalid code?

Brian Lowe
---------@
 
K

Karl Seguin

Brian:
One thing you could do is mark myPlaceHolder in myClass as mustimplement and
myclass as mustinherit (abstract in the C# world), I believe this will
solve your problems:

public MustInherit class myClass
inherits Page

protected WithEvents MustImplement myPlaceholder as PlaceHolder
....
end class

and you can leave the myPlaceholder in your page as you normally would


The only down side is that VS.Net doesn't like having abstract classes as
base-pages, so the designer will spit up an error each time you switch to
design mode...you should be fine to just hit "ok" and keep going...sorta
annoying...

Karl



--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
 
B

Brian Lowe

in message
One thing you could do is mark myPlaceHolder in myClass as mustimplement
and
myclass as mustinherit (abstract in the C# world), I believe this will
solve your problems:

public MustInherit class myClass
inherits Page

protected WithEvents MustImplement myPlaceholder as PlaceHolder
...
end class

Thanks for this. I really thoguht it was going to solve my problem. I can
live with the relatively minor annoyance of having to click 'OK' each time I
switch views if it saves me the major annoyance of having to remember to
open up the code-behind and manually delete the offending lines of
auto-generated code.

However, when I change my code to read
Protected WithEvents MustImplement myPlaceholder As
System.Web.UI.WebControls.PlaceHolder
VS comes up with a new warning saying that
'WithEvents' variables must have an 'As' clause.

As far as I can see it *does* have an 'As' clause, so what's VS complaining
of now?

The help for MustImplement shows examples of use which I don't understand...
Public Property MustImplement() As Boolean

This doesn't seem to refer to any control, but adds a MustImplement Boolean
property to he MustInherit class. How do I apply this to my placeholder
controls?

Brian Lowe
---------@
 
B

Brian Lowe

in message
The only down side is that VS.Net doesn't like having abstract classes as
base-pages, so the designer will spit up an error each time you switch to
design mode...you should be fine to just hit "ok" and keep going...sorta
annoying...

Not just anoying... if I make myClass abstract then after I click 'OL' in
the warning dialog I get the HTML view of the page with no option to view
the page in designer mode.

This may not be a bad thing... It's the switch from designer to HTML modes
that triggers the autogeneration of the bad code, so using an abstract and
losing the designer mode will indirectly solve the problem, but I'd rather
solve it properly.

Brian Lowe
---------@
 
K

Karl Seguin

I'm being a vb.net idiot but give a c# guy a break...

it's MustOverride and it'll only work on properties, so you'll have to
change things around a bit:

public class mustinherit myclass
protected MustOverride ReadOnly myPlaceholderProperty () As Placeholder

...
end class

public class page
inherits myclass

protected myPlaceHolder as PlaceHolder

protected ReadOnly myPlaceholderProperty as Placeholder
get
return myPlaceHolder
end get
end property
end class


try that on for size :)
--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
 

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,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top