mvc中的ViewData用到webfrom中去

 public class UserControlPageHelper : UserControl
    {
        protected IDictionary<string, object> ViewData
        {
            get
            {
                return
                    (Dictionary<string, object>)
                    (Session["ViewData"] ?? (Session["ViewData"] = new Dictionary<string, object>()));
            }
        }
        protected override void OnUnload(EventArgs e)
        {
            base.OnUnload(e);
            if (Session["ViewData"] == null) return;
            Session.Remove("ViewData");
            GC.Collect();
        }

        protected dynamic Model
        {
            get { return ViewData["Model"]; }
            set { ViewData["Model"] = value; }
        }
  }
}

 

      分别继承MasterPage, Page 和 UserController 类
      注意一点 判断 ViewData.ContainsKey("x")

      用法:(前台代码)

 <% if (ViewData.ContainsKey("__tip"))
       {
           string tip = (string)ViewData["__tip"];
           if (!string.IsNullOrEmpty(tip))
           {
               Response.Write(tip);
           }
       }
       if (ViewData.ContainsKey("__refresh"))
       {
           string refresh = (string)ViewData["__refresh"];
           if (!string.IsNullOrEmpty(refresh))
           {
               Response.Write(refresh);
           } 
       }
       
    %>
posted @ 2012-03-21 23:50  曾祥展  阅读(1713)  评论(0编辑  收藏  举报