Dynamically generate Button based on Database table record with Button Text using C# and VB.Net in ASP.Net

Last Reply on Oct 02, 2017 04:00 AM By dharmendr

Posted on Oct 02, 2017 03:47 AM

Hi   i need to display data on Button. I am using MSSQL 2014 I have tried below code. But I don't know how to bind the data with button

Pls advice me. Thank you. Maideen 

 

	  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 = 1 To 10
                Dim btn As New Button()
                btn.ID = "btn" & i
                btn.Text = i.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
            cmd = Nothing
            conn.Close()
        End Try

 

You are viewing reply posted by: dharmendr on Oct 02, 2017 04:00 AM.
Posted on Oct 02, 2017 04:00 AM Modified on on Oct 03, 2017 04:39 AM

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