开发者

BulletedList onClick not firing

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" r开发者_开发问答unat="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");
}


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!!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