如何创建动态必填字段验证器

时间:2015-05-11 11:30:42

标签: c# asp.net

我使用以下代码生成了一系列必需的字段验证器:

 Panel[] divMain = new Panel[22];
 DropDownList[] gender = new DropDownList[22];
 TextBox[] txtFirstName = new TextBox[22];
 TextBox[] txtMiddleName = new TextBox[22];
 TextBox[] txtLastName = new TextBox[22];
 TextBox[] txtAge = new TextBox[22];
 RequiredFieldValidator[] req = new RequiredFieldValidator[30];

我正在使用以下代码创建一些动态控件:

     for (int i = 0; i < noOfad - 1; i++)
        {


            HtmlGenericControl p = new HtmlGenericControl("p");
            HtmlGenericControl strong = new HtmlGenericControl("strong");
            strong.InnerText = "Adult" + Convert.ToString(i + 2);
            p.Controls.Add(strong);
            divAdultMoreForm.Controls.Add(p);
            Panel div = new Panel();
            HtmlGenericControl p1 = new HtmlGenericControl("p");
            p1.InnerHtml = "<span><strong>Full Name (As per Valid Govt Id Proof valid )</strong></span> <span><strong style='  margin-left: 549px;'>Age(in Years 18 +) </strong></span>";
            div.Controls.Add(p1);

            HtmlGenericControl p2 = new HtmlGenericControl("p");
            gender[i] = new DropDownList();
            gender[i].Items.Add(new ListItem("Title", "0"));
            gender[i].Items.Add(new ListItem("Mr.", "1"));
            gender[i].Items.Add(new ListItem("Mrs.", "2"));
            gender[i].Items.Add(new ListItem("Ms.", "3"));
            gender[i].CssClass = "txt";
            p2.Controls.Add(gender[i]);

            txtFirstName[i] = new TextBox();
            txtFirstName[i].Attributes.Add("placeholder", "First Name");
            txtFirstName[i].CssClass = "txt input";
            txtFirstName[i].ID = "txtFirstName" + Convert.ToString(i);
            req[i].ControlToValidate = txtFirstName[i].ID;//Object refrence not set to an instance ofo an object
            req[i].ForeColor = System.Drawing.Color.Red;
            req[i].ErrorMessage = "*";
            txtFirstName[i].CssClass = "input txt";
            txtMiddleName[i] = new TextBox();
            txtMiddleName[i].Attributes.Add("placeholder", "Middle Name");
            txtMiddleName[i].CssClass = "txt input";
            txtMiddleName[i].CssClass = "input txt";
            txtMiddleName[i].ID = "txtMiddleName" + Convert.ToString(i);
            txtLastName[i] = new TextBox();
            txtLastName[i].Attributes.Add("placeholder", "Last Name");
            txtLastName[i].CssClass = "txt input";
            txtLastName[i].CssClass = "input txt";
            txtLastName[i].ID = "txtLastName" + Convert.ToString(i);
            txtAge[i] = new TextBox();
            txtAge[i].Attributes.Add("placeholder", "Enter Age");
            txtAge[i].CssClass = "txt";
            p2.Controls.Add(txtFirstName[i]);

            p2.Controls.Add(txtMiddleName[i]);

            p2.Controls.Add(txtLastName[i]);

            p2.Controls.Add(txtAge[i]);

            div.Controls.Add(p2);
            divAdultMoreForm.Controls.Add(div);
        }

但是当我分配控件以验证验证器req [i]时,会出现错误对象引用未设置为对象的实例。 无法理解为什么会出现这个问题。 任何纠正我的代码的帮助! 感谢

0 个答案:

没有答案