Quantcast
Channel: SharePoint 2010 - Development and Programming forum
Viewing all articles
Browse latest Browse all 11571

Add Validator Controls programmatically in an ASP.net page

$
0
0

I have a text box in asp.net page like below..

<asp:TextBox ID="txtname" runat="server"></asp:TextBox>

Now, I have added one validator control in Page_Load dynamically...

protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack)
            {
                RequiredFieldValidator validator = new RequiredFieldValidator();
                validator.ControlToValidate = "txtname";
                validator.ErrorMessage = "TExtbox 4 is mandatory";
                validator.Attributes.Add("runat", "server");
                validator.ID = "validatorID";
                this.Form.Controls.Add(validator);
            }            
        }

But while running it is giving error below...

System.Web.HttpException: Unable to find control id 'txtname' referenced by the 'ControlToValidate' property of 'validatorID'.

Please suggest where to correct...:)


sripada mishra


Viewing all articles
Browse latest Browse all 11571

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>