If Statements and XHTML

Hi Guys,

Now I am someone who is mega used to Classic ASP I suppose like alot of you were. Im trying to get the simple bit s mastered first and I have came across another issue.

CLASSIC ASP

<% if page = "about" then %>

<ul class='sub_nav'>
<li><a href='about/about_landmarks.htm'>> Landmarks</a></li>
<li><a href='about/about_attractions.htm'>> Visitor Attractions</a></li>
<li><a href='about/about_townsnvillages.htm'>> Towns &amp; Villages</a></li>
<li><a href='about/about_interest.htm'>> Local Interest</a></li>
<li><a href='about/about_links.htm'>> Useful Visitor Links</a></li></ul>

<% end if %>

The above basically hides/shows the section of code and that part of the menu dependant on if the user is on the about page.

I want to do exactly the same is asp.net

So far I have done this and it does not flag up any errors, but simply doesnt work. I know I am composing my IF statements correct as i used a simple lblTest.Text label test.

<script runat="server">

Sub Page_Load()

        Dim page
        page = "about"

End Sub


    Function sub_menu(ByVal Page) As String

        If Page = "about" Then
            lblAbout_menu.Text = "<ul class='sub_nav'><li><a href='about/about_landmarks.htm'>> Landmarks</a></li><li><a href='about/about_attractions.htm'>> Visitor Attractions</a></li><li><a href='about/about_townsnvillages.htm'>> Towns &amp; Villages</a></li><li><a href='about/about_interest.htm'>> Local Interest</a></li><li><a href='about/about_links.htm'>> Useful Visitor Links</a></li></ul>"
        ElseIf Page = "news" Then
            lblNews_menu.Text = "<ul class='sub_nav'><li><a href='about.htm'>> Submit a news story</a></li></ul>"
        End If

    End Function

</script>

Can anyone help or put me in the direction of a decent tutorial / starter site?

Thanks,

Tim

Could you simply do something along the lines of…?

If (Page = “about”) Then
subnav1.Visible = True
subnav2.Visible = False
End If

<ul id=“subnav1” class=‘sub_nav’>
<li><a href=‘about/about_landmarks.htm’>> Landmarks</a></li>
</ul>
<ul id=“subnav2” class=‘sub_nav’>
<li><a href=‘about/about_landmarks.htm’>> Landmarks</a></li>
</ul>

Blimey! its that simple. I didnt know ASP.net could pick up the ID’s on the different XHTML tags. - nice!

Name ‘about_menu’ is not declared.

If (page = "about") Then
            about_menu.Visible = True
        ElseIf (page = "news") Then
            news_menu.Visible = True
        End If
<ul id="about_menu" class="sub_nav">
<li><a href="about/about_landmarks.htm">> Landmarks</a></li>
<li><a href="about/about_attractions.htm">> Visitor Attractions</a></li>
<li><a href="about/about_townsnvillages.htm">> Towns &amp; Villages</a></li>
<li><a href="about/about_interest.htm">> Local Interest</a></li>
<li><a href="about/about_links.htm">> Useful Visitor Links</a></li>
</ul>

Have I done it correct? Or am I missing something???

I see.

I have to set the ul to runat=“server”

eg.

<ul id=“about_menu” class=“sub_menu” runat=“server”>

Helps other newbies like me out :smiley:

Still makes me ponder abit…

Im using Visual Web Developer 2005 Express as well, and it underlines in blue the objects about_menu and says it still isn’t declared.

But if I declare it as a varible if throws up another random colour Green and error on mouse over which says, it has been used before it has been assigned a variable!

Confused!

Ah, sorry. I missed off the runat=“server” in my post. I’m fairly new to .Net too - gradually moving from Classic ASP and trying more and more projects in .Net. There are enough similaries to make the cross-over smooth, but also enough new techniques to make it a real pain sometimes!

The way to get a tag out of a document when it isn’t a control object is like so:

HTML you want to get:


<div id="myDiv" runat="server"></div>

Backend:


HtmlGenericControl myDiv = (HtmlGenericControl)Page.FindControl("myDiv");

Now you can use myDiv as an object in your code.

Also, while what you’re planning to do will technically work, it’s not going to be accessible, and it will cause code bloat. You should figure out how to actually not output the portions of the menu you don’t want to see. What I would do is populate a collection of menu items, then use a repeater to output the menu you want.

You don’t need this if the tag is at the outer level of the markup, i.e. not inside any templates. ASP.NET will process the markup and any outer level tags with an Id and runat=“server” will have a member variable created for it with a corresponding type in the partial class generated by ASP.NET.