Link to home
Start Free TrialLog in
Avatar of Mike Eghtebas
Mike EghtebasFlag for United States of America

asked on

create buttons dynamically asp.net c#

List of items from a stored procedure sp1 with one parameter @Region (linked to Session["RegionID"]):

PK   Title      Region
1       A           1
2       B            1
.
.


returns list of PKs and Titles.

In a <td> tag, I hope to create number of buttons dynamically (one per title) with title as button caption (the returned list has 3 to 15 titles in it depending on the region value).

Question: Would you please give me something to get started with this?

Thank you.
ASKER CERTIFIED SOLUTION
Avatar of Jens Fiederer
Jens Fiederer
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mike Eghtebas

ASKER

Hi Jens,

Good direction resulting in good progress. As shown on the attached image, the buttons do not sit inside the panel.

Q1: How can I make the buttons to be inside the panel in two columns and as many rows necessary.
Q2: Could you please take a look at my code and improve its syntax. I am sure this could be done much cleaner.

It is desirable the buttons to be 90px wide x 40px high.

Shortly, I will add a question requesting some help in adding OnClick event(s) for the buttons.

We could have only one event for all of the buttons if we could somehow transmit PK field of the captions to the event. The PK is 2-digit first column on the image.

Thank you,

Mike
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class medCtrList : System.Web.UI.Page
{

    static Panel myPanel2 = new Panel();

    protected void Page_Load(object sender, EventArgs e)
    {
        Session["Region"] = "Region 1";
         Panel  myPanel = (Panel)this.FindControl("Panel1");
         myPanel2 = myPanel;
        ReadButtons();
    }

    static void ReadButtons()
    {
        using (SqlConnection connection = new SqlConnection("Data Source=USER-PC;Initial Catalog=ROD_July18;Integrated Security=True"))
        {
            // Create the command and set its properties.
            SqlCommand command = new SqlCommand();
            command.Connection = connection;
            command.CommandText = "SELECT t0... Sort DESC";
            command.CommandType = CommandType.Text;

            connection.Open();
            SqlDataReader reader = command.ExecuteReader();

           
            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    string btnCaption = "";
                    btnCaption = reader.GetString(1);
                    makeButtonFor(btnCaption);
                }
            }
            else
            {
                // so something else
            }
     
            reader.Close();
        }
    }
    static void makeButtonFor(string caption)
    {
        Button b = new Button { Text = caption };
        myPanel2.Controls.Add(b);
    }
}

Open in new window

panel1.png
SOLUTION
Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I would have to see the markup rather than just the code-behind to see why you are adding buttons to the wrong panel.

You don't HAVE to use a single panel, although that is the quickest way to program it....you can force columns using CSS as above, or you could construct a table and add the buttons to specific cells in that table if you prefer....or you could bind to a Repeater.

There are really lots of possibilities, and you can always check the "sender" in your event handler to see which button was pressed.

With the code page being
                <asp:Label runat="server" ID="result" />
                <Table>
                <asp:Repeater runat="server" ID="rpt">
                    <ItemTemplate>
                        <tr>
                            <td>
                                <asp:Button  runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Text") %>' OnClick="Unnamed_Click" />

                            </td>
                            <td>
                                <asp:Button runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Text2") %>' OnClick="Unnamed_Click" />
                            </td>
                        </tr>
                    </ItemTemplate>
                </asp:Repeater>

                </Table>

Open in new window


code behind like the below will set the label to the text of whichever button you pressed (assuming "all" is a list of titles):
            var list = new List<object>();
            foreach (var i in all)
            {
                list.Add(new { Text = i, Text2 = i + "foo" });
            }

            rpt.DataSource = list;
            rpt.DataBind();

Open in new window

Of course, this allows the REPEATER to construct the table ... there is nothing to stop you from creating an asp:Table in code behind and adding rows and cells and buttons to suit exactly what you want.

If you prefer to use CSS for the layout (which, if the reason for columns is visual rather than functional, is good style) and can assume HTML5 you could also use the new flexbox feature.
Thank you very much.