简体   繁体   中英

Issue after changing control property in Master Page from code behind

Background

I have some LinkButtons in a nested Master Page. They are submenus so when user clicks a button, it redirects users to a specific page. Each button shows an icon and the name of the specific page. (eg Project Description)

Markup in NestedMasterPage.master looks like this:

<asp:LinkButton ID="btnTest" CssClass="testButton" runat="server">
    <div class="submenu-left"><img src="~/Images/test.png" runat="server" /></div>
    <div class="submenu-right">Test</div>
</asp:LinkButton>

What I want to do

When a linkbutton is clicked, I want to change the backcolor of the clicked linkbutton so that users know in which page they currently are.

What I did

In NestedMasterPage.master.cs, for each linkbutton, I have

public LinkButton BtnTest
{
    get{ return btnTest; }
}

In each .aspx page, I added

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

and when the redirected page is loaded, I added this code to change the backcolor.

protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);
    Master.BtnTest.BackColor = System.Drawing.Color.Black;
}

This does change the backcolor... but I'm having an issue.

Issue

The problem I'm having is that when a postback occurs in the redirected page, the button loses the icon and the page name, and it keeps just the color.

eg

Click button2 redirects to page2, and the button2 has black backcolor:
+---------+  +---------+  +---------+
| button1 |  | button2 |  | button3 |
+---------+  +---------+  +---------+
   white        black        white

After postback: The button loses the text (and icon)
+---------+  +---------+  +---------+
| button1 |  |         |  | button3 |
+---------+  +---------+  +---------+
   white        black        white

I have tried putting Master.BtnTest.BackColor = System.Drawing.Color.Black; in if(!Page.IsPostBack) but obviously that didn't do anything.

I don't understand why modifying the color would do this. Re-rendering the button text every time postback occurs doesn't sound the right solution, and I'm wondering what exactly is happening here. I looked many articles about accessing controls in Master file but didn't find anything that addressed this kind of issues, and I don't even know what I should look for to find similar problems. Any help is appreciated.

I don't know why but it seems that it's the <div> that's causing the text and icon to disappear.

<asp:LinkButton ID="btnTest" CssClass="testButton" runat="server" >
    <img src="~/Images/testIcon.png" class="submenu-left" runat="server" />
    <p class="submenu-right">Test</p>
</asp:LinkButton> 

This does the trick but I'm not sure if this is the right solution, and I still don't know why would cause such an issue..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM