0

如何使用 asp.net C# 将验证放入动态 html 表中?? 我想要 RequireField 和其他一些带有此代码的验证器。. protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { HtmlTable dtbl = new HtmlTable(); dtbl.Border = 5;

        dtbl.BorderColor = "black";
        dtbl.CellPadding = 6;
        dtbl.CellSpacing = 1;
        HtmlTableRow row1;
        for (int i = 0; i <= 2; i++)
        {
            row1 = new HtmlTableRow();
            row1.BgColor = "Orange";

            if (i == 0)
            {
                HtmlTableCell cell;
                for (int j = 1; j <= 4; j++)
                {
                    cell = new HtmlTableCell();
                    cell.Align = "center";
                    switch (j)
                    {
                        case 1:
                            Label l1 = new Label();
                            l1.Text = "Name";
                            l1.ForeColor = Color.White;
                            cell.Controls.Add(l1);
                            break;

                        case 2:
                            Label l2 = new Label();
                            l2.Text = "Age";
                            l2.ForeColor = Color.White;
                            cell.Controls.Add(l2);
                            break;

                        case 3:
                            Label l3 = new Label();
                            l3.Text = "Gender";
                            l3.ForeColor = Color.White;
                            cell.Controls.Add(l3);
                            break;

                        case 4:
                            Label l4 = new Label();
                            l4.Text = "EmailId";
                            l4.ForeColor = Color.White;
                            cell.Controls.Add(l4);
                            break;
                    }
                    row1.Controls.Add(cell);
                }

            }
            if (i == 1)
            {
                HtmlTableCell cell;
                for (int j = 1; j <= 4; j++)
                {
                    cell = new HtmlTableCell();
                    cell.Align = "center";
                    switch (j)
                    {
                        case 1:
                            TextBox t1 = new TextBox();
                            t1.ID = "txtName";
                            t1.ForeColor = Color.Green;
                            cell.Controls.Add(t1);
                            break;

                        case 2:
                            TextBox t2 = new TextBox();
                            t2.ID = "txtAge";
                            t2.ForeColor = Color.Green;
                            cell.Controls.Add(t2);
                            break;

                        case 3:
                            RadioButtonList rbl1 = new RadioButtonList();
                            rbl1.ID = "rbl";
                            rbl1.RepeatDirection = RepeatDirection.Horizontal;
                            rbl1.Items.Add(new ListItem("Male","Male"));
                            rbl1.Items.Add(new ListItem("Female", "Female"));
                            cell.Controls.Add(rbl1);


                            break;

                        case 4:
                            TextBox t4 = new TextBox();
                            t4.ID = "txtEmail";
                            t4.ForeColor = Color.Green;
                            cell.Controls.Add(t4);
                            break;
                    }
                    row1.Controls.Add(cell);
                }
            }
            if (i == 2)
            {
                HtmlTableCell cell = new HtmlTableCell();
                cell.ColSpan = 4;
                Button b1 = new Button();
                cell.Align = "center";
                b1.Style.Add(HtmlTextWriterStyle.BackgroundColor,"Green");
                b1.Style.Add(HtmlTextWriterStyle.Color, "White");
                b1.Style.Add(HtmlTextWriterStyle.BorderStyle, "solid");
                b1.Style.Add(HtmlTextWriterStyle.BorderColor, "White");
                b1.Style.Add(HtmlTextWriterStyle.Height, "25px");
                b1.Style.Add(HtmlTextWriterStyle.Width, "100px");
                b1.ID = "btnSubmit";
                b1.Text = "Submit";
                b1.Click+=new System.EventHandler()
                cell.Controls.Add(b1);
                row1.Controls.Add(cell);
            }
            dtbl.Controls.Add(row1);

        }

        form1.Controls.Add(dtbl);
        Page.Controls.Add(form1);

    }
}

}

4

1 回答 1

2

创建一个RequiredFieldValidator控件,将ControlToValidate属性设置为要验证的控件的 ID,然后将新的 Validator 控件添加到表格单元格中。

RequiredFieldValidator req = new RequiredFieldValidator();
req.ControlToValidate = txtEmail.ID;
// set additional properties here
cell.Controls.Add(req);
于 2012-09-06T08:42:17.060 回答