473,385 Members | 1,593 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,385 software developers and data experts.

Validator in DropDownList?

I'm trying to put conditional code in a dropdown that would check the value
of the current ListItem and set Selected=true depending on that value, and
Selected=false if it's any other value.

Due to security restrictions, I can't place any script blocks on, or behind,
the page, so it has to be done in the .Net framework.

I've tried using the CompareValidator but that throws errors. (see code
block below)

Any ideas?

<asp:DropDownList runat="server" id="ctl_03" DataTextField="StateName"
DataSourceID="StateList" DataValueField="StateName" DataMember="DefaultView"
AutoPostBack="True" AppendDataBoundItems="true">
<asp:CompareValidator runat="server" ErrorMessage="CustomValidator"
id="CompareValidator 1" ControlToValidate="ctl_03"
ValueToCompare="California" type="String" Operator="Equal" >
<asp:ListItem Selected="True" Value="California" Text="California" />
</asp:CompareValidator >

<asp:ListItem Value="" Text="" />
</asp:DropDownList>

Apr 27 '07 #1
5 10269
Chris, could you clarify what you are trying to accomplish a little more
clearly? You are using the CompareValidator incorrectly. You can't nest the
CompareValidator inside the DropDownList declaration.

<asp:DropDownList
runat="server"
id="ctl_03"
DataTextField="StateName"
DataSourceID="StateList"
DataValueField="StateName"
DataMember="DefaultView"
AutoPostBack="True"
AppendDataBoundItems="true">
<asp:ListItem Selected="True" Value="California"
Text="California" />
<asp:ListItem Value="" Text="" />
</asp:DropDownList>

<asp:CompareValidator
runat="server"
ErrorMessage="CustomValidator"
id="CompareValidator 1"
ControlToValidate="ctl_03"
ValueToCompare="California"
type="String"
Operator="Equal">
</asp:CompareValidator >

"Chris G." wrote:
I'm trying to put conditional code in a dropdown that would check the value
of the current ListItem and set Selected=true depending on that value, and
Selected=false if it's any other value.

Due to security restrictions, I can't place any script blocks on, or behind,
the page, so it has to be done in the .Net framework.

I've tried using the CompareValidator but that throws errors. (see code
block below)

Any ideas?

<asp:DropDownList runat="server" id="ctl_03" DataTextField="StateName"
DataSourceID="StateList" DataValueField="StateName" DataMember="DefaultView"
AutoPostBack="True" AppendDataBoundItems="true">
<asp:CompareValidator runat="server" ErrorMessage="CustomValidator"
id="CompareValidator 1" ControlToValidate="ctl_03"
ValueToCompare="California" type="String" Operator="Equal" >
<asp:ListItem Selected="True" Value="California" Text="California" />
</asp:CompareValidator >

<asp:ListItem Value="" Text="" />
</asp:DropDownList>
Apr 27 '07 #2
"Chris G." <Ch****@discussions.microsoft.comwrote in message
news:EC**********************************@microsof t.com...
I'm trying to put conditional code in a dropdown that would check the
value
of the current ListItem and set Selected=true depending on that value, and
Selected=false if it's any other value.

Due to security restrictions, I can't place any script blocks on, or
behind,
the page, so it has to be done in the .Net framework.

I've tried using the CompareValidator but that throws errors. (see code
block below)

Any ideas?

<asp:DropDownList runat="server" id="ctl_03" DataTextField="StateName"
DataSourceID="StateList" DataValueField="StateName"
DataMember="DefaultView"
AutoPostBack="True" AppendDataBoundItems="true">
<asp:CompareValidator runat="server" ErrorMessage="CustomValidator"
id="CompareValidator 1" ControlToValidate="ctl_03"
ValueToCompare="California" type="String" Operator="Equal" >
<asp:ListItem Selected="True" Value="California" Text="California" />
</asp:CompareValidator >

<asp:ListItem Value="" Text="" />
</asp:DropDownList>
I think you are missing the purpose of a validator here.
A validator will produce output preventing a form from being submitted if it
is not filled in properly, it will not produce output depending on a value.

Riki
Apr 27 '07 #3
An example of what I'm trying to do is
1. Query a db table called "states" (q_Sel_States: that's already done. no
problem there)
2. Populate a DropDownList with the values returned from q_Sel_States
3. If the state's name is 'California' set Selected="True" otherwise,
Slected="False"

But I can't use VB or C# anywhere on the page.

TIA!

CG

"Page" wrote:
Chris, could you clarify what you are trying to accomplish a little more
clearly? You are using the CompareValidator incorrectly. You can't nest the
CompareValidator inside the DropDownList declaration.

<asp:DropDownList
runat="server"
id="ctl_03"
DataTextField="StateName"
DataSourceID="StateList"
DataValueField="StateName"
DataMember="DefaultView"
AutoPostBack="True"
AppendDataBoundItems="true">
<asp:ListItem Selected="True" Value="California"
Text="California" />
<asp:ListItem Value="" Text="" />
</asp:DropDownList>

<asp:CompareValidator
runat="server"
ErrorMessage="CustomValidator"
id="CompareValidator 1"
ControlToValidate="ctl_03"
ValueToCompare="California"
type="String"
Operator="Equal">
</asp:CompareValidator >

"Chris G." wrote:
I'm trying to put conditional code in a dropdown that would check the value
of the current ListItem and set Selected=true depending on that value, and
Selected=false if it's any other value.

Due to security restrictions, I can't place any script blocks on, or behind,
the page, so it has to be done in the .Net framework.

I've tried using the CompareValidator but that throws errors. (see code
block below)

Any ideas?

