当把XmlFormView放到自己的aspx页面的时候,可能需要切换视图的逻辑,比如当流程绑定到表单库的时候,QuickFlow的FormServiceFormTask页面会按照任务活动的TaskFields中的__CurrentView字段来切换视图。

XmlFormView本身提供2个方法:

XmlViewer.XmlForm.ViewInfos.SwitchView("Approval")

XmlViewer.XmlForm.ViewInfos.Initial = XmlViewer.XmlForm.ViewInfos["Approval"]

但这两个方法或者出错,或者没用。

找到的最合理的方式是采用js切换,参考 http://www.moss2007.be/blogs/vandest/archive/2008/10/10/forms-server-xmlformview-and-setting-the-initialview-on-load.aspx

但这样会造成页面二次刷新。

没办法用Reflect分析代码,找到这个用反射设置视图的方法:

1)for SharePoint2007

void SwitchView12(string viewName)
        {
            if (String.IsNullOrEmpty(viewName))
                throw new ArgumentNullException("viewName");

            //check view exist.
            object viewInfo = null;

            try
            {
                viewInfo = this.XmlViewer.XmlForm.ViewInfos[viewName];
            }
            catch { }

            if (viewInfo == null)
            {
                throw new NotSupportedException("view doesn't exist:" + viewName);
                return;
            }

            //利用反射控制  this.XmlViewer.XmlForm.Document.CurrentViewName
            System.Reflection.PropertyInfo pDocument = this.XmlViewer.XmlForm.GetType().GetProperty("Document", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
            if (pDocument == null)
            {
                throw new NullReferenceException("can't get property XmlForm.Document");
                return;
            }
            object objDoc = pDocument.GetValue(this.XmlViewer.XmlForm, null);
            if (objDoc == null)
            {
                throw new NullReferenceException("can't get value: XmlForm.Document");
                return;
            }

            //objDoc.CurrentView = objDoc.Solution.Views[this._xmlFormHost.ViewInfos.Default.Name];

            System.Reflection.PropertyInfo pCurrentView = objDoc.GetType().GetProperty("CurrentView");
            //Debug("CurrentView: " + pCurrentView);

            System.Reflection.PropertyInfo pSolution = objDoc.GetType().GetProperty("Solution");
            //Debug("pSolution: " + pSolution);

            object objSolution = pSolution.GetValue(objDoc, null);
            //Debug("objSolution: " + objSolution); 

            IDictionary views = objSolution.GetType().GetProperty("Views", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).GetValue(objSolution, null) as IDictionary;
            //Debug("views: " + views); 

            object v = views[viewName];
            //Debug("v: " + v); 

            pCurrentView.SetValue(objDoc, v, null);
        }

  此方法在OnInitlized方法中调用。

2)for SharePoint2010

以上方法在2010上应该也是可以的,但2010提供了一个更方便的方法:XmlFormView.DefaultView,

bool SwitchView14(string viewName)
{
var pDefaultView = XmlViewer.GetType().GetProperty("DefaultView");//只能在DataBind之前设置

//DefaultView属性可能是公共方法,这样就不必用反射了,但笔者是在2007下实现的这段代码,所以必须用反射
if (pDefaultView == null)
return false;
pDefaultView.SetValue(XmlViewer, viewName, null);
return true;
}

这个方法不同的是:不能在OnInitlized方法中调用,必须在XmlFormView DataBind之前调用。

 

posted on 2011-12-08 15:11  jianyi  阅读(756)  评论(0编辑  收藏  举报