Dynamicaly adding buttons to Asp 2

T

Totto

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
 
T

Tim_Mac

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
 
T

Totto

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
 
T

Tim_Mac

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
 
T

Totto

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
 
T

Tim_Mac

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.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 said:
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 said:
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
 
T

Totto

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 said:
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.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 said:
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 said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top