473,397 Members | 1,969 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Help adding buttons at runtime...

Hi,
I'm using ASP.NET. I need help adding new buttons to my web page at run
time on the server side prior to sending to the client.
As the web form is loading, I won't know in advance how many buttons I'll
need or the properties for each until I read a database.
For each new button, I need to assign the text, name ('id' for later
reference), and set the code-behind to handle the events for each.
I hope I'm clear. Could someone suggest some code that will help me please?
Thanks,
Doug
Nov 18 '05 #1
2 1789
you can write like this..keep the loop based on your database data.

for(int i=0; i<10; i++)
{
Button btn = new Button();
btn.ID = "Button" + i.ToString();
btn.Text = "Button" + i.ToString();
btn.CommandName = "Button" + i.ToString();
btn.Click += new EventHandler(btn_Click);
}

private void btn_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
if(btn.CommandName == "")
{
}
}

make sure that you always add these controls irrespective of IsPostBack..you
can put them in page_load.

Av.

"Doug Slocum" <ms**********@restrack.com> wrote in message
news:uT*************@tk2msftngp13.phx.gbl...
Hi,
I'm using ASP.NET. I need help adding new buttons to my web page at run
time on the server side prior to sending to the client.
As the web form is loading, I won't know in advance how many buttons I'll
need or the properties for each until I read a database.
For each new button, I need to assign the text, name ('id' for later
reference), and set the code-behind to handle the events for each.
I hope I'm clear. Could someone suggest some code that will help me
please?
Thanks,
Doug

Nov 18 '05 #2
Av,

In VB.NET I've tried :
----
Try

Dim i As Integer

For i = 0 To 9

Dim btn As New Button

btn.ID = "button" & i.ToString

btn.Text = "button" & i.ToString

btn.CommandName = "button" & i.ToString

Next

Catch ex As Exception

MsgBox(ex.Message)

End Try

-------------------

....This runs without errors but no buttons show in the browser. Also, there
is no btn.Click that I can find.

What could be wrong?

Doug

"avnrao" <av*@newsgroups.com> wrote in message
news:u4*************@tk2msftngp13.phx.gbl...
you can write like this..keep the loop based on your database data.

for(int i=0; i<10; i++)
{
Button btn = new Button();
btn.ID = "Button" + i.ToString();
btn.Text = "Button" + i.ToString();
btn.CommandName = "Button" + i.ToString();
btn.Click += new EventHandler(btn_Click);
}

private void btn_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
if(btn.CommandName == "")
{
}
}

make sure that you always add these controls irrespective of IsPostBack..you can put them in page_load.

Av.

"Doug Slocum" <ms**********@restrack.com> wrote in message
news:uT*************@tk2msftngp13.phx.gbl...
Hi,
I'm using ASP.NET. I need help adding new buttons to my web page at run
time on the server side prior to sending to the client.
As the web form is loading, I won't know in advance how many buttons I'll need or the properties for each until I read a database.
For each new button, I need to assign the text, name ('id' for later
reference), and set the code-behind to handle the events for each.
I hope I'm clear. Could someone suggest some code that will help me
please?
Thanks,
Doug


Nov 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: freddy | last post by:
I would like to add a to z buttoms at runtime
0
by: Lars Netzel | last post by:
Hey! I'm adding a ButtonColumn (Push button) in a datagrid in Runtime. How do I write the code to add the eventhandler for this buttons Click Event? Best regards /Lars
1
by: seanmayhew | last post by:
I have a form page that that while editing saves the data to an xml doc before submitting to db. On each page unload it saves the xmldoc as the user can add multiple items to the company like...
3
by: Byron Hopp | last post by:
Anybody have code to add a ToolBar, and its buttons to a MDI Child window. I have added the Toolbar, and added the buttons, but how do you determine what the button is going to execute upon the...
5
by: David S. Zuza | last post by:
Hey, I am new to .NET and I would like to index a button control on my form just like I used to be able to in VB6. In VB6 all I had to do was create one control and new to something like ...
4
by: jon f kaminsky | last post by:
Hi- I've seen this problem discussed a jillion times but I cannot seem to implement any advice that makes it work. I am porting a large project from VB6 to .NET. The issue is using the combo box...
3
by: Learner | last post by:
Hello, I have two buttons on one of my VehicleDetails.aspx page. Obiviously these two buttons takes the user to two different pages. Now my client is interested in having a linkbutton instead of...
6
by: Marc | last post by:
Hi, I am using the below code which simply allows a single button control to be dragged and dropped anywhere around a form. My problem is that want to move about 100 buttons on the form. Is...
4
by: Ron | last post by:
I want to create 10 buttons on a form at runtime, the code below is only creating one button labeled 1. Any idea what I am doing wrong? Public Class Form1 Public code(10) As String Public...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.