Validator in DropDownList?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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>
 
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. said:
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
 
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
 
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.
 
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";
 
Back
Top