Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 3.5 > ASP.NET 3.5 Basics
|
ASP.NET 3.5 Basics If you are new to ASP or ASP.NET programming with version 3.5, this is the forum to begin asking questions. Please also see the Visual Web Developer 2008 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 3.5 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 26th, 2010, 11:19 AM
Authorized User
 
Join Date: Jul 2009
Posts: 61
Thanks: 15
Thanked 1 Time in 1 Post
Default Error checking length of data entered in a web form

Hi
I have a web form requesting information regarding a business. One of the fields is Business Description which may not exceed 1500 characters.

My page starts like this:
Code:
<%@ Page Title="" Language="VB" MasterPageFile="~/MasterPages/ManagementMaster.master" AutoEventWireup="false" CodeFile="Members_AddEdit.aspx.vb" Inherits="Management_Members_AddEdit" %>

<%@ Register Assembly="System.Web.DynamicData, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
  Namespace="System.Web.DynamicData" TagPrefix="cc1" %>
  
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="cpmainContent" Runat="Server">
 <script type="text/javascript">
   function ClValidateText(oSrc, args) 
   {
     var txtDesc = document.getElementById('<%= tbBusDesc.ClientID %>');

     if (txtDesc.value.length == 10) 
     {
       args.IsValid = 'true'
     }
     else
     {
       args.IsValid = 'False'
     }
   }
</script>
The Details View entries for the field are:
Code:
<EditItemTemplate>
          <asp:TextBox ID="tbBusDesc" runat="server" type="multiline" Width="500" Height="100" Text='<%# Bind("BusinessDescription") %>' ></asp:TextBox>
          <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Please enter a description of the business" ControlToValidate="tbBusDesc" Text="*"></asp:RequiredFieldValidator>
          <asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="Business Description Exceeds 1500 characters" ControlToValidate="tbBusDesc1" Text="*" OnServervalidate="ValidateText10" Display="Dynamic" ></asp:CustomValidator>
        </EditItemTemplate>
        <InsertItemTemplate>
          <asp:TextBox ID="tbBusDesc" runat="server" type="multiline" Width="500" Height="100" Text='<%# Bind("BusinessDescription") %>' TextMode="MultiLine"></asp:TextBox>
          <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="Please enter a description of the business" ControlToValidate="tbBusDesc" Text="*"></asp:RequiredFieldValidator>
          <asp:CustomValidator ID="CustomValidator2" runat="server" ErrorMessage="Business Description Exceeds 1500 characters" ControlToValidate="tbBusDesc2" Text="*" OnServerValidate="ValidateText10" ClientValidationFunction="ClValidateText"></asp:CustomValidator>       
        </InsertItemTemplate>

The server side check works fine:
Code:
    Protected Sub ValidateText10(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs)
        args.IsValid = args.Value.Length <= 1500
    End Sub
(before I added the script for client side checking)
When I try build the page in VS (using ctlF5) I get build errors referencing Line1 and Line 12
Code:
var txtDesc = document.getElementById('<%= tbBusDesc.ClientID  %>');
Name tbBusDesc is not declared.
Presumably missing something simple but cannot see what :(
__________________
Geoff Baldwin
 
Old March 26th, 2010, 12:38 PM
Lee Dumond's Avatar
Wrox Author
 
Join Date: Jan 2008
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
Default

I presume it is because tbBusDesc is embedded inside a template, which means you cannot access it directly using <%= tbBusDesc.ClientID %>.

Generally, you need to use the FindControl method to access controls inside of templates.
__________________
Visit my blog at http://leedumond.com
Follow me on Twitter: http://twitter.com/LeeDumond

Code:
if (this.PostHelpedYou)
{
   ClickThanksButton(); 
}





Similar Threads
Thread Thread Starter Forum Replies Last Post
Calculation using previously entered data in a form wobmat Access VBA 3 July 22nd, 2009 07:17 PM
retaining the last data that was entered Calibus Classic ASP Databases 4 October 14th, 2004 03:31 PM
How to check data entered 3 textboxs minhpx General .NET 1 September 13th, 2004 11:46 PM
checking string length for form validation Tachyon Beginning PHP 1 June 14th, 2004 08:16 PM
Maintain entered data sporkman43 Classic ASP Basics 2 October 14th, 2003 02:11 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.