471,075 Members | 948 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,075 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 1486
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by minigitoo | last post: by
2 posts views Thread by Bisser Milanov | last post: by
2 posts views Thread by Marc Nederhoff | last post: by
1 post views Thread by Paddy | last post: by
reply views Thread by leo001 | last post: by

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.