add validation to droplist control

T

TJS

I need to add validation to drodownlist control but it sends back an error
message that says:

"System.Web.UI.WebControls.DropDownList' does not allow child controls"

the code is :

If e.ItemType = ItemType.FieldItem And e.ColumnName = "IDEvent" Then
Dim ctlEvent As DropDownList = CType(e.Control, DropDownList)
Dim compVal AS CompareValidator = New CompareValidator()
compVal.ErrorMessage = " Please, select something from the list"
compVal.ValueToCompare = ""
compVal.Type = ValidationDataType.String
compVal.Operator = ValidationCompareOperator.NotEqual
ctlEvent.Controls.Add(compVal) '<== error here
End If

How canI add validation to the droplist ??
 
J

jfleeson

Don't add the validator to the DropDownList control. Set the validator's
ControlToValidate property to be the DropDownList.

example:
compVal.ControlToValidate = DropDownList;
 
T

TJS

ahh..

that makes sense.
I tried using "e.control" but that throws an error.

now the question becomes:
how do I find the droplist control ?
 
G

Guest

Set the dropdownlist control as the ControlToValidate when you create the
controls not in an event handler. Validation controls validate on the client
side. The connection between the control and the validator should be setup
before the page is first render. Note: if your users use a client other
then IE. You should call Page.Validate on the post back. Hope this helps.
 
T

TJS

yes, this is all being done thru OnItemcreated.

but I can't seem to get it to find the control name to valdidate:
tried "e.control" -- failed
tried "ctrlEvent" -- failed
tried "IDEvent" -- failed

What else might I try ??????
..
'*******************************************************
Sub EventForm_ItemCreated(sender As Object, e As FieldBuilderEventArgs)
'
'*******************************************************
If e.ItemType = ItemType.FieldItem And e.ColumnName = "IDEvent" Then
Dim ctrlEvent As DropDownList = CType(e.Control, DropDownList)
ctrlEvent.Items.Insert(0, New ListItem("-- Select one --",""))
If vEventTeeTimeID > 0 then
Dim vID As String = Cstr(vEventTeeTimeID) 'Always string
ctrlEvent.SelectedIndex =
ctrlEvent.Items.IndexOf(ctrlEvent.Items.FindByValue(vID))
else
ctrlEvent.SelectedIndex =
ctrlEvent.Items.IndexOf(ctrlEvent.Items.FindByValue(""))
end if

Dim compVal AS CompareValidator = New CompareValidator()
compVal.ErrorMessage = " Please, select something from the list"
compVal.ValueToCompare = "-1"
compVal.Type = ValidationDataType.String
compVal.Operator = ValidationCompareOperator.NotEqual
compVal.ControlToValidate = e.Control '<=================need the
correct code here
e.Item.Controls.Add(compVal)

End If

end sub
 
G

Guest

It's easier to do this in the aspx file i.e.

<ASP:DROPDOWNLIST ID="DropDownList1" RUNAT="server"></ASP:DROPDOWNLIST>
<ASP:REQUIREDFIELDVALIDATOR ID="RequiredFieldValidator1" RUNAT="server"
ERRORMESSAGE="RequiredFieldValidator"
CONTROLTOVALIDATE="DropDownList1"></ASP:REQUIREDFIELDVALIDATOR>

or in the page load event i.e.:

RequiredFieldValidator1.ControlToValidate = DropDownList;

but if for some reason you need to do it when the control is created uses
the FindControl([name]) method.

Hope this helps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top