Solved

DropDownList does not allow child controls solution help is needed.

Posted on 2007-11-14
6
2,012 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
ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

 
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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Why is some text in blue in Visual Studio? 6 33
IIS issues - Convert to Application - APS.NET v4.0 3 36
Deploying to Azure 3 29
jquery datatables, asp.net MVC 9 41
Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

810 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