Adding LinkButton Controls to a BulletedList

J

Jonathan Wood

I want to dynamically create an unordered list where each item is a link
that posts back to the source page.

I thought it might be nice to use a BulletedList control for the list, and
create LinkButton controls for each item in the list. But the BulletedList
control doesn't allow child controls, and the ListItem object doesn't
support child controls either.

I was curious how you folks might approach this situation.

Thanks.

Jonathan
 
G

Guest

I want to dynamically create an unordered list where each item is a link
that posts back to the source page.

I thought it might be nice to use a BulletedList control for the list, and
create LinkButton controls for each item in the list. But the BulletedList
control doesn't allow child controls, and the ListItem object doesn't
support child controls either.

I was curious how you folks might approach this situation.

Thanks.

Jonathan

I think the easiest way would be to use Repeater with LinkButtons

Something like this

<UL>

<asp:Repeater ID="Repeater1" runat="server"
OnItemCommand="Repeater1_ItemCommand">
<ItemTemplate>
<LI>
<asp:LinkButton ID="LinkButton1" runat="server"
Text="Trigger" CommandName="trigger"></asp:LinkButton>
</ItemTemplate>
</asp:Repeater>

</UL>
 
G

Gregory A. Beamer

I want to dynamically create an unordered list where each item is a
link that posts back to the source page.

I thought it might be nice to use a BulletedList control for the list,
and create LinkButton controls for each item in the list. But the
BulletedList control doesn't allow child controls, and the ListItem
object doesn't support child controls either.

I was curious how you folks might approach this situation.

If you want a built in .NET control, I would use the Repeater, which
allows you to start the unordered list in the header and finish in the
footer and do the "dirty work" in the middle.

If you want something you can drag and drop with no need to create tags,
a custom built control is the best option. You may be able to find an
open source one already built, or a third party control you can
purchase, that does what you want.

Peace and Grace,
 
J

Jonathan Wood

Anon User said:
I think the easiest way would be to use Repeater with LinkButtons

Something like this

<UL>

<asp:Repeater ID="Repeater1" runat="server"
OnItemCommand="Repeater1_ItemCommand">
<ItemTemplate>
<LI>
<asp:LinkButton ID="LinkButton1" runat="server"
Text="Trigger" CommandName="trigger"></asp:LinkButton>
</ItemTemplate>
</asp:Repeater>

</UL>

Thanks, I haven't worked with this control before.

I'm not exactly clear on how I can then build this list dynamically but I
should be able to figure it out. I'll check it out.

Jonathan
 
J

Jonathan Wood

Gregory A. Beamer said:
If you want a built in .NET control, I would use the Repeater, which
allows you to start the unordered list in the header and finish in the
footer and do the "dirty work" in the middle.

Thanks, that's two suggestions for the same thing.

Jonathan
 
G

Gregory A. Beamer

Thanks, that's two suggestions for the same thing.

Jonathan

It is the easiest declarative solution. If you need to customize the
output more, then the custom control is the best option.

Peace and Grace,
 
J

Jonathan Wood

Gregory A. Beamer said:
It is the easiest declarative solution. If you need to customize the
output more, then the custom control is the best option.

I got it working by binding to the datasource. However, it kind of annoyed
me that there is no method to add a row manually. I mean, the control does
that itself so why not expose that functionality? Perhaps I'll be okay with
the way it works now.

Thanks.

Jonathan
 
G

Guest

I got it working by binding to the datasource. However, it kind of annoyed
me that there is no method to add a row manually. I mean, the control does
that itself so why not expose that functionality? Perhaps I'll be okay with
the way it works now.

Thanks.

Jonathan

Well, it's true, it does not provide any built-in means for editing,
sorting, or paging of data, but it's simple. You could add new
features programmatically, but would result in a lot of code and
effort.

Actually, add a new row is not that difficult

if you bind your repeater to an array

string[] arr1 = new string[] {
"array item 1",
"array item 2",
"array item 3",
"array item 4",
"array item 5"
};

Repeater1.DataSource = arr1;
Repeater1.DataBind();

than to add a new row you would need just to add a new item to the
array and re-bind the control.

I think this should not be a big issue.

There is also another option to set <ul> </ul>. You can add the <ul>
tag to the HeaderTemplate, the </ul> tag to the FooterTemplate

<asp:Repeater...>
<HeaderTemplate>
<ul>
</HeaderTemplate>
<ItemTemplate>
<li>...</li>
</ItemTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
</asp:Repeater>

Hope this helps
 
J

Jonathan Wood

Anon User said:
Actually, add a new row is not that difficult

if you bind your repeater to an array

string[] arr1 = new string[] {
"array item 1",
"array item 2",
"array item 3",
"array item 4",
"array item 5"
};

Repeater1.DataSource = arr1;
Repeater1.DataBind();

But this adds unnecessary overhead because I must make an additional copy of
the data I'm working with. Internally, the repeater control has logic to add
a row to the output. Why not just add a method to do this manually?

Jonathan
 
G

Guest

Anon User said:
Actually, add a new row is not that difficult
if you bind your repeater to an array
string[] arr1 = new string[] {
"array item 1",
"array item 2",
"array item 3",
"array item 4",
"array item 5"
};
Repeater1.DataSource = arr1;
Repeater1.DataBind();

But this adds unnecessary overhead because I must make an additional copy of
the data I'm working with. Internally, the repeater control has logic to add
a row to the output. Why not just add a method to do this manually?

Jonathan

Repeater is a data-bound container control that needs to have a data
source. I agree, array is probably not the best choice, you may
consider to use an ArrayList, where you can use Insert method as

ArrayList arr1 = new ArrayList();
arr1.Insert(0, "array item 1");
....

Repeater1.DataSource = arr1;
Repeater1.DataBind();

and then

ArrayList arr1 = (ArrayList)Repeater1.DataSource;
arr1.Insert(arr1.Count, "array item 3");

but you need to keep you data somewhere anyhow.

If you don't like to use Repeater, either try to extend the original
BulletedList (you would need to overwrite Render and RenderContents
events), or simply use LiteralControl where you can output

sb.Append("<UL>");
foreach (string s in .....your data source)
{
string x = sb.Append("<LI>")
sb.Append(....)
sb.Append("</LI>");
}
sb.Append("</UL>");

.....
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top