开发者

Programmatically adding validation control to asp.net page

开发者 https://www.devze.com 2022-12-14 13:33 出处:网络
I am trying to add a required fields validator programmatically in asp.net. But I get the following error message - Control \'req2\' of type \'RequiredFieldValidator\' must be placed inside a form tag

I am trying to add a required fields validator programmatically in asp.net. But I get the following error message - Control 'req2' of type 'RequiredFieldValidator' must be placed inside a form tag with runat=server

The c# code i have used is below -

protected void Page_Load(object sender, EventArgs e)
    {
        RequiredFieldValidator rv = new RequiredFieldValidator();
        rv.ID = "req2";
        rv.ControlToValidate = "TextBox2";
     开发者_JAVA百科   rv.ErrorMessage = "Data Required";
        this.Controls.Add(rv);
    }

Could someone tell me whats gone wrong here?

Thanks for sharing your valuable time.


Try adding the control to the Form of the page. The reason for this is that you need to add this type of controls to a form.

this.Form.Controls.Add(rv);
0

精彩评论

暂无评论...
验证码 换一张
取 消