0
.aspx file:

Postal Code:

<asp:TextBox runat="server" ID="txtPostalCode" CssClass="inputs" /><br />
 <asp:RegularExpressionValidator ID="regPostalCode" runat="server" ErrorMessage="Should be 5 Digits" ControlToValidate="txtPostalCode" ValidationExpression="\d{5}"></asp:RegularExpressionValidator>
<br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtPostalCode" 
   Display="Dynamic" EnableClientScript="False" onload="RequiredFieldValidator1_Load"></asp:RequiredFieldValidator>
<asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="Cannot be left blank" 
    Display="Dynamic" ControlToValidate="txtPostalCode" onservervalidate="CustomValidator1_ServerValidate"></asp:CustomValidator>

.aspx.cs file :

 protected void RequiredFieldValidator1_Load(object sender, EventArgs e)
{

    if (IsPostBack)
    {
        //get which input TextBox will be validated.
        TextBox tx = (TextBox)this.FindControl(
            RequiredFieldValidator1.ControlToValidate);
        if (string.IsNullOrEmpty(tx.Text))
        {
            RequiredFieldValidator1.ErrorMessage =
                "Required field cannot be left blank.";
        }
    }


}
protected void CustomValidator1_ServerValidate(object source,ServerValidateEventArgs args)
{
    //Test whether the length of the value is more than 6 characters
    if (args.Value.Length <= 5)
    {
        args.IsValid = true;
    }
    else
    {
        args.IsValid = false;
    }
}

It shows me an error on the line : if (string.IsNullOrEmpty(tx.Text)) Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. Iam not sure what to do can someone help me with this i will be very grateful.

2 Answers2

0

The problem most likely is due to to the FindControl method not finding the textbox. If you are using a Master page, you should try using a recursive FindControl method like the one below. For the Root parameter, you can pass this.Master, and Id would be your RequiredFieldValidator1.ControlToValidate.

    TextBox tx = (TextBox)FindControlRecursive(this.Master, RequiredFieldValidator1.ControlToValidate);

Recusive FindControl:

    public static Control FindControlRecursive(Control Root, string Id)
    {
        if (Root.ID == Id)
            return Root;

        foreach (Control Ctl in Root.Controls)
        {
            Control FoundCtl = FindControlRecursive(Ctl, Id);
            if (FoundCtl != null)
                return FoundCtl;
        }

        return null;
    }
gotmilk13531
  • 234
  • 1
  • 6
  • Thanks I will check your method now.You also gave me a hint to look for the findcontrol and masterpage and i made this : TextBox tx = (TextBox)this.Page.Master.FindControl("ContentPlaceHolder1").FindControl("RequiredFieldValidator1").FindControl("txtPostalCode"); – Przemek Giarnot Sep 09 '15 at 14:40
-1

Reference for IsNullOrWhiteSpace method

http://msdn.microsoft.com/en-us/library/system.string.isnullorwhitespace.aspx

Indicates whether a specified string is Nothing, empty, or consists only of white-space characters.

you could do something like this:

if (String.IsNullOrEmpty(tx.Text) || tx.Text.Trim().Length == 0)

The String.IsNullOrEmpty

method used above is equivalent to:

if (tx.Text == null || tx.Text== String.Empty)

Which means you still need to check for your "IsWhiteSpace" case with the .Trim().Length == 0 as per the example.

Reference for IsNullOrEmpty method

http://msdn.microsoft.com/en-us/library/system.string.isnullorempty.aspx

Indicates whether the specified string is Nothing or an Empty string.

In your example you want to make sure your string has a value, which means you want to ensure the string:

  1. Is not null
  2. Is not the empty string (String.Empty / "")
  3. Is not just whitespace
vijay
  • 1
  • 4