Click here to Skip to main content
15,908,264 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
how can i do this throw code...
Posted
Comments
Peter Leow 6-Mar-14 23:42pm    
Write some code.
BillWoodruff 7-Mar-14 0:49am    
You need to make an attempt at getting started, and then come here with specific questions.
nandakishoreroyal 7-Mar-14 4:48am    
update the code what you have tried..

1 solution

IN Aspx Page :

XML
<div>
        <table cellpadding="0" cellspacing="0" border="0" id="tblQuestions" runat="server">
        </table>
    </div>



In Aspx.cs Page:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.UI.HtmlControls;

namespace Practice.Aspx
{
public partial class BuildDynamicControls : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
try
{
BuildDynamicTextBoxesForQuestions();
}
catch (Exception ex)
{
}
}

private void BuildDynamicTextBoxesForQuestions()
{
try
{
DataTable dt = new DataTable();
dt.Columns.Add("Question", typeof(string));
dt.Rows.Add("Question1");
dt.Rows.Add("Question2");
dt.Rows.Add("Question3");
dt.Rows.Add("Question4");
dt.Rows.Add("Question5");
dt.Rows.Add("Question6");
dt.Rows.Add("Question7");
dt.Rows.Add("Question8");
if (dt != null && dt.Rows.Count > 0)
{
HtmlTable tbl = null;
tbl = (HtmlTable)this.FindControl("tblQuestions");
if (tbl != null)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
HtmlTableRow newRow = new HtmlTableRow();
HtmlTableCell newCell1 = new HtmlTableCell();

Label lbl = new Label();
lbl.Text = dt.Rows[i]["Question"].ToString();
lbl.ID = "lbl" + i.ToString();
newCell1.Controls.Add(lbl);
newRow.Cells.Add(newCell1);

HtmlTableCell newCell2 = new HtmlTableCell();
TextBox txtBox = new TextBox();
txtBox.ID = "txt" + i.ToString();
newCell2.Controls.Add(txtBox);
newRow.Cells.Add(newCell2);

tbl.Rows.Add(newRow);
}
}

}
}
catch (Exception ex)
{
}
}
}
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900