Question re: Server Control Development

C

Chris McKenzie

I'm developing a server control to automatically display UI associated with
a report developed in SQL Server Reporting Services. One of the things I
want to do is to optionally display boolean parameters as a checkbox or a
radiobuttonlist.

I created an enum as follows:

Public Enum BooleanDisplayType

CheckBox

Radiobutton

End Enum



and an associated property as follows:

Public Property DisplayBooleanControlAs() As BooleanDisplayType

Get

Dim o As Object =
Me.ViewState(Me.GetPropertyLookup(PropertyLookup.BooleanControl))

Return CType(IIf(IsNothing(o), BooleanDisplayType.CheckBox, o),
BooleanDisplayType)

End Get

Set(ByVal Value As BooleanDisplayType)

Me.ViewState(Me.GetPropertyLookup(PropertyLookup.BooleanControl)) =
Value

End Set

End Property



I can drag/drop the control onto the webform without issue, but if I change
the default value for Control.DisplayBooleanControlAs, I get an error when
the Page tries to init.

Compiler Error Message: BC30456: 'ReportParamViewer' is not a member of
'System.Web.UI.ControlCollection'.

Here is the code from the aspx page:

<cc1:ReportParamViewer id=rpViewer
style="Z-INDEX: 105; LEFT: 552px; POSITION: absolute; TOP: 118px"
runat="server"
Height="542px" Width="577px" ResizeToFitContents="True" Columns="2"
BackColor="Linen"
BorderColor="Maroon" BorderStyle="Solid" BorderWidth="1px"
ForeColor="MediumOrchid"
Font-Names="Lucida Console" ParametersHeader="This is a test Report"
Font-Bold="True"
DisplayBooleanControlAs="Radiobutton">
</cc1:ReportParamViewer>


If I leave everything exactly as it is, and remove the last portion of the
ReportParamViewer declaration ("DisplayBooleanControlAs='RadioButton'") then
everything compiles and runs fine. If the value of DisplayBooleanControlAs
= "RadioButton" or "CheckBox", I get an the above compiler error. It
appears as if "RadioButton" and "CheckBox" are treated as reserved words by
the compiler--which doesn't make sense for attributes of a control element.
Can anyone confirm / deny this? Does anyone have any other ideas?

Thanks,

Chris
 
N

Natty Gur

Hi,

I dont think so. I even try your post on simple user control and it's
working. I didnt check GetPropertyLookup cause you didnt post it source.

Natty Gur[MVP]

blog : http://weblogs.asp.net/ngur
Mobile: +972-(0)58-888377
 
C

Chris McKenzie

GetPropertyLookup simply returns a formatted string for the server control
as follows:

Private Function GetPropertyLookup(ByVal item As PropertyLookup) As String

Return Me.UniqueID + ":" + item.ToString

End Function

I did some further testing--I changed the name of the enum elements from
CheckBox and RadioButton to "CB" and "RB". I get identical behavior, so my
theory that something was choking on the names is invalid.

Any ideas?

To reiterate: When I do not have the reference ot DisplayBooleanControlAs in
the aspx page (i.e., the server control is in its initial state) I get no
errors, and the server control renders and behaves as expected. If I change
the DisplayBooleanControlAs value in the propertyGrid, the ASP .NET
application will no longer compile at all. I can't even get to Page_Load().
I can remove the reference to the property in the aspx page, and everything
is okay again. This is the only property I get this behavior with.

Chris
 
C

Chris McKenzie

I've gone further with my testing, and changed the name of the property.
Same issue. I added a new property of type integer. No problems changing its
values. There's something subtle going on here....

Chris
 
C

Chris McKenzie

Yet another test: this time I created a simple server control with just a
couple of properties. I created an enum as follows:

Public Enum TestEnum

test1

test2

test3

End Enum

and a property as follows:

Public Property Test() As TestEnum

Get

Dim o As Object = Me.ViewState("mytest")

If IsNothing(o) Then Return TestEnum.test1

Return CType(o, TestEnum)

End Get

Set(ByVal Value As TestEnum)

Me.ViewState("mytest") = Value

End Set

End Property



Again, when I set the value of the enum, I get the compiler error. If I
don't set it at all, everything works fine. The problem seems to be related
to the fact that I'm using an enum as my property type. How could that be,
though, when so many other webcontrols use enums for various properties?

Chris
 
K

Kevin Spencer

An Enum is a set of numeric values represented by tokens. You're trying to
cast a StateBag as an enum.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
C

Chris McKenzie

Again, I fall back to: How is it that this is done (successfully) in other
WebControls? The MSDN Documentation clearly shows that what I'm doing is
correct:

http://msdn.microsoft.com/library/d...uide/html/cpcondevelopingwebformscontrols.asp
"The ASP.NET syntax for declaratively setting an enumeration property is
shown in the following example, which sets the TextMode property on an
instance of the System.Web.UI.WebControls.TextBox control.

<asp:TextBox TextMode = "MultiLine" runat = server/>The ASP.NET page parser
converts the supplied string to the appropriate enumeration value, in this
case TextBoxMode.MultiLine.

If a property is an enumeration type, its values are displayed in a
drop-down list when a user clicks on it in the property browser of a visual
designer such as Visual Studio .NET."

So I'm not sure what you mean by "trying to cast a StateBag as an enum. The
ViewState is intelligent enough to parse enums, as is evidenced by the above
and numrous other examples. Further, the code I wrote compiles fine as long
as I don't try to reference the enumerated property in the aspx page. I can
set the property at runtime, and it works as expected--no problem. So the
issue apparently an enum-to-ViewState cast, but the compiler. This raises
the question again: "How was this done successfully at all in the other
WebControls?" "What's the workaround?"

