Return to the menu   Select another DES Module

Demonstrates the SelectedIndexRangesValidator

The SelectedIndexRangesValidator evaluates the SelectedIndex property on ListBoxes, DropDownLists, and RadioButtonLists. It is an extension of the idea behind the SelectedIndexValidator where you indicate a SelectedIndex that’s valid. In this Validator, you provide one or more ranges that the SelectedIndex can match to. For example, if you have a dropdownlist of 20 items and you want items 1, 10-12, and 15-20 to be valid, this is the right Validator.

Specify the control to evaluate with the ControlIDToEvaluate property.

Establish the indexes that the user can select in the Ranges property, a collection where you define ranges in PeterBlum.DES.Web.WebControls.SelectedIndexRange objects. Use the StartIndex and EndIndex properties of PeterBlum.DES.Web.WebControls.SelectedIndexRange to establish a range. When StartIndex = EndIndex, a single index is defined.

Use the NotCondition property to reverse the logic, making all elements in the Ranges property invalid, while those not in the Ranges property are valid.


Controls

ListBox

Values of 2 and 6 thru 8 are legal. The rest are not.



Multiple selection ListBox

Values of 2 and 6 thru 8 are all errors. This uses NotCondition=True.



RadioButtonList

Even numbers are legal.




Source Code (C#)

<script runat="server">
protected void Button1_Click(object sender, EventArgs e) { if (PeterBlum.DES.Globals.WebFormDirector.IsValid) { // save your data here } }
</script> <h2>ListBox</h2> Values of 2 and 6 thru 8 are legal. The rest are not.<br/><br/> <asp:ListBox ID="ListBox1" runat="server" > <asp:ListItem>1</asp:ListItem> <asp:ListItem>2</asp:ListItem> <asp:ListItem>3</asp:ListItem> <asp:ListItem>4</asp:ListItem> <asp:ListItem>5</asp:ListItem> <asp:ListItem>6</asp:ListItem> <asp:ListItem>7</asp:ListItem> <asp:ListItem>8</asp:ListItem> </asp:ListBox> <des:SelectedIndexRangesValidator ID="SelectedIndexRangesValidator1" runat="server" ControlIDToEvaluate="ListBox1" ErrorMessage="Valid values are 2, 6, 7, and 8"> <Ranges> <des:SelectedIndexRange StartIndex="1" EndIndex="1" /> <des:SelectedIndexRange StartIndex="5" /> </Ranges> <ErrorFormatterContainer> <des:TextErrorFormatter></des:TextErrorFormatter> </ErrorFormatterContainer> </des:SelectedIndexRangesValidator> <br/> <br/> <h2>Multiple selection ListBox</h2> Values of 2 and 6 thru 8 are all errors. This uses <span class="PropertyName">NotCondition</span>=True.<br/><br/> <asp:ListBox ID="ListBox2" runat="server" SelectionMode="Multiple"> <asp:ListItem Value="1">1</asp:ListItem> <asp:ListItem>2</asp:ListItem> <asp:ListItem>3</asp:ListItem> <asp:ListItem>4</asp:ListItem> <asp:ListItem>5</asp:ListItem> <asp:ListItem>6</asp:ListItem> <asp:ListItem>7</asp:ListItem> <asp:ListItem>8</asp:ListItem> </asp:ListBox> <des:SelectedIndexRangesValidator ID="SelectedIndexRangesValidator2" runat="server" ControlIDToEvaluate="ListBox2" ErrorMessage="Don't select '2' or above '6'" NotCondition="true"> <Ranges> <des:SelectedIndexRange StartIndex="1" EndIndex="1" /> <des:SelectedIndexRange StartIndex="5" /> </Ranges> </des:SelectedIndexRangesValidator> <br/><br/> <h2>RadioButtonList</h2> Even numbers are legal.<br/><br/> <asp:RadioButtonList ID="RadioButtonList1" runat="server"> <asp:ListItem>1</asp:ListItem> <asp:ListItem>2</asp:ListItem> <asp:ListItem>3</asp:ListItem> <asp:ListItem>4</asp:ListItem> <asp:ListItem>5</asp:ListItem> <asp:ListItem>6</asp:ListItem> <asp:ListItem>7</asp:ListItem> <asp:ListItem>8</asp:ListItem> </asp:RadioButtonList> <des:SelectedIndexRangesValidator ID="SelectedIndexRangesValidator3" runat="server" ControlIDToEvaluate="RadioButtonList1" ErrorMessage="Must select an even number"> <Ranges> <des:SelectedIndexRange StartIndex="1" EndIndex="1" /> <des:SelectedIndexRange StartIndex="3" EndIndex="3" /> <des:SelectedIndexRange StartIndex="5" EndIndex="5" /> <des:SelectedIndexRange StartIndex="7" EndIndex="7" /> </Ranges> </des:SelectedIndexRangesValidator> <br/> <br/> <des:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click"></des:Button>

Return to the menu   Select another DES Module