您可以使用視圖的RenderAsHtml()方法。
此方法返回一個HTML字符串,您可以在Web部件中顯示該字符串。但要小心,有關於上下文ID的錯誤。
我建議使用下列函數用於手動設置的ID:
public static String RenderAsHtmlWithFix(SPView view, uint id)
{
String html = String.Empty;
if (view != null)
{
html = view.RenderAsHtml();
String ctxIDString;
int ctxID;
GetCtxID(html, out ctxIDString, out ctxID);
if (Int32.TryParse(ctxIDString, out ctxID))
{
html = html.Replace("ctx" + ctxID, "ctx" + id);
html = html.Replace("ctxId = " + ctxID, "ctxId= " + id);
html = html.Replace("CtxNum=\"" + ctxID + "\"", "CtxNum=\"" + id + "\"");
html = html.Replace("FilterIframe" + ctxID, "FilterIframe" + id);
html = html.Replace("titl" + ctxID + "-", "titl" + id + "-");
html = html.Replace("tbod" + ctxID + "-", "tbod" + id + "-");
html = html.Replace("foot" + ctxID + "-", "foot" + id + "-");
html = html.Replace("up('" + ctxID + "-", "up('" + id + "-");
html = html.Replace("img_" + ctxID + "-", "img_" + id + "-");
}
}
return html;
}
private static void GetCtxID(String html, out String ctxIDString, out int ctxID)
{
int idIndex = html.IndexOf("ctxId =");
ctxIDString = String.Empty;
for (int i = idIndex + 7; html[i] != ';'; i++)
{
ctxIDString += html[i];
}
ctxIDString = ctxIDString.Trim();
ctxID = 1;
}