Solved

DropDownList does not allow child controls solution help is needed.

Posted on 2007-11-14
6
2,018 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:
Oliver Amaya 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:Oliver Amaya
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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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:Oliver Amaya
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

856 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