473,398 Members | 2,165 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,398 software developers and data experts.

Dynamicaly adding buttons to Asp 2

Hi,
Anyone know the best solution to dynamically add buttons to a asp 2.0 page
using data from Sql server?

Are there any contols suitable for this or is it best to iterate the dataset
and dynamicaly add buttons in the asp page?
Totto
Aug 21 '06 #1
6 1575
hi Totto,
you can do it with a Repeater if you want. can you give a description of
what you are doing?
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<asp:Button runat="server" Text="Submit" />
</ItemTemplate>
</asp:Repeater>

different list controls or code-generated may be best as you suggested, it
depends on your purpose. presumably they will need event handlers?

you need to make sure to re-create the buttons for each postback since they
are dynamic controls and events will not be raised unless they are
re-created each time.

tim
Aug 21 '06 #2
Hi Tim,
What I want is to generate buttons for each row returned from a database
query. Added to the page horizontaly.
[Button1] [Button2] [Button3].... [ButtonN]

And as you mentioned I also need event handler for the buttons, but I will
use the same handler for all of them.
So you think a repater is my best choice?

thanks Totto

"Tim_Mac" <ti********@community.nospamwrote in message
news:ub*****************@TK2MSFTNGP06.phx.gbl...
hi Totto,
you can do it with a Repeater if you want. can you give a description of
what you are doing?
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<asp:Button runat="server" Text="Submit" />
</ItemTemplate>
</asp:Repeater>

different list controls or code-generated may be best as you suggested, it
depends on your purpose. presumably they will need event handlers?

you need to make sure to re-create the buttons for each postback since
they are dynamic controls and events will not be raised unless they are
re-created each time.

tim

Aug 21 '06 #3
hi totto,
that's what i would use. the repeater is the most light-weight, so it is
the fastest to use when you don't need all the grid functionality, and it
generates no HTML markup except what you specify. it should work if you
declare the OnClick event handler in the repeater itemtemplate for the
button. make sure your event handler is marked Public.

if your events aren't firing, post your code here and i'll take a look at
it. dynamic controls can be difficult to get your head around at first,
because of the postback life-cycle.

tim
Aug 21 '06 #4
Hi tim,
I found that the Repeater.Controls.Add() function was a nice way to add
buttons to the repeater, that way I can set all the properties of the button
before is's added to the repeater.

Button btn = new Button();
btn.ID = i.ToString();
btn.Click += FyllingsKnapp_Click;
btn.Text = row[0].ToString();
btn.ToolTip = row[1].ToString();
repFylleknapper.Controls.Add(btn);
But I have one problem:
I have used context to transfere values between pages with
Server.Transfere(), but when I press one of the buttons in the Repeater,
there is a postback performed. And when I come to the clickeventhandler for
the buttons, all the previus values is gone exept for the button values
because its's reloaded during postback.
Any suggestions?
I have also posted the question in another thread.

Thanks Totto

"Tim_Mac" <ti********@community.nospamwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
hi totto,
that's what i would use. the repeater is the most light-weight, so it is
the fastest to use when you don't need all the grid functionality, and it
generates no HTML markup except what you specify. it should work if you
declare the OnClick event handler in the repeater itemtemplate for the
button. make sure your event handler is marked Public.

if your events aren't firing, post your code here and i'll take a look at
it. dynamic controls can be difficult to get your head around at first,
because of the postback life-cycle.

tim

Aug 22 '06 #5
hi Totto,
from your code it looks like you are adding the controls directly into the
Repeater controls collection, i don't think this is correct. you may have
intended to insert the control into the relevant item:

repFylleknapper.Items[i].Controls.Add(...)

it may be easier to have your buttons declared in the ItemTemplate/aspx of
the Repeater control. this is because the Repeater viewstate will preserve
the values this way. if you do it this way, you will only need to bind the
datasource for the first page_load (if you do manual databinding).

<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<asp:Button ID="Button1" runat="server" Text='<%# Eval("ID") %>'
Tooltip='<%# Eval("Country") %>' OnClick="RepeaterButtonClick" />
</ItemTemplate>
</asp:Repeater>

i presume the previous values you refer to are in textboxes also in the
repeater? if you are inserting these the same way as the buttons, i
wouldn't be surprised that they get lost :)

my original advice to recreate the controls manually for every postback is
only valid when the controls are created programatically and excluded from
the page viewstate management. it isn't necessary if you use my suggestion
above.

when using Server.Transfer you can choose to preserve the Form values with a
second boolean parameter. does this solve it for you?

if you could describe what your code in more detail it would be helpful,
i.e. what do you do in the click event handler, and then what do you do
after the server.transfer.

thansk
tim

"Totto" <to**********@losmail.nowrote in message
news:e4**************@TK2MSFTNGP04.phx.gbl...
Hi tim,
I found that the Repeater.Controls.Add() function was a nice way to add
buttons to the repeater, that way I can set all the properties of the
button before is's added to the repeater.

Button btn = new Button();
btn.ID = i.ToString();
btn.Click += FyllingsKnapp_Click;
btn.Text = row[0].ToString();
btn.ToolTip = row[1].ToString();
repFylleknapper.Controls.Add(btn);
But I have one problem:
I have used context to transfere values between pages with
Server.Transfere(), but when I press one of the buttons in the Repeater,
there is a postback performed. And when I come to the clickeventhandler
for the buttons, all the previus values is gone exept for the button
values because its's reloaded during postback.
Any suggestions?
I have also posted the question in another thread.

