views:

47

answers:

1

Ugh, this is driving me mad

Im trying to build up a dynamic menu from a bulletedList, most menu items are plain links however the log out button needs to perform some cleanup code.

I cant for the life of me get the BullettedLists onclick event to fire. The BulletedList is inside a user control (if that makes a difference)

Any ideas? Or - any ideas for an alternative, better solution?

Code below

BulletedList

<asp:BulletedList OnClick="menu_Click" runat="server" CssClass="MainMenu" ID="loggedInMenu" DisplayMode="HyperLink"  />  

Adding an element

loggedInMenu.Items.Add(new ListItem("Logout", ""));

Click handler

protected void menu_Click(object sender, BulletedListEventArgs e)
{

    user.logout();
    Response.Redirect("Default.aspx");
}
+1  A: 

You're using the wrong DisplayMode for your BulletedList control. You should use a DisplayMode of LinkButton. When you use DisplayMode.HyperLink:

Users can click links to move to another page. You must provide a target URL as the Value property of individual items.

This is from the MSDN docs for this control. (It's about 3/4 of the way down the page.)

When you use a BulletedList control in HyperLink mode, the value of your ListItem is the URL that you're navigating to. So your static page HTML controls would use ListItem.Value as the href attribute of the <a> tag.

Here's what the HTML markup looks like when you use a DisplayMode of HyperLink (it's a plain old HTML anchor tag w/ a href):

<li><a href="1">One</a></li>

But since you want to postback, you should set the DisplayMode of your BulletedList control to LinkButton. When you do that, you'll enable a postback back to your page and your event handler will trap the event. You can then process the click appropriately then. The event argument that's passed in (of type BulletedListEventArgs) will have an Index property, and that will tell you what item in your list was clicked.

Here's the updated .aspx code that I used:

<asp:BulletedList ID="bullet" runat="server" DisplayMode="LinkButton"
    onclick="bullet_Click">
    <asp:ListItem Text="One" Value="1">One</asp:ListItem>
</asp:BulletedList>

Everything else is the same except the DisplayMode, which is set to LinkButton. When I use that, then my bullet_Click event handler is fired when I click a list item.

I hope this helps!!

David Hoerster
yup, that did it. Thanks. I'm gonna keep trying to find another more flexible solution, Im not sure I want all these post backs happening on the OTHER links
Keeno
What are you trying to do on the postback fir the other links? You may want to consider some AJAX functionality and potentially use a framework like jQuery. It's very easy to build up a dynamic list and handle events with it. Let me know and I can add an example.
David Hoerster
hey, Ajax is exactly what I did in the end, still using the bulletedList (with hyperlinks), then I added an onclick attribute to one of the items. The onclick then fired off an ajax call.Thanks again :)
Keeno
Glad it worked out. Lately I've found that I can avoid a lot of postbacks by using a little client-side functionality. jQuery (along with a few other frameworks) has opened my eyes to a different world of web programming. Good luck!
David Hoerster