• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 357
  • Last Modified:

Validate Range between 2 dropdown boxes

Hi Experts,
 I have 2 dropdownlists in my webpart.   (project start and project end).  Each dropdown contains integers, which represent years. (ie, 2008, 2009, 2010, etc)

I need to ensure that projects dont span more than 10 years with a validator control.
I've been trying to use rangeValidator, but it gets confused between postbacks and isn't working.

Any ideas?




0
SOS_Helpdesk
Asked:
SOS_Helpdesk
  • 3
  • 3
1 Solution
 
MaxOvrdrv2Commented:
why don't you simply validate it either server-side or client-side? i don't like validators :S

simply check it.
0
 
SOS_HelpdeskAuthor Commented:
Ah cmon, thats no fun!
Forever trying to get a handle on these .NET controls.

If nobody else can help, i'll have to do it manually and will award points.

Thanks,
0
 
MaxOvrdrv2Commented:
i think the rang validators only work on textboxes... e.g.: range in text should be between 1 and 100... doesn't check 2 controls...
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
SOS_HelpdeskAuthor Commented:
Could a custom-Validator do the trick?
0
 
MaxOvrdrv2Commented:
yes, but it would come back to doing it manually... the only thing is that you would put the validation code inside the validator block and not directly in your save/submit function... that would be the only difference really... but yes, you can do that.
0
 
channa_mCommented:
You can use a dummy textbox to store the value of difference and apply the range validator on the textbox

See below
 <form id="form1" runat="server">
        <div>
            <asp:DropDownList ID="ddlStartDate" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList_SelectedIndexChanged">
                <asp:ListItem>2008</asp:ListItem>
                <asp:ListItem>2009</asp:ListItem>
                <asp:ListItem>2010</asp:ListItem>
                <asp:ListItem>2007</asp:ListItem>
            </asp:DropDownList>
            <asp:DropDownList ID="ddlEndDate" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList_SelectedIndexChanged">
                <asp:ListItem>2008</asp:ListItem>
                <asp:ListItem>2009</asp:ListItem>
                <asp:ListItem>2010</asp:ListItem>
                <asp:ListItem>2007</asp:ListItem>
            </asp:DropDownList>
            <asp:RangeValidator ID="RangeValidator1" runat="server" ControlToValidate="TextBox1"
                ErrorMessage="RangeValidator" MaximumValue="10" MinimumValue="0" Visible="false"></asp:RangeValidator>
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></div>
    </form>
 
 
 
 
protected void DropDownList_SelectedIndexChanged(object sender, EventArgs e)
    {
        TextBox1.Text = Convert.ToInt32(ddlStartDate.SelectedValue) - Convert.ToInt32(ddlEndDate.SelectedValue);
    }

Open in new window

0
 
SOS_HelpdeskAuthor Commented:
Custom Validator worked quite nicely.  just finished setting it up, took approximately 20 minutes.  Thanks!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now