Wrox Programmer Forums
|
BOOK: Beginning ASP.NET 4 : in C# and VB
This is the forum to discuss the Wrox book Beginning ASP.NET 4: in C# and VB by Imar Spaanjaars; ISBN: 9780470502211
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 4 : in C# and VB 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 January 6th, 2011, 11:25 PM
Registered User
 
Join Date: Jan 2011
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
Default Validation Problems in Chapter 9

Hi all,

While following the validation controls procedure for the contact form in chapter 9, I have trouble displaying the "messagebox" the ValidationSummary control is supposed to show. When ShowSummary is set to true, the page displays fine right underneath the contact form. But when Messagebox is activated, nothing happens. I made sure Javascript is enabled, double checked my code, allowed pop ups on Chrome, IE, and FireFox and it still won't work. Everything worked up to this point, including the phone number validation control.

Please help!
-Peter
 
Old January 7th, 2011, 08:41 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

Can ypu post the markup for the page and its code behind? If you post code, please paste it in Notepad first to remove color coding and then use the forum editor's Code toolbar button to wrap your code in code tags.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old January 7th, 2011, 12:39 PM
Registered User
 
Join Date: Jan 2011
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Code for Contact Form Control (ContactForm.ascx):

Code:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ContactForm.ascx.cs" Inherits="Controls_ContactForm" %>
<style type="text/css">
  .style1
  {
    width: 100%;
  }
</style>
<script type="text/javascript">
  function ValidatePhoneNumbers(source, args)
  {
    var phoneHome = document.getElementById('<%= PhoneHome.ClientID %>');
    var phoneBusiness = doc.getElementByID('<%= PhoneBusiness.ClientID %>');
    if (phoneHome.value != '' || phoneBusiness.value != '')
    {
      args.isValid = true;
    }
    else
    {
      args.isValid = false;
    }
  }
</script>
<table class="style1" runat="server" id="FormTable">
  <tr>
    <td colspan="3">
      <h1>
        Get in touch with us</h1>
      <p>
        Use the form below to get in touch with us.&nbsp; Enter your name, e-mail address,
        and your home or business phone number to get in touch with us.</p>
    </td>
  </tr>
  <tr>
    <td>
      Name
    </td>
    <td>
      <asp:TextBox ID="Name" runat="server"></asp:TextBox>
    </td>
    <td>
      <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="Name"
        CssClass="ErrorMessage" ErrorMessage="Enter your name">*</asp:RequiredFieldValidator>
    </td>
  </tr>
  <tr>
    <td>
      E-mail address
    </td>
    <td>
      <asp:TextBox ID="EmailAddress" runat="server"></asp:TextBox>
    </td>
    <td>
      <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="EmailAddress"
        CssClass="ErrorMessage" Display="Dynamic" ErrorMessage="Enter an e-mail address">*</asp:RequiredFieldValidator>
      <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="EmailAddress"
        CssClass="ErrorMessage" Display="Dynamic" ErrorMessage="Enter a valid e-mail address"
        ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">*</asp:RegularExpressionValidator>
    </td>
  </tr>
  <tr>
    <td>
      Confirm e-mail address
    </td>
    <td>
      <asp:TextBox ID="ConfirmEmailAddress" runat="server"></asp:TextBox>
    </td>
    <td>
      <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="ConfirmEmailAddress"
        CssClass="ErrorMessage" Display="Dynamic" ErrorMessage="Confirm the e-mail address">*</asp:RequiredFieldValidator>
      <asp:CompareValidator ID="CompareValidator1" runat="server" ControlToCompare="EmailAddress"
        ControlToValidate="ConfirmEmailAddress" CssClass="ErrorMessage" Display="Dynamic"
        ErrorMessage="Retype the e-mail address">*</asp:CompareValidator>
    </td>
  </tr>
  <tr>
    <td>
      Home phone number
    </td>
    <td>
      <asp:TextBox ID="PhoneHome" runat="server" ClientIDMode="Static"></asp:TextBox>
    </td>
    <td>
      <asp:CustomValidator ID="CustomValidator1" runat="server" ClientValidationFunction="ValidatePhoneNumbers"
        CssClass="ErrorMessage" Display="Dynamic" ErrorMessage="Enter your home or business phone number"
        OnServerValidate="CustomValidator1_ServerValidate">*</asp:CustomValidator>
    </td>
  </tr>
  <tr>
    <td>
      Business phone number
    </td>
    <td>
      <asp:TextBox ID="PhoneBusiness" runat="server" ClientIDMode="Static"></asp:TextBox>
    </td>
    <td>
      &nbsp;
    </td>
  </tr>
  <tr>
    <td>
      Comments
    </td>
    <td>
      <asp:TextBox ID="Comments" runat="server" Height="101px" TextMode="MultiLine" Width="484px"></asp:TextBox>
    </td>
    <td>
      <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="Comments"
        CssClass="ErrorMessage" Display="Dynamic" ErrorMessage="Enter a comment">*</asp:RequiredFieldValidator>
    </td>
  </tr>
  <tr>
    <td>
      &nbsp;
    </td>
    <td>
      <asp:Button ID="SendButton" runat="server" Text="Send" OnClick="SendButton_Click" />
    </td>
    <td>
      &nbsp;
    </td>
  </tr>
  <tr>
    <td colspan="3">
      <asp:ValidationSummary ID="ValidationSummary1" runat="server" CssClass="ErrorMessage" HeaderText="Please correct the following errors before you press the Send button:" 
        ShowMessageBox="True" ShowSummary="False" />
    </td>
  </tr>
