Return to the menu   Select another DES Module

Demonstrates the CompareToStringsValidator

The CompareToStringsValidator determines if your textbox has text that matches to any item in a list of strings that you supply. For example, you can define a list of UserNames to compare to the textbox.

This control supports retrieving data from a data source such as a DataSet, DataTable or a System.Collections.IEnumerable collection, in the same way you populate a ListBox or DropList.

Specify the control to evaluate with the ControlIDToEvaluate property.

The data comes from three properties that are familiar if you have used databinding on ListBoxes or DataGrids: DataSource, DataMember, and DataTextField. However, you don’t need to call the DataBind() method unless you use databinding notation in the ASP.NET text (as shown below.) Alternatively, you can add strings to the Items property. When working with ASP.NET Markup, add PeterBlum.DES.CompareToStringsItem objects to Items. These objects have one property, Value, where you assign the string.

It defaults to a case insensitive match. If you prefer case sensitive, set CaseInsensitive to false.

It defaults to matching the full string entered to each string. Use the Mode property to change it to match these strings to the start of the string, end of the string, or contained in the string.


Controls

Using the DataSource property



Using PeterBlum.DES.CompareToStringItem objects






Source Code (C#)

<script runat="server">
protected override void OnLoad(EventArgs e) { base.OnLoad(e); CreateDataSet(); } /// <summary> /// Creates fDataSet with two strings, used by the first demo /// </summary> protected void CreateDataSet() { fDataSet = new DataSet("dataSet"); fDataSet.Namespace= "NetFrameWork"; DataTable fTable = new DataTable("Categories"); DataColumn fItemColumn = new DataColumn("CategoryName", typeof(string)); fTable.Columns.Add(fItemColumn); fDataSet.Tables.Add(fTable); AddCategory("hardware", fTable); AddCategory("software", fTable); CompareToStringsValidator1.DataBind(); // because we attach to the DataSource using DataBinding. } private void AddCategory(string pCategoryName, DataTable pDestination) { DataRow vNewRow = pDestination.NewRow(); vNewRow["CategoryName"] = pCategoryName; pDestination.Rows.Add(vNewRow); } System.Data.DataSet fDataSet; protected void Button1_Click(object sender, EventArgs e) { if (PeterBlum.DES.Globals.WebFormDirector.IsValid) { // save your data here } }
</script> <h2>Using the DataSource property</h2> <asp:TextBox id="TextBox1" runat="server" /> <des:CompareToStringsValidator id="CompareToStringsValidator1" runat="server" ErrorMessage="Enter a category" ControlIDToEvaluate="TextBox1" DataMember="Categories" DataSource="<%# fDataSet%>" DataTextField="CategoryName"> </des:CompareToStringsValidator> <br/> <br/> <h2>Using PeterBlum.DES.CompareToStringItem objects</h2> <asp:TextBox id="TextBox2" runat="server" /> <des:CompareToStringsValidator id="CompareToStringsValidator2" runat="server" ErrorMessage="Enter a category" ControlIDToEvaluate="TextBox2" > <Items> <des:CompareToStringsItem Value="hardware" /> <des:CompareToStringsItem Value="software" /> </Items> </des:CompareToStringsValidator> <br/><br/> <br/> <br/> <des:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click"></des:Button>

Return to the menu   Select another DES Module