Solved

DropDownList does not allow child controls solution help is needed.

Posted on 2007-11-14
6
2,029 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 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

Question has a verified solution.

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

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…
One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

696 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