Solved

DropDownList does not allow child controls solution help is needed.

Posted on 2007-11-14
6
1,986 Views
Last Modified: 2008-02-01
I am creating some custom validators from codebehind. It all works fine until I try to add my validator to a dropdownlist.

The errors is as followed: 'System.Web.UI.WebControls.DropDownList' does not allow child controls on line 15.

I have googled the web and found this solution to my problem:
http://www.codeproject.com/useritems/DoesNotAllowChildControls.asp

However I am having some difficulties implementing this solution to my code. It does not seem to work by just copy pasting it into my class and I do not have the necessary experience yet to modify this so it will work. Therefore I will need some help :)

The class file: (shortened of course)
------------------------------------------------------------------------------------------------------------------------------------------
Namespace GlobalFunctions
    Public Class eX
        Public Sub AddValidator(ByVal ControlToValidate As Object, ByVal ValidationType As String, ByVal ClientValidation As Boolean, ByVal ValidateOnEmpty As Boolean, ByVal ResponseType As Integer)
            'Some stuff here like generating an ID etc.

            Dim NewValidator As New CustomValidatorWithParams
            NewValidator.ID = NewValidatorID
            NewValidator.ControlToValidate = ControlToValidate.ClientID
            NewValidator.EnableClientScript = ClientValidation
            NewValidator.ClientValidationFunction = ValidationType
            NewValidator.ValidateEmptyText = ValidateOnEmpty
            NewValidator.ClientValidatorFunction = "Validate" & ControlToValidate.ClientID
            NewValidator.ResponseType = ResponseType

            AddHandler NewValidator.ServerValidate, AddressOf DoServerValidation
            ControlToValidate.Controls.Add(NewValidator) <== THIS IS WHERE IT GETS BAD
        End Sub

        Protected Overrides Function CreateControlCollection() As System.Web.UI.ControlCollection
            Return New ControlCollection(Me)
        End Function
------------------------------------------------------------------------------------------------------------------------------------------

Please note. This is of course not the complete code. There are a lot more things involved like the actual validating etc. This is all working since I could test this with a textbox. The problem is attaching it to the dropdownlist.

Please note 2. If I remove the line  ControlToValidate.Controls.Add(NewValidator) my code stops working so that would not be an option as it is based on the validator as child control.

Hope someone can be of assistance :)
0
Comment
Question by:Onthrax
  • 3
  • 3
6 Comments
 
LVL 10

Accepted Solution

by:
joex911 earned 500 total points
ID: 20279366
Hi,

I'm guessing that you're getting a compiler error when you add that method?, if so, I believe that you need to create a new custom control class that inherits from DropDownList, and there is where you add the overrided method, then you would use that control in your class eX as if it was a normal DropDownList.
0
 
LVL 13

Author Comment

by:Onthrax
ID: 20279670
You pointed me in the right direction. However I require some more assistance.

I have created the new class as followed:
------------------------------------------------------------------------------------------------------------------------------------------
    Public Class DropDownListWithValidation
        Inherits System.Web.UI.WebControls.DropDownList

        Protected Overrides Function CreateControlCollection() As System.Web.UI.ControlCollection
            Return New ControlCollection(Me)
        End Function

    End Class
------------------------------------------------------------------------------------------------------------------------------------------

Now in my original class I am using this to create the new class object:

------------------------------------------------------------------------------------------------------------------------------------------
Dim aaa As DropDownListWithValidation = CType(FindControl(ControlToValidate.ClientID), DropDownListWithValidation)
------------------------------------------------------------------------------------------------------------------------------------------
This should give me my dropdown control in variable aaa correct?

However this gives me this error:
      Unable to cast object of type 'System.Web.UI.WebControls.DropDownList' to type 'GlobalFunctions.DropDownListWithValidation'

The control is being found, so that is not the issue. However what am I doing wrong here?
0
 
LVL 10

Expert Comment

by:joex911
ID: 20279874
You have to use your new control in the aspx page as well (DropDownListWithValidation instead of a normal DropDownList), this is what is called a web custom control:

http://www.ondotnet.com/pub/a/dotnet/excerpt/progaspdotnet_14/index2.html
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 13

Author Comment

by:Onthrax
ID: 20279932
Hmm.. Is there no way to programmatically convert the existing dropdownlist with values into the new dynamically created extended dropdownlist control? Nothing is changed except for the part where it is allowed to get child controls..
0
 
LVL 10

Expert Comment

by:joex911
ID: 20280121
No, because of the inheritance you can do it the other way around, meaning you could cast a DropDownListWithValidation to a normal DropDownlist, derived class to base class, this happens since the base class has no way of knowing that the derived class exists.
0
 
LVL 13

Author Comment

by:Onthrax
ID: 20289187
Ok. thank you for your help
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now