<asp:DropDownList runat="server" id="ctl_03" DataTextField="StateName"
DataSourceID="StateList" DataValueField="StateName" DataMember="DefaultView"
AutoPostBack="True" AppendDataBoundItems="true">
<asp:CompareValidator runat="server" ErrorMessage="CustomValidator"
id="CompareValidator 1" ControlToValidate="ctl_03"
ValueToCompare="California" type="String" Operator="Equal" >
<asp:ListItem Selected="True" Value="California" Text="California" />
</asp:CompareValidator >

<asp:ListItem Value="" Text="" />
</asp:DropDownList>
Apr 27 '07 #4
Riki,
Thanks for your input! You might be thinking of RequiredFieldValidator,
though. With CompareValidator, all it does is give an output depending on the
comparrison. I know it's not the correct usage in this case, but I'm trying
to accomplish something similar here. I've done the exact same thing in
ColdFusion with no problem, I' trying to figure out the .Net method.

"Riki" wrote:
"Chris G." <Ch****@discussions.microsoft.comwrote in message
news:EC**********************************@microsof t.com...
I'm trying to put conditional code in a dropdown that would check the
value
of the current ListItem and set Selected=true depending on that value, and
Selected=false if it's any other value.

Due to security restrictions, I can't place any script blocks on, or
behind,
the page, so it has to be done in the .Net framework.

I've tried using the CompareValidator but that throws errors. (see code
block below)

Any ideas?

<asp:DropDownList runat="server" id="ctl_03" DataTextField="StateName"
DataSourceID="StateList" DataValueField="StateName"
DataMember="DefaultView"
AutoPostBack="True" AppendDataBoundItems="true">
<asp:CompareValidator runat="server" ErrorMessage="CustomValidator"
id="CompareValidator 1" ControlToValidate="ctl_03"
ValueToCompare="California" type="String" Operator="Equal" >
<asp:ListItem Selected="True" Value="California" Text="California" />
</asp:CompareValidator >

<asp:ListItem Value="" Text="" />
</asp:DropDownList>

I think you are missing the purpose of a validator here.
A validator will produce output preventing a form from being submitted if it
is not filled in properly, it will not produce output depending on a value.

Riki
Apr 27 '07 #5
I agree with Riki, the validator controls are not going to help you here. If
the user selects California from the dropdown list, the selected Attribute
will be set to True automatically upon postback. If you want California to
be automatically selected based on a value from the database, you are going
to have to write some server-side code (C# or VB).

EX:
if(!Page.IsPostBack)
ctl_03.SelectedValue = "California";
--
Thanks,
Page Brooks
www.explosivedog.com
"Riki" wrote:
"Chris G." <Ch****@discussions.microsoft.comwrote in message
news:EC**********************************@microsof t.com...
I'm trying to put conditional code in a dropdown that would check the
value
of the current ListItem and set Selected=true depending on that value, and
Selected=false if it's any other value.

Due to security restrictions, I can't place any script blocks on, or
behind,
the page, so it has to be done in the .Net framework.

I've tried using the CompareValidator but that throws errors. (see code
block below)

Any ideas?

<asp:DropDownList runat="server" id="ctl_03" DataTextField="StateName"
DataSourceID="StateList" DataValueField="StateName"
DataMember="DefaultView"
AutoPostBack="True" AppendDataBoundItems="true">
<asp:CompareValidator runat="server" ErrorMessage="CustomValidator"
id="CompareValidator 1" ControlToValidate="ctl_03"
ValueToCompare="California" type="String" Operator="Equal" >
<asp:ListItem Selected="True" Value="California" Text="California" />
</asp:CompareValidator >

<asp:ListItem Value="" Text="" />
</asp:DropDownList>

I think you are missing the purpose of a validator here.
A validator will produce output preventing a form from being submitted if it
is not filled in properly, it will not produce output depending on a value.

Riki
Apr 27 '07 #6

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

Similar topics

0
by: Tom Pearson | last post by:
I create controls and validators dynamically dependent on data at runtime. I create the control then the relevant validator(s) for it assigning the Control.ID as the control to validate. These...
1
by: Assimalyst | last post by:
Hi, I'm pretty new to javascript and i'm struggling to get a custom validator working. I have a dropdownlist populated by a datareader, the top value of which is always "Please Select..." at...
2
by: Wes Weems | last post by:
I'd like to have a 3 values in a drop down, and have a validator not allow a form submit unless its 2 of the 3 values (eg, one of the values says "select one") I used custom validator and did a...
2
by: Cortvriend Andy | last post by:
hi i'm filling an dropdownlist with data from sql server now i want to validate if a selection have been made the problem is that the validator never shows up i think he always says that...
7
by: Lastie | last post by:
Hi all, I’ve got a ‘dropdownlist’ web control and I can add ‘listitem’ no problem. I can also bind data from an SQL database fine. My problem is that I want to do both at the same...
5
by: gane kol | last post by:
Hi I have a custom validator that validates a dropdownlist with a listitem value as "". But the clientvalidation function is never called. but if i use value = "0" and check it in javascript, it...
2
by: Bas Groeneveld | last post by:
Hi I want to detect a name in brackets - eg (Bas) - being selected in a dropdownlist. I intended to use the RegEx validator with ^\(.*\)$ which should work. My problem is that the value of...
2
by: akshalika | last post by:
Hi, I have a repeater control. it dynamically bind textbox or dropdown base on some condition. i want to bind required field validator dynamically for validate textbox or dropdown. here is my...
0
by: cmrhema | last post by:
Hi, I populate two dropdownlist in asp.net. Both are assigned to a required field validator. The codebehind is as below if (!Page.IsPostBack) { DataTable dt = new...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.