views:

68

answers:

4

In my web app (asp.net C#) I have menus as "All", "Education", "Fun", "Comedy". I want when I select All then it should be displayed as current menu, for which I have a CSS class current.

In case of WebUserControls I can do it easily by passing parameter of current page to select as below:

mywebpage.aspx

<uc:header ID="header1" runat="server" selectedMenu="comedy" />

header.ascx (code (c#))

public string selectedMenu
{
    get { return strSelected; }
    set { strSelected = value; }
}

header.ascx (html)

<ul>
   <li><a href="/all/" title="All Videos" <%if (strSelected == "all"){%>class="current"<%} %>><span>All</span></a></li>
   <li><a href="/fun/" title="Fun Videos" <%if (strSelected == "fun"){%>class="current"<%} %>><span>Fun</span></a></li>
   <li><a href="/comedy/" title="Comedy Videos" <%if (strSelected == "comedy"){%>class="current"<%} %>><span>Comedy</span></a></li>
</ul>

When I'll pass comedy from my webpage to usercontrol then it will select comedy menu. I want to implement the same kind of functionality in case of master pages, could anyone tell me how to achieve this type of facility for master pages.

One way what I feel is to pass a query string parameter http://example.com/all/?select=all, I'll check on master page_load function if select parameter is "all" or fun or comedy then select corresponding menu. But this is not good, because I don't want to add an extra query string parameter to my URLs.

Please tell me how to solve this issue.

Thanks

A: 

Hai prashant,

I had the same issue a month back and i posted in stack overflow check this one it may help you http://stackoverflow.com/questions/1762506/find-a-unorderedlist-ul-control-inside-a-master-page-from-a-content-page-in-asp

Pandiya Chendur
It own address your issue but its very similar to urs...
Pandiya Chendur
In your approach, I have to make all my html controls to server controls which I don't think a good approach.
Prashant
+1  A: 

One way i have done simular in the past is add this to the page derivatives of any content pages:

<%@ MasterType VirtualPath="~/YourMaster.master" %>

Then in the master i exposed this:

 private PageTypes currentPageType;

public PageTypes CurrentPageType
{
    get { return currentPageType; }
    set { currentPageType = value; }
}

On this job this was used so the master knew what type of page it was on and therefore changed a few things, colours, controls etc. So from a contents pageload i did

Master.CurrentPageType = PageTypes.System;

One thing to note however, VS tends to moan about the MasterType derivative until you do a rebuild all.

Jammin
+1  A: 

You can access master page properties from your content page after casting the master to the correct type:

public class MyMasterPage : MasterPage
{
    public string MyMenuProperty { get; set; }

    protected void Page_PreRender(object sender, EventArgs e)
    {
        if (MyMenuProperty == "comedy")
        {
            /* do your menu stuff */
        }
    }
}

public class MyContentPage : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        var myMaster = Page.Master as MyMasterPage;
        if (myMaster != null)
        {
            myMaster.MyMenuProperty = "comedy";
        }
    }
rmacfie
Looks good and same as usercontrol, but is there any thing with which instead of writing code of property on my web page, can I directly pass "comedy" from html markup?? like in usrecontrols??
Prashant
I think Jammin's answer has that part covered. It seems that if you put <%@ MasterType VirtualPath="~/YourMaster.master" %> in your content page, the casting will be done automagically.
rmacfie
A: 

I'm thinking that maybe a completely different approach might be easier to implement and maintain.

How about you just parse the URL in your header.ascx?

For example:

<li><a href="/all/" title="All Videos" runat="server" class='<%= Request.Url.ToString().Contains("/all/") ? "current" : "" %>' ><span>All</span></a></li>

That way, you won't have to worry about setting properties, accessing/casting master pages etc...

Stefan