</table>
<asp:Label ID="Message" runat="server" Text="Message Sent" Visible="false" />
Code for Code Behind of ContactForm.ascx (ContactForm.ascx.cs):

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;        //Provides access to the File class for reading the file
using System.Net.Mail;  //Provides access tot he various mail related classes

public partial class Controls_ContactForm : System.Web.UI.UserControl
{
  protected void Page_Load(object sender, EventArgs e)
  {

  }
  protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
  {
    if (!string.IsNullOrEmpty(PhoneHome.Text) || !string.IsNullOrEmpty(PhoneBusiness.Text))
    {
      args.IsValid = true;
    }
    else
    {
      args.IsValid = false;
    }
  }
  protected void SendButton_Click(object sender, EventArgs e)
  {
    if (Page.IsValid)
    {
      string fileName = Server.MapPath("~/App_Data/ContactForm.txt");
      string mailBody = File.ReadAllText(fileName);

      mailBody = mailBody.Replace("##Name##", Name.Text);
      mailBody = mailBody.Replace("##Email##", EmailAddress.Text);
      mailBody = mailBody.Replace("##HomePhone##", PhoneHome.Text);
      mailBody = mailBody.Replace("##BusinessPhone##", PhoneBusiness.Text);
      mailBody = mailBody.Replace("##Comments##", Comments.Text);

      MailMessage myMessage = new MailMessage();
      myMessage.Subject = "Response from web site";
      myMessage.Body = mailBody;

      myMessage.From = new MailAddress("[email protected]", "Peter - Sender");
      myMessage.To.Add(new MailAddress("[email protected]", "Peter - Receiever"));

      SmtpClient mySmtpClient = new SmtpClient();
      mySmtpClient.Send(myMessage);

      Message.Visible = true;
      FormTable.Visible = false;
    }
  }
}
 
Old January 7th, 2011, 12:53 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

Take a look at this:

Code:
 
var phoneHome = document.getElementById('<%= PhoneHome.ClientID %>');
var phoneBusiness = doc.getElementByID('<%= PhoneBusiness.ClientID %>');
The second line uses doc.getElementByID (doc versus document and ID versus Id). Once you fix that, the popup will appear.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
The Following User Says Thank You to Imar For This Useful Post:
PMooney90 (January 7th, 2011)
 
Old January 7th, 2011, 01:02 PM
Registered User
 
Join Date: Jan 2011
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Wow, thank you so much. I don't know how I missed that. It was driving me crazy!
 
Old August 31st, 2011, 08:09 AM
Authorized User
 
Join Date: Apr 2011
Posts: 11
Thanks: 4
Thanked 0 Times in 0 Posts
Default

Wow, had pretty much the exact same problem...except that I had the 'D' capital in the var phoneHome part. That is tricky since you have to have 'Id' and then 'ID' on the same line, written differently and with no warnings or errors getting flagged in VWD.

Thanks for your close eye to detail Imar, and for these forums...otherwise that could have taken me forever to figure out.
 
Old August 31st, 2011, 06:53 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Ha, yes, the joy of case-sensitive languages which gets even better if you're mixing two of those with different naming conventions..... ;-)

Glad this post was helpful.

Cheers.

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!





Similar Threads
Thread Thread Starter Forum Replies Last Post
validation problems rlindsey Visual Web Developer 2008 0 November 22nd, 2010 04:40 AM
Chapter 9 - Validation question Rushino BOOK: Beginning ASP.NET 4 : in C# and VB 3 April 26th, 2010 03:47 AM
validation problem Chapter 8 Glenn BOOK: Beginning Oracle Application Express ISBN: 9780470388372 3 January 16th, 2010 01:32 AM
XML schema validation problems BrendonMelville XML 3 February 15th, 2006 06:02 PM





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