Hi muhammad12,
Refer below sample.
SQL
CREATE TABLE [Event]
(
ID INT,
EventName VARCHAR(50),
Property VARCHAR(50)
)
INSERT INTO [Event] VALUES (1, 'Name', 'TextBox')
INSERT INTO [Event] VALUES (2, 'Security', 'Button')
INSERT INTO [Event] VALUES (3, 'Class', 'TextBox')
INSERT INTO [Event] VALUES (4, 'Cleaning', 'Button')
INSERT INTO [Event] VALUES (5, 'Weaving', 'TextBox')
INSERT INTO [Event] VALUES (6, 'Dying', 'Button')
INSERT INTO [Event] VALUES (7, 'Society', 'TextBox')
INSERT INTO [Event] VALUES (8, 'Washing', 'Button')
HTML
<asp:Panel runat="server" ID="pnlDynamic">
</asp:Panel>
Namespaces
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Drawing;
Code
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
string query = @"SELECT * FROM [Event]";
query += "ORDER BY ";
query += "CASE WHEN property = 'TextBox' THEN 0 ";
query += "WHEN property = 'Button' THEN 1 ";
query += "END";
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand(query, con))
{
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
sda.Fill(dt);
}
}
}
DataRow[] textBoxes = dt.Select("Property='TextBox'");
DataRow[] buttons = dt.Select("Property='Button'");
Table table = new Table();
TableRow tr = new TableRow();
for (int i = 0; i < textBoxes.Length; i++)
{
TableCell cell = new TableCell();
Label lbl = new Label();
lbl.Text = textBoxes[i]["EventName"].ToString();
cell.Controls.Add(lbl);
Literal lt = new Literal();
lt.Text = "<br /><br />";
cell.Controls.Add(lt);
TextBox txt = new TextBox();
txt.ID = "txt" + textBoxes[i]["EventName"].ToString();
cell.Controls.Add(txt);
if (i % 3 == 0 && i != 0)
{
tr = new TableRow();
}
else
{
tr.Cells.Add(cell);
}
if (i % 2 == 0 && i != 0)
{
table.Rows.Add(tr);
}
else
{
tr.Cells.Add(cell);
if (textBoxes.Length == i + 1)
{
table.Rows.Add(tr);
}
}
}
tr = new TableRow();
for (int i = 0; i < buttons.Length; i++)
{
TableCell cell = new TableCell();
Label lbl = new Label();
lbl.Text = buttons[i]["EventName"].ToString();
cell.Controls.Add(lbl);
Literal lt = new Literal();
lt.Text = "<br /><br />";
cell.Controls.Add(lt);
Button btn = new Button();
btn.Text = "Time";
btn.ID = "btn" + buttons[i]["EventName"].ToString();
btn.BackColor = Color.Green;
btn.Click += new EventHandler(this.OnSubmit);
cell.Controls.Add(btn);
lt = new Literal();
lt.Text = "<br /><br />";
cell.Controls.Add(lt);
Label lblTime = new Label();
lblTime.ID = "lbl" + buttons[i]["EventName"].ToString();
cell.Controls.Add(lblTime);
//tr.Cells.Add(cell);
if (i % 3 == 0 && i != 0)
{
tr = new TableRow();
}
else
{
tr.Cells.Add(cell);
}
if (i % 2 == 0 && i != 0)
{
table.Rows.Add(tr);
}
else
{
tr.Cells.Add(cell);
if (buttons.Length == i + 1)
{
table.Rows.Add(tr);
}
}
}
//table.Rows.Add(tr);
pnlDynamic.Controls.Add(table);
}
protected void OnSubmit(object sender, EventArgs e)
{
string btnId = (sender as Button).ID;
string lblId = (sender as Button).ID.Replace("btn", "lbl");
(sender as Button).Enabled = false;
(pnlDynamic.FindControl(btnId) as Button).BackColor = Color.Gray;
(pnlDynamic.FindControl(lblId) as Label).Text = DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss");
}
Screenshot