Thanks Totto

"Tim_Mac" <ti********@community.nospamwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
>hi totto,
that's what i would use. the repeater is the most light-weight, so it is
the fastest to use when you don't need all the grid functionality, and it
generates no HTML markup except what you specify. it should work if you
declare the OnClick event handler in the repeater itemtemplate for the
button. make sure your event handler is marked Public.

if your events aren't firing, post your code here and i'll take a look at
it. dynamic controls can be difficult to get your head around at first,
because of the postback life-cycle.

tim


Aug 22 '06 #6
Hi Tim,
Actually my solution worked, but I had to use session parameter to transfere
the value.
So your solution is much better.

What I want to do is to load the same page again, with a new id set by the
button.
I will give it a try with Server.Transfere and the boolean value set to
true.

Thanks Totto
"Tim_Mac" <ti********@community.nospamwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
hi Totto,
from your code it looks like you are adding the controls directly into the
Repeater controls collection, i don't think this is correct. you may have
intended to insert the control into the relevant item:

repFylleknapper.Items[i].Controls.Add(...)

it may be easier to have your buttons declared in the ItemTemplate/aspx of
the Repeater control. this is because the Repeater viewstate will
preserve the values this way. if you do it this way, you will only need
to bind the datasource for the first page_load (if you do manual
databinding).

<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<asp:Button ID="Button1" runat="server" Text='<%# Eval("ID") %>'
Tooltip='<%# Eval("Country") %>' OnClick="RepeaterButtonClick" />
</ItemTemplate>
</asp:Repeater>

i presume the previous values you refer to are in textboxes also in the
repeater? if you are inserting these the same way as the buttons, i
wouldn't be surprised that they get lost :)

my original advice to recreate the controls manually for every postback is
only valid when the controls are created programatically and excluded from
the page viewstate management. it isn't necessary if you use my
suggestion above.

when using Server.Transfer you can choose to preserve the Form values with
a second boolean parameter. does this solve it for you?

if you could describe what your code in more detail it would be helpful,
i.e. what do you do in the click event handler, and then what do you do
after the server.transfer.

thansk
tim

"Totto" <to**********@losmail.nowrote in message
news:e4**************@TK2MSFTNGP04.phx.gbl...
>Hi tim,
I found that the Repeater.Controls.Add() function was a nice way to add
buttons to the repeater, that way I can set all the properties of the
button before is's added to the repeater.

Button btn = new Button();
btn.ID = i.ToString();
btn.Click += FyllingsKnapp_Click;
btn.Text = row[0].ToString();
btn.ToolTip = row[1].ToString();
repFylleknapper.Controls.Add(btn);
But I have one problem:
I have used context to transfere values between pages with
Server.Transfere(), but when I press one of the buttons in the Repeater,
there is a postback performed. And when I come to the clickeventhandler
for the buttons, all the previus values is gone exept for the button
values because its's reloaded during postback.
Any suggestions?
I have also posted the question in another thread.

Thanks Totto

"Tim_Mac" <ti********@community.nospamwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
>>hi totto,
that's what i would use. the repeater is the most light-weight, so it
is the fastest to use when you don't need all the grid functionality,
and it generates no HTML markup except what you specify. it should work
if you declare the OnClick event handler in the repeater itemtemplate
for the button. make sure your event handler is marked Public.

if your events aren't firing, post your code here and i'll take a look
at it. dynamic controls can be difficult to get your head around at
first, because of the postback life-cycle.

tim



Aug 22 '06 #7

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

Similar topics

2
by: Paul | last post by:
I am trying to get a form to dynamicaly add hidden elements. Below is the function I've created, basicaly it loops thru an array and attempts to add those values to a newly created hidden input...
16
by: Picho | last post by:
Hi all, Is there any .NET way (I am not rulling out API usage) to add button(s) to a form's title bar? I found some non-.NET solutions that did actually work in VB6 but not in the ..NET...
3
by: minigitoo | last post by:
Hi I try to get dynamicaly all requests made to DB2 I process like this: 1) I stop then start my instance of database in the db2 control center 2) I use : db2 update monitor switches using...
7
by: Mathew Hill | last post by:
I am a beginner to the more technical aspects of Microsoft Access (2000) and was wondering if any one can help. I have 3 buttons on a form which add, delete and search for a record. However, when I...
3
by: leon | last post by:
hello friends, i am writing a page aspx and creating controls dinamicaly and then i must to create for each control the events as well. Anybody to know how????? happy day lion
2
by: Bisser Milanov | last post by:
I add radio buttons created dynamically in a datagrid on each row of the grid. When I see the generated HTML I see that in front of each name for a radio button is added: name="_ctl0:_ctl1:_ctl3:...
2
by: Marc Nederhoff | last post by:
Hi all, Is it possible to add the <link rel="stylesheet"....> dynamicaly. All pages in my site use the same style sheet, and I don't want to add the link manualy because of maintenance. Marc
5
by: sam | last post by:
Hi all, I am dynamically creating a table rows and inerting radio buttons which are also dynamically created. Everything works fine in Firefox as expected. But I am not able to select radio...
1
by: Paddy | last post by:
The problem I am facing is as follows: I am populating an HTML table on my webpage with rows of data from a database. The rows may be sometimes 10 and sometimes say,3. I have two buttons on that...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.