Hi muhammad12,
Refer below sample.
HTML
<asp:Panel runat="server" ID="pnlDynamic"></asp:Panel>
<br />
<asp:Label ID="lblDateTime" runat="server" />
Namespaces
C#
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
VB.Net
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Imports System.Drawing
Code
C#
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);
tr.Cells.Add(cell);
}
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);
}
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");
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim dt As DataTable = New DataTable()
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim query As String = "SELECT * FROM [Event]"
query += "ORDER BY "
query += "CASE WHEN property = 'TextBox' THEN 0 "
query += "WHEN property = 'Button' THEN 1 "
query += "END"
Using con As SqlConnection = New SqlConnection(conString)
Using cmd As SqlCommand = New SqlCommand(query, con)
Using sda As SqlDataAdapter = New SqlDataAdapter(cmd)
sda.Fill(dt)
End Using
End Using
End Using
Dim textBoxes As DataRow() = dt.Select("Property='TextBox'")
Dim buttons As DataRow() = dt.Select("Property='Button'")
Dim table As Table = New Table()
Dim tr As TableRow = New TableRow()
For i As Integer = 0 To textBoxes.Length - 1
Dim cell As TableCell = New TableCell()
Dim lbl As Label = New Label()
lbl.Text = textBoxes(i)("EventName").ToString()
cell.Controls.Add(lbl)
Dim lt As Literal = New Literal()
lt.Text = "<br /><br />"
cell.Controls.Add(lt)
Dim txt As TextBox = New TextBox()
txt.ID = "txt" & textBoxes(i)("EventName").ToString()
cell.Controls.Add(txt)
tr.Cells.Add(cell)
Next
table.Rows.Add(tr)
tr = New TableRow()
For i As Integer = 0 To buttons.Length - 1
Dim cell As TableCell = New TableCell()
Dim lbl As Label = New Label()
lbl.Text = buttons(i)("EventName").ToString()
cell.Controls.Add(lbl)
Dim lt As Literal = New Literal()
lt.Text = "<br /><br />"
cell.Controls.Add(lt)
Dim btn As Button = New Button()
btn.Text = "Time"
btn.ID = "btn" & buttons(i)("EventName").ToString()
btn.BackColor = Color.Green
AddHandler btn.Click, AddressOf OnSubmit
cell.Controls.Add(btn)
lt = New Literal()
lt.Text = "<br /><br />"
cell.Controls.Add(lt)
Dim lblTime As Label = New Label()
lblTime.ID = "lbl" & buttons(i)("EventName").ToString()
cell.Controls.Add(lblTime)
tr.Cells.Add(cell)
Next
table.Rows.Add(tr)
pnlDynamic.Controls.Add(table)
End Sub
Protected Sub OnSubmit(ByVal sender As Object, ByVal e As EventArgs)
Dim btnId As String = (TryCast(sender, Button)).ID
Dim lblId As String = (TryCast(sender, Button)).ID.Replace("btn", "lbl")
TryCast(sender, Button).Enabled = False
TryCast(pnlDynamic.FindControl(btnId), Button).BackColor = Color.Gray
TryCast(pnlDynamic.FindControl(lblId), Label).Text = DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss")
End Sub
Screenshot