这个Framework说实话我用了下不错。而且它是我最近发现的使用ViewState最牛的一个网站模板。
从下面的Copyright可以看出来这个不是我做的,呵呵。左侧的Menu是一个Amenu控件,关于这个控件的类结构图如下:
我会后面把代码传上去有兴趣的可以研究研究。
整个架构的风格很不错,给人的感觉是比较清新明了。Menu中的每个选项都是一个ascx控件,Viewstate的使用使得网站的性能更加好。
页面初始化时的代码:
// Load or reload a UserControl:
string file = this.TrackedControl;
if (!string.IsNullOrEmpty(file)) {
if (IsPostBack) ReloadContent();
}
else {
file = "~/Controls/Home.ascx";
if (!IsPostBack) LoadContent(file, null);
else throw new Exception("Page_Load(): invalid state"); // dbg
}
TrackedControl是个Viewstate:
protected string TrackedControl
{
get {
return ViewState["TrackedControl"] as string;
}
set {
if (value == null) ViewState.Remove("TrackedControl");
else ViewState["TrackedControl"] = value;
}
}
而在前台页面上是通过一个placeholder存放ascx文件:
<asp:UpdatePanel ID&#61;"UpdatePanelAdminData" runat&#61;"server"
UpdateMode&#61;"Conditional" ChildrenAsTriggers&#61;"False">
<Triggers>
<asp:AsyncPostBackTrigger ControlID&#61;"ImageLogo" EventName&#61;"Click" />
<asp:AsyncPostBackTrigger ControlID&#61;"MTMenuAdmin" EventName&#61;"ItemClick" />
<asp:AsyncPostBackTrigger ControlID&#61;"ScriptManager1" EventName&#61;"Navigate" />
Triggers>
<ContentTemplate>
<asp:PlaceHolder ID&#61;"PlaceHolderAdminData" runat&#61;"server" />
ContentTemplate>
asp:UpdatePanel>
在点击某个菜单时会清空placeHolder:
PlaceHolderAdminData.Controls.Clear();
鼠标点击事件&#xff1a;
protected void OnMenuClick(object sender, CommandEventArgs e)
{
string from &#61; ((Control)sender).ClientID;
string cmd &#61; e.CommandName;
string arg &#61; e.CommandArgument as string;
// Parameters for LoadContent()
ParameterCollection pc &#61; new ParameterCollection();
pc.Add(new Parameter("CommandArgument", TypeCode.String, arg));
if (!string.IsNullOrEmpty(cmd)) {
LoadContent(cmd, pc);
}
}
根据当前选中的菜单的CommandName来决定加载哪个ascx控件,具体加载时通过LoadContent方法来实现:
public DControl LoadContent(string file, ParameterCollection pc)
{
// Remove all controls
PlaceHolderAdminData.Controls.Clear();
if (string.IsNullOrEmpty(file)) return null;
// The return value
DControl dc &#61; null;
// Load the control
try {
dc &#61; DControl.LoadUC<DControl>(file, this);
if (dc &#61;&#61; null) throw new Exception();
}
catch (Exception ex) {
string msg &#61; "Couldn&#39;t load " &#43; file &#43; ". " &#43; ex.Message;
throw new Exception(msg);
}
// Save as tracked control
TrackedControl &#61; file;
// Pass on parameters
if (pc !&#61; null) dc.Params &#61; pc;
// Insert the new control
PlaceHolderAdminData.Controls.Add(dc);
// Add history points
ScriptManager sm &#61; ScriptManager.GetCurrent(Page);
if (sm.IsInAsyncPostBack && !sm.IsNavigating)
{
sm.AddHistoryPoint("UCPage", file);
sm.AddHistoryPoint("Params", Util.SerializePC(pc));
}
return dc;
}