473,379 Members | 1,539 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

add validation to droplist control

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 ??
Nov 19 '05 #1
5 2120
Don't add the validator to the DropDownList control. Set the validator's
ControlToValidate property to be the DropDownList.

example:
compVal.ControlToValidate = DropDownList;

"TJS" <no****@here.com> wrote in message
news:u0*************@TK2MSFTNGP15.phx.gbl...
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 ??

Nov 19 '05 #2
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 ?

"jfleeson" <ja***********@roseheartinn.com> wrote in message
news:uz**************@TK2MSFTNGP12.phx.gbl...
Don't add the validator to the DropDownList control. Set the validator's
ControlToValidate property to be the DropDownList.

example:
compVal.ControlToValidate = DropDownList;

"TJS" <no****@here.com> wrote in message
news:u0*************@TK2MSFTNGP15.phx.gbl...
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 ??


Nov 19 '05 #3
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.

"TJS" wrote:
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 ?

"jfleeson" <ja***********@roseheartinn.com> wrote in message
news:uz**************@TK2MSFTNGP12.phx.gbl...
Don't add the validator to the DropDownList control. Set the validator's
ControlToValidate property to be the DropDownList.

example:
compVal.ControlToValidate = DropDownList;

"TJS" <no****@here.com> wrote in message
news:u0*************@TK2MSFTNGP15.phx.gbl...
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 ??



Nov 19 '05 #4
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.FindByValu e(vID))
else
ctrlEvent.SelectedIndex =
ctrlEvent.Items.IndexOf(ctrlEvent.Items.FindByValu e(""))
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
"jfleeson" <jf******@discussions.microsoft.com> wrote in message
news:40**********************************@microsof t.com... 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.

"TJS" wrote:
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 ?

"jfleeson" <ja***********@roseheartinn.com> wrote in message
news:uz**************@TK2MSFTNGP12.phx.gbl...
> Don't add the validator to the DropDownList control. Set the
> validator's
> ControlToValidate property to be the DropDownList.
>
> example:
> compVal.ControlToValidate = DropDownList;
>
> "TJS" <no****@here.com> wrote in message
> news:u0*************@TK2MSFTNGP15.phx.gbl...
>> 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 ??
>>
>>
>
>


Nov 19 '05 #5
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.

"TJS" wrote:
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.FindByValu e(vID))
else
ctrlEvent.SelectedIndex =
ctrlEvent.Items.IndexOf(ctrlEvent.Items.FindByValu e(""))
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
"jfleeson" <jf******@discussions.microsoft.com> wrote in message
news:40**********************************@microsof t.com...
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.

"TJS" wrote:
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 ?

"jfleeson" <ja***********@roseheartinn.com> wrote in message
news:uz**************@TK2MSFTNGP12.phx.gbl...
> Don't add the validator to the DropDownList control. Set the
> validator's
> ControlToValidate property to be the DropDownList.
>
> example:
> compVal.ControlToValidate = DropDownList;
>
> "TJS" <no****@here.com> wrote in message
> news:u0*************@TK2MSFTNGP15.phx.gbl...
>> 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 ??
>>
>>
>
>


Nov 19 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Stanley J Mroczek | last post by:
I am trying to load a droplist in VB when the edit is clicked in a datagrid. I tried to use OnDataBinding and loading the droplist in subroutine "loaddd". I get this error Object reference not set...
4
by: Tim Meagher | last post by:
I am trying to use both validation controls and to add submit button attributes, but when I add the button attributes, the javascript fpr the validation controls is no longer created for the page. ...
14
by: Matt | last post by:
I want to know if ASP.NET Web Forms Validation Controls are Server-Side or Client-Side form validation? Since I think each validator control can select either 1) JavaScript based error dialog or 2)...
2
by: Barbara Alderton | last post by:
I setup some standard Required Field Validation controls and one Custom validation control on an ASP.NET page (within a user control) to validate text entry. I also setup a Summary Control to post...
4
by: TJS | last post by:
After a post back the selected index should be rest in a droplist to match user's choice, but page always show selected item as first one in the droplist. How can iforce the selected item to be...
2
by: TJS | last post by:
need help with trying to set selected value on a droplist. -- tried vCaptainID as integer and string , no luck -- tried it in pre render and also after droplist is created , no luck =========...
2
by: TJS | last post by:
I have a control, called FieldBuilder, which generates a droplist dynamically for a form. I want to add validation to the droplist thru the 'OnItemCreated' function. i can't seem to get the...
2
by: Tim Frawley | last post by:
Source code attached indicates my problem with validation and a button bar save button. Fill the Textbox with some text then tab off the control. The message box will display the text in the...
0
by: yuchang | last post by:
Hi, I try to use a droplist control to show a list value from Database table "A", and the droplist control is an item of a formview control, its data from Database table "B". My problem is how...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.