Hi muhammad12,
Please refer below sample.
HTML
<asp:Panel runat="server" ID="pnlDynamic"></asp:Panel>
<br />
<asp:Label ID="lblDateTime" runat="server" />
Namespaces
C#
using System.Data;
using System.Drawing;
using System.Data.SqlClient;
using System.Configuration;
VB.Net
Imports System.Data
Imports System.Drawing
Imports System.Configuration
Imports System.Data.SqlClient
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
//lblUserId.Text = Session["User"].ToString();
// lblSession.Text = Session.SessionID;
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))
{
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
foreach (DataRow dr in dt.Rows)
{
string type = dr["Property"].ToString().ToUpper();
string eventName = dr["EventName"].ToString();
switch (type)
{
case "TEXTBOX":
Label lbl = new Label();
lbl.Text = eventName + ": ";
pnlDynamic.Controls.Add(lbl);
TextBox txt = new TextBox();
txt.ID = "txt" + eventName;
pnlDynamic.Controls.Add(txt);
Literal lt = new Literal();
lt.Text = "<br /><br />";
pnlDynamic.Controls.Add(lt);
break;
case "BUTTON":
Label lblEventName = new Label();
lblEventName.Text = eventName + ": ";
pnlDynamic.Controls.Add(lblEventName);
Button btn = new Button();
btn.ID = "btn" + eventName;
btn.Text = "Time";
btn.BackColor = Color.Green;
btn.Click += new EventHandler(this.OnSubmit);
pnlDynamic.Controls.Add(btn);
Label lblTime = new Label();
lblTime.ID = "lbl" + eventName;
pnlDynamic.Controls.Add(lblTime);
lt = new Literal();
lt.Text = "<br /><br />";
pnlDynamic.Controls.Add(lt);
break;
default:
break;
}
}
}
}
}
}
}
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 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)
Using dt As DataTable = New DataTable()
sda.Fill(dt)
For Each dr As DataRow In dt.Rows
Dim type As String = dr("Property").ToString().ToUpper()
Dim eventName As String = dr("EventName").ToString()
Select Case type
Case "TEXTBOX"
Dim lbl As Label = New Label()
lbl.Text = eventName & ": "
pnlDynamic.Controls.Add(lbl)
Dim txt As TextBox = New TextBox()
txt.ID = "txt" & eventName
pnlDynamic.Controls.Add(txt)
Dim lt As Literal = New Literal()
lt.Text = "<br /><br />"
pnlDynamic.Controls.Add(lt)
Case "BUTTON"
Dim lblEventName As Label = New Label()
lblEventName.Text = eventName & ": "
pnlDynamic.Controls.Add(lblEventName)
Dim btn As Button = New Button()
btn.ID = "btn" & eventName
btn.Text = "Time"
btn.BackColor = Color.Green
AddHandler btn.Click, AddressOf OnSubmit
pnlDynamic.Controls.Add(btn)
Dim lblTime As Label = New Label()
lblTime.ID = "lbl" & eventName
pnlDynamic.Controls.Add(lblTime)
Dim lt = New Literal()
lt.Text = "<br /><br />"
pnlDynamic.Controls.Add(lt)
Case Else
End Select
Next
End Using
End Using
End Using
End Using
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