Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

How can we include a set of buttons to a web page? These set of buttons must load in to the page dynamically.How can we create a dynamic button array for this purpose?

Please Guide me.

Thanks in Advance.
Posted
Updated 29-Jun-11 19:53pm
v3

The Best way to use Dynamic Buttons is DataList Make a DataList with item template containing a Button element. Bind the datalist with dataSource of buttons data collection. As
<asp:datalist id="dlButtons" runat="Server" onitemcommand="dlButtons_Command" xmlns:asp="#unknown">
      <itemtemplate>
              <asp:button id="cmdSubmit" text='<%#   Eval( "Text" ) %>' runat="server" commandname='<%# Eval("Command") %>' />
      </itemtemplate>



Now Handling on server Side on Page Load Event just bind the data source with Data List like As
 List<Button> buttons = new List<Button>();
 Button b1=new Button();
 b1.Text="Previous";
 b1.Command="Prev";
 buttons.Add(b1);

 Button b2=new Button();
 b2.Text="Next";
 b2.Command="Next";
 buttons.Add(b2);

dlButtons.DataSource=buttons;
dlButtons.DataBind();


Now You can Write Button Action Code here

 private void dlButtons_Command(object sender , DataListCommandEventArgs e)
{
       if(e.Command == "Prev")
       {
             //Write your code here
       }
      if(e.Command == "Next")
      {
             //Write Your Code here
      }
}



Ask if any issue comes ....
 
Share this answer
 
v3
Below code Might Help You



Listlist = new List();
for (int runs = 0; runs <= 10; runs++)
{
list.Add(new Button());
}
Button [] btnArray = list.ToArray();

Thanks
Faisal
 
Share this answer
 
Use "using System.Web.UI.WebControls;"


Button button1 = new Button();
button1.ID = btndynamic //create a buttonid
 
Share this answer
 
C#
if (!Page.IsPostBack)
      {
        for (int i = 1; i <= 100; i++ )
        {

          Button btn = new Button();
          btn.ID = "btn" + i;
          btn.Width = new Unit("100px");
          btn.Height = new Unit("20px");
          btn.Text = "Button " + i;
          this.Form.Controls.Add(btn);

        }
      }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900