Tuesday, March 13, 2012

Set selected in a BulletedList

I would like set "Selected = True" where the listitem value equals the current page.

<asp:BulletedList ID="Navigate" runat="server" DisplayMode="HyperLink">
<asp:ListItem Value="localhost">Home</asp:ListItem>
<asp:ListItem Value="localhost/link1">Link1</asp:ListItem>
<asp:ListItem Value="localhost/link2">Link2</asp:ListItem>
<asp:ListItem Value="localhost/link3">Link3</asp:ListItem>
</asp:BulletedList>

I just don't see how yet. Could someone point me in the right direction?

Thnx,

Jacco

You need to get into your codebehind and do something like this.

Navigate.SelectedValue = Page.Title


First you need to get the name of the current page. In your page directive, set
Trace="True"

Look at the list of Server Variable items in the list - PathInfo, Path Translated, ScriptName, etc - grab the one you want, and parse it to match however you're writing out your list items.

Create a variable in the page:
Dim sPage as String
Then, when the page loads (in the Page_load routine), put something like:

if not Page.IsPostback then
Dim itmAs ListItem
ForEach itmIn RadioButtonList1.Items
If itm.Text = sPageThen
itm.Selected =True
EndIf
Next
End if

Almost there.

Now I've got:

Dim itm As ListItem
Dim sDom As String = Request.ServerVariables("HTTP_HOST")
Dim sPage As String = "http://" & sDom & Request.ServerVariables("SCRIPT_NAME")
For Each itm In navigate.Items
If itm.Value = sPage Then
itm.Selected = True
End If
Next

In the debugger I see it reaches the itm.Selected only once for my four links when the Value equals sPage. But when I view the source code of the page there's still no selected item. What's missing?

Thnx,

Jacco


BTW, This does work:

If itm.Value = sPage Then
itm.Text = "|" & itm.Text & "|"
End If

Why won't it write out selected when itm.Selected is used?

Thnx,

Jacco


not sure - that code I wrote was off the top of my head (ottomh), so I really should have put a disclaimer

:)

you might try something more along the lines of:
Navigate.Items.FindByText(itm.Value).Selected = true

again - a little more off the top


I appreciate all the help. So, don't worry :-)

I've tried it and it has the same result.

The goal is to get my bulleted CSS list to display correctly. In classic ASP I did something like:

<% if urlcheck = "/web" then %> id="current"<% end if %>

Maybe I'm trying in the wrong direction.

Thnx.

0 comments:

Post a Comment