Dropdown Validation

Hi

I Wonder if you could help - I have an ASP.NET web form (c#) that includes a dropdown box.  I would like to add validation to the page to ensure that the default option is not selected (in this case "Select")

I have attempted to use customer validation shown below

<asp:dropdownlist id="cmbPrioity" runat="server"></asp:dropdownlist>
<asp:customvalidator ID="ValidatorPrioity" runat="server" ErrorMessage="Please Select a Prioity" ControlToValidate="cmbPrioity" ClientValidationFunction="ValidatorPrioity"></asp:customvalidator>

then the function

public void ValidateStatus(object source, ServerValidateEventArgs args)
                  {
                        if (cmbStatus.SelectedItem.Equals("Select"))
                        {
                              args.IsValid = false;
                        }
                        else {args.IsValid = true;}
                  }

to submit the form i have a button which so far runs this function

            private void Button1_Click(object sender, System.EventArgs e)
            {
                  if (Page.IsValid == true)
                  {
                        Button1.Text = "Valid";
                        }
            }

But this only seems to validate the first time the dropdown is changed and i need it to work when submitted

Your help is appreciated - I realise what i have done is wrong

Thanks

Matt
caffermAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

YurichCommented:
does it validate once only if it's true or only if it's false. try to reset the Page.IsValid in the page_load but don't put it in (!IsPostBack) - just for every page_load.

regs,
yurich
martie_11Commented:
cafferm,

Here's what you do:

1)  Add a RequiredFieldValidator to your page
2)  Set the 'ControlToValidate' property to your drop down (i.e. "cmbPrioity" in your case)
3)  Set the 'InitialValue' property of the validator to your default value (i.e. "Select" in your case)
4)  That's it...your done!

Now, if the user tries to submit and the default/initial selection is still selected, the validation will get triggered!

They should document that better : )

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
BurntSkyCommented:
Here's what I usually do.  Create a CustomValidator like this:

<asp:CustomValidator id="CustomValidator1" runat="server" ErrorMessage="This is a required field." Display="None" ClientValidationFunction="isItemSelected" ControlToValidate="DropDownList1" />

Then include a javascript method like so:

function isItemSelected(s, args)
{
      if(args.Value != 0)
            args.IsValid = true;
      else
      {
            var item = document.getElementById(s.controltovalidate);
            item.style.color='red';
            args.IsValid = false;
      }
}
caffermAuthor Commented:
Thanks martie_11

thats nice and simple - I agree why can't they just write that

thanks

Matt
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.