Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

programin style/application design 1

Status
Not open for further replies.

cjburkha

Programmer
Jul 30, 2004
76
0
0
US
Hi,

I'm more of an html programer who is trying to leverage asp.net to make my html programing easier and smarter, so I don't fully understand the .net paradigm yet. I have a design question, and I would like some opinions on it.

Let me try and set the situation. I have a nav on the left side of my website, and it is the same for all pages, so I have it in an include file(user control) leftNav.ascx. This control consists of various anchor tags leading to pages within the web site.

My goal is to have the anchor tag of the page you are on a different color from all the other tags. This is not so hard to do, but it is difficult for me to do it right.

I do it like this:
This control accepts a paramater pageName, and you pass the name of the page in a string, like so
Code:
<WebIncludes:leftNav pageName="improvingIAQ" runat="server"/>

Then each of the links has an inline function:

Code:
class="<% response.write(getSupplimentalClass("improvingIAQ")) %>

and the function:
Code:
Public function getSupplimentalClass(anchorId) as string
	Dim returnString as string = ""
	if anchorId = pageVar
		returnString = "activeLink"
	end if
	return returnString
end function

Not too complicated, but you can see the inherant problem, too much hard coding of strings. If I want to change the name of the page, I have then change the param I pass to my control, and then the param I pass to my inline function.

I thought of passing ordinals, like 1 or 2 to the control, but I would like something more descriptive. I have many pages, and remembering that "cleaningTips.aspx" is page 10 is a pain, and what if I change the order?

Any tips/comments/suggestions are much appriciated.

Can I throw one more requirement? I don't want my control based on a form. I don't want to use <asp:a > although that might make my life simpler, because then my page will not validate, which is a project requirement.

Thanks again

CJB
 
In theory, this is not too difficult.

You basically want to state the name of the page that the user is currently on so you should be able to access that through the Request.ServerVariables collection (namely "PATH_INFO"). It does make it slightly more tricky that you want to do this via a user control, but still it's not too much of a hardship to do.

I'm not sure what you mean about not using a using server controls though (when you mention at using <asp:a>) as:

1) There is no server control named <asp:a>
2) The correct server control would be a hyperlink control which would look like:
Code:
<asp:HyperLink Runat="server" ID="myHyperlink" NavigateUrl="Test.aspx">Hello</asp:HyperLink>
Which, once it had been rendered, would look like the following which does validate.
Code:
<a id="myHyperlink" href="Test.aspx">Hello</a>

If you don't know what I mean by my first paragraph, I can knock up a simple example, however, not right at this minute!


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Thank you very much. I had thought of the Request object, but for some reason I didn't think it would be available at the time my user control was renderd.

"In theory, this is not too difficult"
Yes, but in practice getting a variable value rather than hard coded was a bit more difficult than I though, but possible!

Yes, I meant <asp:HyperLink> I was under the mistaken impression that needed to be in a <form runat=server>

0-2 for assumptions today.

Also, I didn't realize that not specifying an id for my control would result in <a id=_ctrl_10_improvingIAQLink></a>, which would not validate. When I give my control a id, I get <A id=leftNavControl_improvingIAQLink></a> which is great!

Then I can override the onload

Code:
sub hyperLinkPreRender(sender As Object, e As System.EventArgs)
if sender.navigateUrl.substring(0, sender.NavigateUrl.length -5) = pageName
	sender.cssclass = "leftLink activeLink"
else
	sender.cssclass = "leftLink"
end if

end sub

Now the page can be any name I want. Of course, I have to change any href, but what can I do.

Thanks again for your help,

CJB
 
What I had in mind, was quite similar to what you have done here, but I've looped through the controls collection in the uer control code behind (if you did use this method, you'd have to set the code up as a function and call it recursively for any hyperlink controls that had a different parent to the page e.g. ones that may be in a panel for example).
Code:
     Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        Dim hyp As HyperLink
        'Loop the controls collection
        For Each c As Control In Me.Controls
            ' Check if the control is a hyerlink
            If TypeOf c Is HyperLink Then
                hyp = c
                ' Split the page name and take the last index of the array (i.e. the page name)
                If Request.ServerVariables("PATH_INFO").Split("/")(Request.ServerVariables("PATH_INFO").Split("/").GetUpperBound(0)).ToUpper.Trim _
                = hyp.NavigateUrl.ToUpper.Trim Then
                    ' Page we are on
                    hyp.CssClass = "currentPage"
                Else
                    ' Other links
                    hyp.CssClass = "otherPage"
                End If
            End If
        Next
    End Sub
So now, any page that includes this user control will have a different css class for the current page's link.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Thanks for your reply.

Your code showed me an error I consistently make, that string = is case sensitive, and I forget the ToUpper.

Also, for some reason I was obsessed with getting the Request.ServerVariables("PATH_INFO") on the aspx page and passing it to the control via a property. That was a pain, and silly if I can just get it in the control. Why make each page get the page name and pass to the control, when the control can do it. I'll move that.

Finally, we have two ways to access all the links, one by looping through all the links, and the other by accessing them as they are created(or just as soon as they are created)

Do you prefer one style over the other? Why?

Thanks for your help ca8msn, I've taken away 4 valubale tips from this thread.
 
Do you prefer one style over the other? Why?
In my scenario I looped through the controls after they were created but you used the PreRender event. Looking back, I think I would prefer to use your method and use the PreRender tag as it seems pointless not not render the class for the hyperlink and then go and add it in the page load. Both ways are perfectly acceptable and both do the job so I guess it's just a question of how you would prefer to do it.

Glad to help and hope it's made the situation a bit easier for you.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top