Search code examples
asp.netupdatepanel

how to change a css class of div in master page from update panel in content page


I want to change div style (display) in master page from my child page , I used:

 protected void ShowMsgText(int MsgID)
{
    HtmlGenericControl MsgInner;
    MsgInner =((HtmlGenericControl)Master.FindControl("MsgInner"));
    MsgInner.Attributes["class"] = "MsgInnerShow";
}

The Problem : I want to change the class from update panel , the div in master page Not in the update panel , any way to achieve this without need to move the update panel to master page .


Solution

  • Without the UpdatePanel you would use FindControl to change the class.

    Panel panel = Master.FindControl("Panel1") as Panel;
    panel.CssClass = "myClass";
    

    But since you use an UpdatePanel, the easiest way is to use jQuery.

    On the master page the Panel and the script

    <asp:Panel ID="Panel1" runat="server">Welcome to StackOverflow</asp:Panel>
    
    <script type="text/javascript">
        function changeClass(className) {
            $("#<%= Panel1.ClientID %>").attr("class", className);
        }
    </script>
    

    Then in the code behind of the aspx page, you can call that javascript function on PostBack.

    ScriptManager.RegisterStartupScript(Page, Page.GetType(), "changeClassName", "changeClass('myClass');", true);