Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have a master page which has standard code in it to display messages, thus:
<% if (TempData["message"] != null) { %>
        <div class="statusMessage"><%= TempData["message"] %></div>
<% } %>

However, when I set TempData after a page submit, the master page does not seem to be refreshed when the page view is redisplayed after the postback. Therefore, any message that was set in the postback is not being displayed.

The same applies to a menu bar which is created in the master page. The last entry, Administration, is only displayed if the user is Authenticated and in Roles = "Admin". If an Admin User logs in, the master page is again not being refreshed to show the extra menu item.

How can the master page be refreshed?

Roger H
Posted

Try this:

Master Page code behind:

C#
public partial class Site1 : System.Web.UI.MasterPage
{
    public Dictionary<string, string> TempData;
}


Master Page:

XML
<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site1.master.cs" Inherits="WebApplication1.Site1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <% if (null != TempData && !string.IsNullOrEmpty(TempData["message"]))
           { %>
        <div class="statusMessage">
            <%= TempData["message"] %></div>
        <% } %>
        <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
        </asp:ContentPlaceHolder>
    </div>
    </form>
</body>
</html>


Page:

XML
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="WebApplication1.WebForm2"
    MasterPageFile="~/Site1.Master" %>
<asp:Content ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <asp:Button ID="btnsubmint" runat="server" Text="submit"
    onclick="btnsubmint_Click" />
</asp:Content>


Page code behind:

C#
public partial class WebForm2 : System.Web.UI.Page
{
    protected void btnsubmint_Click(object sender, EventArgs e)
    {
        Site1 master = (Site1)(this.Master);
        if (null == master.TempData) master.TempData = new Dictionary<string, string>();
        master.TempData.Add("message", "Hello");
    }
 }


Good luck
 
Share this answer
 
Comments
Sandeep Mewara 16-Nov-10 11:19am    
Comment from OP:
Ed

The whole point about ASP.NET MVC is that Submit buttons do not generate a button click event and there is no CodeBehind module to trap it. Neither is there a runat="Server" on the form tag. The content page essentially contains only the Html to be displayed which is opened by the Controller, which may or may not pass some data over. The Controller also handles the postback and then causes the page to be re-displayed with a data refresh.

I do not see any way within this paradigm to affect a redisplay of the master page.

Roger H
Ed Guzman 16-Nov-10 12:55pm    
I did not noticed the topic is MVP, sorry :-).
My answer was just for a regular ASP.NET
Ed
The whole point about ASP.NET MVC is that Submit buttons do not generate a button click event and there is no CodeBehind module to trap it. Neither is there a runat="Server" on the form tag. The content page essentially contains only the Html to be displayed which is opened by the Controller, which may or may not pass some data over. The Controller also handles the postback and then causes the page to be re-displayed with a data refresh.

I do not see any way within this paradigm to affect a redisplay of the master page.

Roger H
 
Share this answer
 
Comments
Sandeep Mewara 16-Nov-10 11:21am    
Posting a comment would notify the user via an email. It does not happen much that an answerer comes back by himself to see if you have any followup question. So, 'Add Comment' feature helps here. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900