Demonstrations > Validation demo
Validation demo
It is easy to ensure that either an item has been selected from a DbCombo or that text has been entered.
DbCombo supports the built-in asp.net validation controls by exposing either the Text or Value property to them.
ValidationProperty can be set to Value (default) or Text.
ValidationProperty=Value (default)
Choosing the Value property validates on the hidden Value property. This is useful because if an item from
the results is not selected, the Value property will always be empty. A RequiredFieldValidator can used to
ensure an item is selected.
ValidationProperty=Text
Choosing the Text property will validate on the text entered in the text box.
Demonstration
The Code
The code for this page follows (see the bottom of the page for a link to the entire code listing without interruptions):
First we define our page, and import some namespaces we will be using.
Note that we have specified AutoEventWireup="true". This is important as we will be using the Page_Load function.
<%@ Page
Language="c#"
AutoEventWireup="true" %>
<%@ Register
TagPrefix="DbCombo"
Namespace="Cambro.Web.DbCombo"
Assembly="Cambro.Web.DbCombo" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
This is the start of the page code (something like this is inserted by Visual Studio).
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<html>
<head><title>DemoDataGrid</title></head>
<body MS_POSITIONING="GridLayout"
XMLNS:DbCombo="http://schemas.cambro.net/dbcombo">
<form id="DemoDataGrid" method="post" runat="server">
Our first DbCombo control is validated on its Value property. This is the
default behaviour. This will have the effect that the page will only validate
if an item is selected, as not selecting an item leaves the Value property blank.
<p XMLNS:DbCombo="http://schemas.cambro.net/dbcombo">
<table>
<tr>
<td>
ValidationProperty=Value
</td>
<td>
<DbCombo:DbCombo
Runat="server"
ID="Combo1" />
</td>
<td>
<asp:RequiredFieldValidator
Runat="server"
ControlToValidate="Combo1"
ErrorMessage="Error, no item selected"
Display="Dynamic" />
</td>
</tr>
Our second DbCombo is validated on its Text property. Entering any value in the
Textbox will validate, but leaving the textbox blank will not.
<tr>
<td>
ValidationProperty=Text
</td>
<td>
<DbCombo:DbCombo
Runat="server"
ID="Combo2"
ValidationProperty="Text" />
</td>
<td>
<asp:RequiredFieldValidator
Runat="server"
ControlToValidate="Combo2"
ErrorMessage="Error, no text selected"
Display="Dynamic" />
</td>
</tr>
<tr>
<td colspan=3>
<asp:Button
Runat="server"
OnClick="ButtonClick"
Text="Postback"/>
<asp:Label
Runat="server"
ID="LabelSucccess"
ForeColor="Blue"></asp:Label>
</td>
</tr>
</table>
<script runat=server>
Our Button click event only changes the Text of the Label if the page validates.
protected void ButtonClick(object o, EventArgs e)
{
if (Page.IsValid)
LabelSucccess.Text="Button click event success";
}
This is a simple ServerMethod.
[Cambro.Web.DbCombo.ResultsMethod(true)]
public static object DbComboMethod(
Cambro.Web.DbCombo.ServerMethodArgs args)
{
SqlConnection conn = new SqlConnection(
"your-connection-string");
SqlCommand myCommand = new SqlCommand(@"
SELECT TOP "+args.Top+@"
Word,
RTRIM(Word) as DbComboText,
RTRIM(Word) as DbComboValue
FROM tWords
WHERE Word LIKE @Query
ORDER BY Word", conn);
myCommand.Parameters.Add(
"@Query",
args.Query+"%");
myCommand.Connection.Open();
return myCommand.ExecuteReader(
CommandBehavior.CloseConnection);
}
</script>
</p>
</form>
</body>
</html>
Demonstrations > Validation demo |