Add table in html
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>How to create dynamic checkbox</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Table ID="tblDynamic" runat="server" Width="600" Height="200">
</asp:Table>
</div>
</form>
</body>
</html>
C# Code Add 1 table row and 2 cells
protected void Page_Load(object sender, EventArgs e)
{
// create a table row
TableRow tbrow = new TableRow();
TableCell labelcell = new TableCell();
TableCell txtcell = new TableCell();
TableCell btncell = new TableCell();
Create dynamic label
// Create a new label
Label namelbl = new Label();
namelbl.Text = "First Name :";
namelbl.ID = "lblfirstname";
namelbl.Font.Bold = true;
// add the lable to the cell
labelcell.Controls.Add(namelbl);
// add cell to row
tbrow.Cells.Add(labelcell);
Create dynamic textbox
// create a new textbox
TextBox txtfirstname = new TextBox();
txtfirstname.ID = "txtfirstname";
txtfirstname.Height = 20;
txtfirstname.Width = 200;
// add the textbox to the cell
txtcell.Controls.Add(txtfirstname);
Create dynamic RequiredFieldValidator
// Add validation control
RequiredFieldValidator Rvalidator = new RequiredFieldValidator();
Rvalidator.ID = "Rvalidator";
Rvalidator.ControlToValidate = txtfirstname.ID;
Rvalidator.Text = "This information is required";
Rvalidator.ErrorMessage = "This information is required";
Rvalidator.Display = ValidatorDisplay.Dynamic;
// add the validation control to textbox cell
txtcell.Controls.Add(Rvalidator);
// add textbox cell to row
tbrow.Cells.Add(txtcell);
Create dynamic button
// create a button
Button btnchekc = new Button();
btnchekc.ID = "btnchekc";
btnchekc.Text = "Validation Check";
btnchekc.Height = 30;
btnchekc.CausesValidation = true;
btncell.Controls.Add(btnchekc);
// add cell to row
tbrow.Cells.Add(btncell);
// add the row to table
tblDynamic.Rows.Add(tbrow);
Now the full code
protected void Page_Load(object sender, EventArgs e)
{
// create a table row
TableRow tbrow = new TableRow();
TableCell labelcell = new TableCell();
TableCell txtcell = new TableCell();
TableCell btncell = new TableCell();
// Create a new label
Label namelbl = new Label();
namelbl.Text = "First Name :";
namelbl.ID = "lblfirstname";
namelbl.Font.Bold = true;
// add the lable to the cell
labelcell.Controls.Add(namelbl);
// add cell to row
tbrow.Cells.Add(labelcell);
// create a new textbox
TextBox txtfirstname = new TextBox();
txtfirstname.ID = "txtfirstname";
txtfirstname.Height = 20;
txtfirstname.Width = 200;
// add the textbox to the cell
txtcell.Controls.Add(txtfirstname);
// Add validation control
RequiredFieldValidator Rvalidator = new RequiredFieldValidator();
Rvalidator.ID = "Rvalidator";
Rvalidator.ControlToValidate = txtfirstname.ID;
Rvalidator.Text = "This information is required";
Rvalidator.ErrorMessage = "This information is required";
Rvalidator.Display = ValidatorDisplay.Dynamic;
// add the validation control to textbox cell
txtcell.Controls.Add(Rvalidator);
// add textbox cell to row
tbrow.Cells.Add(txtcell);
// create a button
Button btnchekc = new Button();
btnchekc.ID = "btnchekc";
btnchekc.Text = "Validation Check";
btnchekc.Height = 30;
btnchekc.CausesValidation = true;
btncell.Controls.Add(btnchekc);
// add cell to row
tbrow.Cells.Add(btncell);
// add the row to table
tblDynamic.Rows.Add(tbrow);
}
OutputClick the button to validate
No comments:
Post a Comment