Required Field Validator in code behind C#

I am trying to use required field validator in code behind file but it is showing the following error.

Error:

Unable to find control id 'TextBox1' referenced by the 'ControlToValidate' property of 'abcd854'

Note : TextBox1 exists in the page. i have tested it.

Aspx Page

  <%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="Default.aspx.cs" Inherits="WebApplication3._Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">

    <p>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="Button" ValidationGroup="save" />       
    </p>
    <p>
        &nbsp;<asp:ValidationSummary ID="ValidationSummary1" runat="server" />
</asp:Content>

Cs File

   public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //RequiredFieldValidator validator = ControlsValidation.AssignRequiredFieldValidatorToControl(TextBox1, "Field is required", "*", "save");
            //validator.ControlToValidate = ((TextBox)this.Form.FindControl("MainContent").FindControl("TextBox1")).ID;


            RequiredFieldValidator validator = new RequiredFieldValidator();
            validator.ID = "abcd" + new Random().Next(100, 1000);
            validator.ControlToValidate = ((TextBox)this.Form.FindControl("MainContent").FindControl("TextBox1")).ID;
            validator.EnableClientScript = true;
            validator.ErrorMessage = "";
            validator.Text = "*";
            validator.ValidationGroup = "save";
            validator.Display = ValidatorDisplay.Dynamic;

            this.Controls.Add(validator);
        }
    }

Answers


Finally I got the solution. Issue was

 this.Controls.Add(validator);

As we all can see that the textbox is in child page mean " Content Page" so when using the above line of code its add the control in master page in which there is no control with the id "TextBox1".

so after changing the above line of code with :

this.Form.FindControl("MainContent").Controls.Add(validator);

its working perfectly.

Thanks


In asp.net, after rendering your page TextBox's id will be changed(see source code in browser). you can change it's client id mode to static so that it won't change.

Add ClientIDMode="Static" in your textbox

<asp:TextBox ID="TextBox1" runat="server" ClientIDMode="Static"></asp:TextBox> 

Try This...

Code behind

oTexbox1.Attributes["required"] = "true";

Try using ClientID instead of ID

RequiredFieldValidator validator = new RequiredFieldValidator();
validator.ID = "abcd" + new Random().Next(100, 1000);

validator.ControlToValidate = ((TextBox)this.Form
             .FindControl("MainContent").FindControl("TextBox1")).ClientID;

validator.EnableClientScript = true;
validator.ErrorMessage = "";
validator.Text = "*";
validator.ValidationGroup = "save";
validator.Display = ValidatorDisplay.Dynamic;

this.Controls.Add(validator);

Need Your Help

Error while building qt project for android

android qt

I downloaded Qt Creator 3.3.0 Based on Qt 5.4.0 on Windows.

Javascript ORM Library

javascript database orm web-sql

I am currently working on an application that stores all the data locally on the device,