Validator message disappeared after post back

Discussion in 'ASP .Net Building Controls' started by HipHop, Dec 22, 2006.

  1. HipHop

    HipHop Guest

    Hi,
    I have a compositecontrol contain a textbox (txtPhoneNumber) and a
    RegularExpressionValidator.
    I tested in a page, the validation seems work fine until post back, the
    validation message disappeared after post back while the text in the phone
    number still remain the invalid format.

    Could anybody please help me.
    Thanks.
     
    HipHop, Dec 22, 2006
    #1
    1. Advertisements

  2. Hi HipHop,

    Which control caused the post back? Is its CausesValidation set to true?

    Based on my test code:

    namespace myns
    {
    public class Class1 : CompositeControl
    {
    private TextBox m_txtPhoneNumber;
    private RegularExpressionValidator m_vldPhoneNumber;

    protected override void CreateChildControls()
    {
    Controls.Clear();

    m_txtPhoneNumber = new TextBox();
    m_txtPhoneNumber.ID = "txtPhoneNumber";
    Controls.Add(m_txtPhoneNumber);

    m_vldPhoneNumber = new RegularExpressionValidator();
    m_vldPhoneNumber.ID = "vldPhoneNumber";
    m_vldPhoneNumber.ValidationExpression = @"\d+";
    m_vldPhoneNumber.ControlToValidate = "txtPhoneNumber";
    m_vldPhoneNumber.ErrorMessage = "Invalid phone number";
    m_vldPhoneNumber.EnableClientScript = true;
    Controls.Add(m_vldPhoneNumber);
    }
    }
    }


    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
    Inherits="_Default" %>
    <%@ Register TagPrefix="c" Namespace="myns" Assembly="__code" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
    <title>Untitled Page</title>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
    <c:Class1 ID="c1" runat="server" />
    <asp:Button ID="Button1" runat="server" CausesValidation="False"
    OnClick="Button1_Click"
    Text="Button" /></div>
    </form>
    </body>
    </html>


    This reproduces the exact issue you described. However, this is expected
    since the button's CausesValidation is turned off and the validation will
    not occur.

    If this is not your case, would you please post your code here? Thanks.

    Merry Christmas and Happy New Year!

    Sincerely,
    Walter Wang (, remove 'online.')
    Microsoft Online Community Support

    ==================================================
    Get notification to my posts through email? Please refer to
    http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
    ications. If you are using Outlook Express, please make sure you clear the
    check box "Tools/Options/Read: Get 300 headers at a time" to see your reply
    promptly.

    Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
    where an initial response from the community or a Microsoft Support
    Engineer within 1 business day is acceptable. Please note that each follow
    up response may take approximately 2 business days as the support
    professional working with you may need further investigation to reach the
    most efficient resolution. The offering is not appropriate for situations
    that require urgent, real-time or phone-based interactions or complex
    project analysis and dump analysis issues. Issues of this nature are best
    handled working with a dedicated Microsoft Support Engineer by contacting
    Microsoft Customer Support Services (CSS) at
    http://msdn.microsoft.com/subscriptions/support/default.aspx.
    ==================================================

    This posting is provided "AS IS" with no warranties, and confers no rights.
     
    Walter Wang [MSFT], Dec 25, 2006
    #2
    1. Advertisements

  3. Hi HipHop,

    I am interested in this issue. Would you mind letting me know the result of
    the suggestions? If you need further assistance, feel free to let me know.
    I will be more than happy to be of assistance.

    Have a great day!

    Regards,
    Walter Wang (, remove 'online.')
    Microsoft Online Community Support

    ==================================================
    When responding to posts, please "Reply to Group" via your newsreader so
    that others may learn and benefit from your issue.
    ==================================================

    This posting is provided "AS IS" with no warranties, and confers no rights.
     
    Walter Wang [MSFT], Dec 28, 2006
    #3
  4. HipHop

    HipHop Guest

    Walter, Thank you for your answer. However, I don't have buttons, all I have
    is another textbox which causevalidation set to false, but AutoPostBack set
    to true in order to trigger TextChanged event.

    Here is my code for Conposite Control EmailTextBox (Let's use Email instead
    of PhoneNumber),
    ====================begin of EmailTextBox =====
    using System;

    using System.Collections.Generic;

    using System.Text;

    using System.ComponentModel;

    using System.Web;

    using System.Web.UI;

    using System.Web.UI.WebControls;

    using System.Drawing;



    namespace EnterpriseLibrary.WebControls

    {

    [DefaultProperty("Value")]

    [ToolboxData("<{0}:ncEmailTextBox runat=server></{0}:ncEmailTextBox>")]

    public class ncEmailTextBox : CompositeControl

    {

    private TextBox txtEmail = new TextBox();

    private RegularExpressionValidator VldtrEmail;

    [Bindable(true)]

    [Category("Appearance")]

    [DefaultValue("")]

    [Localizable(true)]

    public string Value

    {

    get

    {

    return txtEmail.Text;

    }

    set

    {

    EnsureChildControls();

    txtEmail.Text = value;

    }

    }




    protected override void CreateChildControls()

    {

    txtEmail.ID = this.ID + "_Email";

    txtEmail.Width = 140;

    VldtrEmail = new RegularExpressionValidator();

    VldtrEmail.ID = this.ID + "_VldtrEmail";

    VldtrEmail.ControlToValidate = txtEmail.ID;

    VldtrEmail.ValidationExpression =
    @"^([0-9a-zA-Z]+[-._+&])*[0-9a-zA-Z]+@([-0-9a-zA-Z]+[.])+[a-zA-Z]{2,6}$";

    VldtrEmail.Text = "Invalid email format.";

    VldtrEmail.Display = ValidatorDisplay.Static;

    this.Controls.Add(txtEmail);

    this.Controls.Add(VldtrEmail);


    base.CreateChildControls();

    }



    public override void RenderControl(System.Web.UI.HtmlTextWriter writer)

    {

    writer.RenderBeginTag(HtmlTextWriterTag.Table);

    writer.RenderBeginTag(HtmlTextWriterTag.Tr);

    writer.RenderBeginTag(HtmlTextWriterTag.Td);

    txtEmail.RenderControl(writer);

    VldtrEmail.RenderControl(writer);

    writer.RenderEndTag(); // td


    writer.RenderEndTag(); // tr

    writer.RenderBeginTag(HtmlTextWriterTag.Tr);

    writer.RenderBeginTag(HtmlTextWriterTag.Td);


    writer.RenderEndTag();//td

    writer.RenderEndTag();//tr

    writer.RenderEndTag(); // table

    }

    protected override void Render(HtmlTextWriter writer)

    {

    base.Render(writer);

    }

    }

    }

    ============ end of EmailTextBox ==================







    Code for the Page:

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="play.aspx.cs"
    Inherits="play" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" >

    <head runat="server">

    <title>Untitled Page</title>

    </head>

    <body>

    <form id="form1" runat="server">

    <table>


    <tr><td>Composite Control Email:</td><td><nc:ncEmailTextBox ID="Email"
    runat="server" /></td></tr>


    <tr><td>Some TextBox with AutoPostBack:</td><td><asp:TextBox ID="TextBox1"
    runat="server" AutoPostBack="true"
    OnTextChanged="TextBox1_TextChanged"></asp:TextBox></td></tr>


    </table>

    </form>

    </body>

    </html>
     
    HipHop, Jan 4, 2007
    #4
  5. Hi HipHop,

    To make the validator work in or before a postback, the control who will
    cause the postback must have CausesValidation set to true. Would you please
    set the TextBox's CausesValidation to true and test it again:

    <asp:TextBox ID="TextBox1" runat="server" CausesValidation="true"
    AutoPostBack="true" OnTextChanged="TextBox1_TextChanged"></asp:TextBox></td>


    I've tested it on my side using your code, if the CausesValidation is set
    to true, the page will not be posted back since client-side validation is
    enabled and the validation result is false, therefore it prevents the page
    being posted back.


    Regards,
    Walter Wang (, remove 'online.')
    Microsoft Online Community Support

    ==================================================
    When responding to posts, please "Reply to Group" via your newsreader so
    that others may learn and benefit from your issue.
    ==================================================

    This posting is provided "AS IS" with no warranties, and confers no rights.
     
    Walter Wang [MSFT], Jan 5, 2007
    #5
  6. HipHop

    HipHop Guest

    Thanks, it works. But what if I have a calendar that required a post back to
    trigger the server event, the error message will disappear after a post
    back.
    Of course it will come back if user did not correct them and push the summit
    button which I check if the page is valid.
    Is this the only way?
     
    HipHop, Jan 8, 2007
    #6
  7. Hi HipHop,

    I've done some research (using Reflector to dig into the implementation of
    Calendar and Button controls), unfortunately it's not possible to extend
    the Calendar to support CausesValidation function as in Button.

    In the Button, it uses customized PostBackOptions to indicate that the
    generated javascript to postback the page will validate the page first. The
    Calendar control's relevant functions are private and not possible to
    override.


    Regards,
    Walter Wang (, remove 'online.')
    Microsoft Online Community Support

    ==================================================
    When responding to posts, please "Reply to Group" via your newsreader so
    that others may learn and benefit from your issue.
    ==================================================

    This posting is provided "AS IS" with no warranties, and confers no rights.
     
    Walter Wang [MSFT], Jan 9, 2007
    #7
  8. Hi Hiphop,

    Not sure if you've seen my previous reply. Please feel free to let me know
    if you want more information on it. Thanks.

    Regards,
    Walter Wang (, remove 'online.')
    Microsoft Online Community Support

    ==================================================
    When responding to posts, please "Reply to Group" via your newsreader so
    that others may learn and benefit from your issue.
    ==================================================

    This posting is provided "AS IS" with no warranties, and confers no rights.
     
    Walter Wang [MSFT], Jan 11, 2007
    #8
  9. HipHop

    HipHop Guest

    Hi, Walter,
    Thank you for your help. I guess I will leave it for now.
     
    HipHop, Jan 17, 2007
    #9
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.