Hi maideen,
Refer the below sample.
HTML
<asp:Panel runat="server" ID="pnl_btns">
</asp:Panel>
C#
protected void Page_Load(object sender, EventArgs e)
{
string query = string.Format("SELECT roomno from AD_Rooms_Master order by roomno");
try
{
DataTable dt = GetData(query);
for (int i = 0; i < dt.Rows.Count; i++)
{
Button btn = new Button();
btn.ID = "btn" + (i + 1);
btn.Text = dt.Rows[i]["roomno"].ToString();
btn.Click += btnLoadButton_Click;
pnl_btns.Controls.Add(btn);
}
}
catch (Exception ex)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", "alert('Error occured : " + ex.Message.ToString() + "');", true);
}
finally
{
//cmd = null;
//conn.Close();
}
}
protected void btnLoadButton_Click(object sender, EventArgs e)
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "button", "<script type = 'text/javascript'>alert('Room no " + (sender as Button).Text + " selected');</script>");
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim query As String = String.Format("SELECT roomno from AD_Rooms_Master order by roomno")
Try
Dim dt As DataTable = GetData(query)
For i As Int32 = 0 To dt.Rows.Count - 1
Dim btn As New Button()
btn.ID = "btn" & (i + 1)
btn.Text = dt.Rows(i)("roomno").ToString()
AddHandler btn.Click, AddressOf btnLoadButton_Click
pnl_btns.Controls.Add(btn)
Next
Catch ex As Exception
ScriptManager.RegisterStartupScript(Me, Me.[GetType](), "Message", "alert('Error occured : " + ex.Message.ToString() + "');", True)
Finally
End Try
End Sub
Protected Sub btnLoadButton_Click(sender As Object, e As EventArgs)
ClientScript.RegisterClientScriptBlock(Me.[GetType](), "button", "<script type = 'text/javascript'>alert('Button " + TryCast(sender, Button).Text + " clicked');</script>")
End Sub
Screenshot