Return to the menu   Select another DES Module

Demonstrates the RequiredSelectionValidator

The RequiredSelectionValidator is another validator that checks for required data to be assigned. In this case, it is for non-textual and non-list controls that display data, usually in a HTML table. It supports ListView, DataList, Calendar, MultiSelectionCalendar, MonthYearPicker, and TimePicker.

Specify the control to evaluate with the ControlIDToEvaluate property.

When you use a ListView or DataList control, it only performs server side validation. When you use a DES Calendar, MultiSelectionCalendar, MonthYearPicker or TimePicker, it performs both client and server side validation.

If you want to show the Required Field Marker, set ShowRequiredFieldMarker to true.


Controls

Start by clicking Submit to see existing errors. Notice that the last example of a DataList does not report an error because it only uses server side validation.

DES Calendar

Previous Month
Next Month
November 2024 Select a Different Month
SunMonTueWedThuFriSat
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567
Clear


TimePicker

9:00 AM10:00 AM11:00 AM
12:00 PM1:00 PM2:00 PM
3:00 PM4:00 PM5:00 PM
6:00 PMNone 


MonthYearPicker

2023
JanFebMarAprMayJun
JulAugSepOctNovDec
2024
JanFebMarAprMayJun
JulAugSepOctNovDec
 *

MultiSelectionCalendar

Previous Month
Next Month
November 2024 Select a Different Month
SunMonTueWedThuFriSat
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567
Click on dates to toggle their selection.


DataList

Requires server side validation. Use the "Submit Server side validation only" to see the error. Then click a Select button to remove the error.
Items
Select Item 0
Select Item 1
Select Item 2
Select Item 3
Select Item 4
Select Item 5
Select Item 6
Select Item 7
Select Item 8



Source Code (C#)

<script runat="server">
protected void Page_Load(object sender, EventArgs e) { TimePicker1.Items.AddTimes(new TimeSpan(9, 0, 0), new TimeSpan(18, 0, 0), 60); TimePicker1.Items.Add(new PeterBlum.DES.Web.WebControls.SpecialTimeNoValue("None")); // Load sample data only once, when the page is first loaded. if (!IsPostBack) { DataList1.DataSource = CreateDataSource(); DataList1.DataBind(); } } protected ICollection CreateDataSource() { // Create sample data for the DataList control. DataTable dt = new DataTable(); DataRow dr; // Define the columns of the table. dt.Columns.Add(new DataColumn("Item", typeof(Int32))); dt.Columns.Add(new DataColumn("Qty", typeof(Int32))); dt.Columns.Add(new DataColumn("Price", typeof(double))); // Populate the table with sample values. for (int i = 0; i < 9; i++) { dr = dt.NewRow(); dr[0] = i; dr[1] = i * 2; dr[2] = 1.23 * (i + 1); dt.Rows.Add(dr); } DataView dv = new DataView(dt); return dv; } protected void Item_Command(Object sender, DataListCommandEventArgs e) { // Set the SelectedIndex property to select an item in the DataList. DataList1.SelectedIndex = e.Item.ItemIndex; // Rebind the data source to the DataList to refresh the control. DataList1.DataSource = CreateDataSource(); DataList1.DataBind(); } protected void Button1_Click(object sender, EventArgs e) { if (PeterBlum.DES.Globals.WebFormDirector.IsValid) { // save your data here } } protected void Button2_Click(object sender, EventArgs e) { PeterBlum.DES.Globals.WebFormDirector.Validate(); if (PeterBlum.DES.Globals.WebFormDirector.IsValid) { // save your data here } }
</script> Start by clicking Submit to see existing errors. Notice that the last example of a DataList does not report an error because it only uses server side validation.<br/> <des:Button ID="Button3" runat="server" Text="Submit" OnClick="Button1_Click"></des:Button> <br/> <h2>DES Calendar</h2> <des:Calendar ID="Calendar1" runat="server"> <ClearCommand ShowField="RecommendedRow" /> </des:Calendar> <des:RequiredSelectionValidator ID="RSV1" runat="server" ControlIDToEvaluate="Calendar1" ErrorMessage="Required" /> <br/> <br/> <h2>TimePicker</h2> <des:TimePicker ID="TimePicker1" runat="server" > </des:TimePicker> <des:RequiredSelectionValidator ID="RequiredSelectionValidator1" runat="server" ControlIDToEvaluate="TimePicker1" ErrorMessage="Required" /> <br/> <br/> <h2>MonthYearPicker</h2> <des:MonthYearPicker ID="MonthYearPicker1" runat="server" Format="FewYears"> </des:MonthYearPicker> <des:RequiredSelectionValidator ID="RequiredSelectionValidator2" runat="server" ControlIDToEvaluate="MonthYearPicker1" ErrorMessage="Required" ShowRequiredFieldMarker="true" /> <br/> <br/> <h2>MultiSelectionCalendar</h2> <des:MultiSelectionCalendar ID="MultipleSelectionCalendar1" runat="server"> </des:MultiSelectionCalendar> <des:RequiredSelectionValidator ID="RequiredSelectionValidator4" runat="server" ControlIDToEvaluate="MultipleSelectionCalendar1" ErrorMessage="Required" /> <br/> <br/> <h2>DataList</h2> Requires server side validation. Use the "Submit Server side validation only" to see the error. Then click a Select button to remove the error.<br/> <asp:DataList ID="DataList1" runat="server" DataKeyField="Item" GridLines="Both" CellPadding="3" CellSpacing="0" OnItemCommand="Item_Command" > <HeaderStyle BackColor="#aaaadd" /> <AlternatingItemStyle BackColor="Gainsboro" /> <SelectedItemStyle BackColor="Yellow" /> <HeaderTemplate> Items </HeaderTemplate> <ItemTemplate> <asp:LinkButton ID="SelectButton" Text="Select" CommandName="Select" runat="server" /> Item <%# DataBinder.Eval(Container.DataItem, "Item")%> </ItemTemplate> <SelectedItemTemplate> Item: <asp:Label ID="ItemLabel" Text='<%# DataBinder.Eval(Container.DataItem, "Item")%>' runat="server" /> <br/> Quantity: <asp:Label ID="QtyLabel" Text='<%# DataBinder.Eval(Container.DataItem, "Qty")%>' runat="server" /> <br/> Price: <asp:Label ID="PriceLabel" Text='<%# DataBinder.Eval(Container.DataItem, "Price", "{0:c}")%>' runat="server" /> </SelectedItemTemplate> </asp:DataList> <des:RequiredSelectionValidator ID="RequiredSelectionValidator5" runat="server" ControlIDToEvaluate="DataList1" ErrorMessage="Required" /> <br/><br/> <des:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click"></des:Button> <des:Button ID="Button2" runat="server" Text="Submit Server side validation only" OnClick="Button2_Click" CausesValidation="false"></des:Button>

Return to the menu   Select another DES Module