?
Solved

DropDownList does not allow child controls solution help is needed.

Posted on 2007-11-14
6
Medium Priority
?
2,069 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 10

Accepted Solution

by:
Oliver Amaya earned 2000 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

762 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