Frage

How to put Validation in dynamicaly html table with asp.net C#? ? I want RequireField and some other validator with this code. . 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);

    }
}

}

War es hilfreich?

Lösung

Create a RequiredFieldValidator control, set the ControlToValidate property to the ID of the control you wish to validate, and add the new Validator control to the table cell.

RequiredFieldValidator req = new RequiredFieldValidator();
req.ControlToValidate = txtEmail.ID;
// set additional properties here
cell.Controls.Add(req);
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top