Thanks,
Chris
 
C

Chris McKenzie

http://msdn.microsoft.com/library/d...uide/html/cpcondevelopingwebformscontrols.asp

"The ASP.NET syntax for declaratively setting an enumeration property is

shown in the following example, which sets the TextMode property on an

instance of the System.Web.UI.WebControls.TextBox control.

<asp:TextBox TextMode = "MultiLine" runat = server/>The ASP.NET page parser

converts the supplied string to the appropriate enumeration value, in this
case TextBoxMode.MultiLine.

If a property is an enumeration type, its values are displayed in a

drop-down list when a user clicks on it in the property browser of a visual

designer such as Visual Studio .NET."



I've been unable to successfully define a property of my server control as
an enumerated type. The error is as follows:

Compiling the server control happens without issue. If I drop the control
on the aspx page, everything compiles/runs fine. If I change the value of
the enumerated property, it won't compile. If I change the value of the
enumerated back to its default value, it still won't compile. If I remove
the reference to the enumerated property from the aspx page (effectively
resetting it to its default value), the project compiles/runs fine.

Anybody have any ideas about what causes this, or how to get around it?



Thanks,

Chris
 
K

Kevin Spencer

You left out the error.
--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
C

Chris McKenzie

I can drag/drop the control onto the webform without issue, but if I change
the default value for Control.DisplayBooleanControlAs, I get an error when
the Page tries to init.

Compiler Error Message: BC30456: 'ReportParamViewer' is not a member of
'System.Web.UI.ControlCollection'.

Here is the relevant code from the server control:
Public Enum BooleanDisplayType

CheckBox

RadioButton

End Enum

Public Property BooleanControlType() As BooleanDisplayType ' Boolean Display
Type

Get

Dim o As Object = Me.ViewState("BooleanControlType")

Return CType(IIf(IsNothing(o), "", o), BooleanDisplayType)

End Get

Set(ByVal Value As BooleanDisplayType)

Me.ViewState("BooleanControlType") = Value

Me.ChildControlsCreated = False

End Set

End Property


Here is the code from the aspx page:

<cc1:ReportParamViewer id=rpViewer
style="Z-INDEX: 105; LEFT: 552px; POSITION: absolute; TOP: 118px"
runat="server"
Height="542px" Width="577px" ResizeToFitContents="True" Columns="2"
BackColor="Linen"
BorderColor="Maroon" BorderStyle="Solid" BorderWidth="1px"
ForeColor="MediumOrchid"
Font-Names="Lucida Console" ParametersHeader="This is a test Report"
Font-Bold="True"
BooleanControlType="radiobutton">
</cc1:reportparamviewer>


If I leave everything exactly as it is, and remove the last portion of the
ReportParamViewer declaration ("BooleanControlType='RadioButton'") then
everything compiles and runs fine. If the value of BooleanControlType
= "RadioButton" or "CheckBox", I get an the above compiler error.

Thanks,
Chris
 
K

Kevin Spencer

There are several problems with your code, and it's hard to sort out the
problem without the entire Control definition. However, from the little code
you have posted, I would suspect that the line "Me.ChildControlsCreated =
False" in your Set method is causing the problem. Why is it there?

In addition, your Get method leavesmuch to be desired. How is a blank string
supposed to be typed as an enumeration? I would advise creating a default
value that IS an enumerated value from your enum. Something like:

Public Enum BooleanDisplayType
CheckBox
RadioButton
Uninitialized
End Enum

Public Property BooleanControlType() As BooleanDisplayType ' Boolean Display
Type

Get
If IsNothing(ViewState("BooleanControlType")) Then Return
BooleanDisplayType.Uninitialized
Return CType(ViewState("BooleanControlType"), BooleanDisplayType)
End Get

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
C

Chris McKenzie

Hi Kevin,

Thanks for your reply.

It's true that this implementaion of hte BooleanDisplayType property is
problematic. That's because I had had to change it from an enumerated
implementation to a string implementation so that I could move forward with
my work. In reposting this question, I had to retro- the code back to when
it was the enumerated type, and I just forgot the one step. In the original
code, the default was BooleanDisplayType.RadioButton.

It's been about 3 days since I originally posted this question, and as of
now I can no longer recreate the problem: argh! I'll repost the question
when/if I have the problem again.

As to the question of why I have Me.ChildControlsCreated = False:

I had tried to use me.EnsureChildControls to be sure that the various style
and other properties were applied to the rendering of the control at design
time. When I used EnsureChildControls, however, the control stopped
rendering in any meaningful way. When I took EnsureChildControls out of the
property setters, the control rendered at design time, but design time edits
to the control via the PropertyGrid did not take effect until the next time
I compiled. When I set Me.ChildControlsCreated = False most of those issues
were resolved. (I still have the same problem with certain aspects of the
various control styles, namely anything that is not by default a part of the
WebControl.CssStyleCollection.) If there's a better way to do this, plase
let me know.

Anyway, Sorry I couldn't reproduce the problem for ya' (a couple of days ago
I couldn't get around it. I have switched from a rendered to a composite
control in the meantime--I wonder if that has anythign to do with it...).
Thanks for your time!

Chris
 
K

Kevin Spencer

Hi Chris,

I wish I could help further, but I don't really know anything about your
Control other than the block of code that you posted. However, if you're not
having any trouble with it presently, perhaps it isn't necessary.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
C

Chris McKenzie

Thanks Kevin, if and when this issue rears its head again, I'll repost with
a full source sample.

Chris
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top