Geeks With Blogs
Bunch's Blog One day I'll have a catchy subtitle, one day

Here is another way to access master page controls. This way has a bit less coding then my previous post on the subject. The scenario would be that you have a master page with a few navigation buttons at the top for users to navigate the app. After a button is clicked the corresponding aspx page would load in the ContentPlaceHolder. To make it easier for the users to see what page they are on I wanted the clicked navigation button to change color. This would be a quick visual for the user and is useful when inevitably they are interrupted with something else and cannot get back to what they were doing for a little while. Anyway the code is something like this.

Master page:

<body>
    <form id="form1" runat="server">
    <div id="header">
    <asp:Panel ID="Panel1" runat="server" CssClass="panelHeader" Width="100%">
       <center>
           <label style="font-size: large; color: White;">Test Application</label>
       </center>
      <asp:Button ID="btnPage1" runat="server" Text="Page1" PostBackUrl="~/Page1.aspx" CssClass="navButton"/>
      <asp:Button ID="btnPage2" runat="server" Text="Page2" PostBackUrl="~/Page2.aspx" CssClass="navButton"/>
      <br />
    </asp:Panel>
    <br />
    </div>
    <div>
        <asp:scriptmanager ID="Scriptmanager1" runat="server"></asp:scriptmanager>
        <asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">
        </asp:ContentPlaceHolder>
    </div>
    </form>
</body>

Page 1:

VB

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim clickedButton As Button = Master.FindControl("btnPage1")
    clickedButton.CssClass = "navButtonClicked"
End Sub

CSharp

protected void Page_Load(object sender, EventArgs e)
{
    Button clickedButton;
    clickedButton = (Button)Master.FindControl("btnPage1");
    clickedButton.CssClass = "navButtonClicked";
}

CSS:

.navButton
{
    background-color: White;
    border: 1px #4e667d solid;
    color: #2275a7;
    display: inline;
    line-height: 1.35em;
    text-decoration: none;
    white-space: nowrap;
    width: 100px;
    text-align: center;
    margin-bottom: 10px;
    margin-left: 5px;
    height: 30px;
}

.navButtonClicked
{
    background-color:#FFFF86;
    border: 1px #4e667d solid;
    color: #2275a7;
    display: inline;
    line-height: 1.35em;
    text-decoration: none;
    white-space: nowrap;
    width: 100px;
    text-align: center;
    margin-bottom: 10px;
    margin-left: 5px;
    height: 30px;
}

The idea is pretty simple, use FindControl for the master page in the page load of your aspx page. In the example I changed the CssClass for the aspx page's corresponding button to navButtonClicked which has a different background-color and makes the clicked button stand out.

Technorati Tags: ,,,

 

Posted on Wednesday, March 16, 2011 9:09 AM | Back to top


Comments on this post: Access Master Page Controls II

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Bunch | Powered by: GeeksWithBlogs.net