This page also implements a two level drop down using static items.

Output
formvalidation.png

ASPX page
<asp:Table ID="Table1" runat="server">
	<asp:TableRow runat="server">
		<asp:TableCell runat="server">Name:</asp:TableCell>
		<asp:TableCell runat="server"><asp:TextBox runat="server" ID="txtName" /></asp:TableCell>
		<asp:TableCell runat="server"><asp:Label runat="server" ID="lblNameError" ForeColor="Red" style="display:none" /></asp:TableCell>
	</asp:TableRow>
	<asp:TableRow runat="server">
		<asp:TableCell runat="server">Favorite Color:</asp:TableCell>
		<asp:TableCell runat="server"><asp:TextBox runat="server" ID="txtColor" /></asp:TableCell>
		<asp:TableCell runat="server"><asp:Label runat="server" ID="lblColorError" ForeColor="Red" style="display:none" /></asp:TableCell>
	</asp:TableRow>
	<asp:TableRow>
		<asp:TableCell runat="server">Two level dropdown:</asp:TableCell>
		<asp:TableCell runat="server">
			<asp:DropDownList runat="server" ID="ddlLevel1" onchange="FillDropDown()">
				<asp:ListItem Text="1" Value="1" />
				<asp:ListItem Text="2" Value="2" />
			</asp:DropDownList>
			<asp:DropDownList runat="server" ID="ddlLevel2">
				<asp:ListItem Text="10" Value="10" />
				<asp:ListItem Text="11" Value="11" />
				<asp:ListItem Text="12" Value="12" />
			</asp:DropDownList>
		</asp:TableCell>
	</asp:TableRow>
	<asp:TableRow runat="server">
		<asp:TableCell runat="server" ColumnSpan="2" HorizontalAlign="Center">
			<asp:Button runat="server" ID="btnSave" Text="Save" OnClientClick="return JSValidate() != 0;" />
		</asp:TableCell>
	</asp:TableRow>
</asp:Table>

<axial:WFScript runat="server" ID="wfsDropDowns" MethodName="FillDropDown" />
<axial:WFValidator runat="server" ID="wfsValidate" MethodName="JSValidate" />


Codebehind
public bool JSValidate()
{
	bool noErrors = true;
	if (txtName.Text.Length < 3)
	{
		lblNameError.Text = "The name is too short.";
		lblNameError.Style[HtmlTextWriterStyle.Display] = "";
		noErrors = false;
	}
	else
	{
		lblNameError.Style[HtmlTextWriterStyle.Display] = "none";
	}
	if (txtColor.Text.ToLower() != "blue")
	{
		lblColorError.Style[HtmlTextWriterStyle.Display] = "";
		lblColorError.Text = "No, your favor color is blue.";
		noErrors = false;
	}
	else
	{
		lblColorError.Style[HtmlTextWriterStyle.Display] = "";
	}
	if (noErrors)
		return true;
	return noErrors;
}
public void FillDropDown()
{
	ddlLevel2.Items.Clear();
	if (ddlLevel1.SelectedValue == "1")
	{
		ddlLevel2.Items.Add(new ListItem { Text = "10", Value = "10" });
		ddlLevel2.Items.Add(new ListItem { Text = "11", Value = "11" });
		ddlLevel2.Items.Add(new ListItem { Text = "12", Value = "12" });
	}
	else
	{
		ddlLevel2.Items.Add(new ListItem { Text = "4", Value = "4" });
		ddlLevel2.Items.Add(new ListItem { Text = "5", Value = "5" });
		ddlLevel2.Items.Add(new ListItem { Text = "6", Value = "6" });
	}
}

Last edited Aug 5, 2008 at 8:41 PM by thasmin, version 2

Comments

No comments yet.