473,581 Members | 2,783 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1586
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********@com munity.nospamwr ote in message
news:ub******** *********@TK2MS FTNGP06.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.Contro ls.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_C lick;
btn.Text = row[0].ToString();
btn.ToolTip = row[1].ToString();
repFylleknapper .Controls.Add(b tn);
But I have one problem:
I have used context to transfere values between pages with
Server.Transfer e(), but when I press one of the buttons in the Repeater,
there is a postback performed. And when I come to the clickeventhandl er 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********@com munity.nospamwr ote in message
news:%2******** ********@TK2MSF TNGP06.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="Repeat erButtonClick" />
</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**********@l osmail.nowrote in message
news:e4******** ******@TK2MSFTN GP04.phx.gbl...
Hi tim,
I found that the Repeater.Contro ls.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_C lick;
btn.Text = row[0].ToString();
btn.ToolTip = row[1].ToString();
repFylleknapper .Controls.Add(b tn);
But I have one problem:
I have used context to transfere values between pages with
Server.Transfer e(), but when I press one of the buttons in the Repeater,
there is a postback performed. And when I come to the clickeventhandl er
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********@com munity.nospamwr ote in message
news:%2******** ********@TK2MSF TNGP06.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.Transfer e and the boolean value set to
true.

Thanks Totto
"Tim_Mac" <ti********@com munity.nospamwr ote in message
news:%2******** ********@TK2MSF TNGP06.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="Repeat erButtonClick" />
</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**********@l osmail.nowrote in message
news:e4******** ******@TK2MSFTN GP04.phx.gbl...
>Hi tim,
I found that the Repeater.Contro ls.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_C lick;
btn.Text = row[0].ToString();
btn.ToolTip = row[1].ToString();
repFylleknappe r.Controls.Add( btn);
But I have one problem:
I have used context to transfere values between pages with
Server.Transfe re(), but when I press one of the buttons in the Repeater,
there is a postback performed. And when I come to the clickeventhandl er
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********@com munity.nospamwr ote in message
news:%2******* *********@TK2MS FTNGP06.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
11813
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 field which it then appends to the form. I works in IE, but in NN7 nothing is passed. function processAssortment(){ // get the form Element var...
16
3172
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 forms... I tried painting, but the paintaing area provided by the form is only the client area - no visible way to paint on the title bar.
3
1683
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 statement on 3) then: db2 get snapshot for dynamic sql on <MyDatabase> I manage to get logs between instant t1 and t2, but i would like to see...
7
1324
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 click on the respective buttons absolutely nothing happens! I was wondering if anyone could help? The code I have is below... ADDING A RECORD ...
3
1791
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
3969
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: and this is unique for each radio button. So they are never in the same group. How can I put them in the same group?
2
1326
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
12541
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 buttons in IE. It does not even throw any errors. I have searched over the net but could not find anyhelp. Hope some experts here could help me. Here is...
1
1692
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 page "Next" and "Previous" which I want displayed just a couple of lines below the table dynamically. Thus the positions of these buttons would vary...
0
7804
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8156
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8180
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6563
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5681
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3809
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3832
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1409
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1144
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.