2010-02-10 62 views
1

Sharepoint控件需要SPContext.current.site/web才能工作,但我使用site = new spsite(siteID)打開了許多站點;我想使用控件。所以你有任何想法或可用的類在SharePoint中使用asp.net控件?如何使用Sharepoint中的控件而不使用spcontext.current

+0

您能否提供更多信息,比如您如何創建控件對象以及在嘗試使用它們時收到哪些異常? 謝謝。 – 2010-02-10 08:26:11

+0

好吧,我會寫在答案區域,因爲我的回覆太大,無法在這裏適應 – Manale 2010-02-12 07:35:07

回答

0
//open site and web 
sSiteID = Request.QueryString["siteID"]; 
sWebID = Request.QueryString["parentWebID"]; 
site = new SPSite(new Guid(sSiteID)); 
web = site.OpenWeb(new Guid(sWebID)); 
//show the properties of the list in the edit form 
(...) 
if ((list.AllowContentTypes == true) && (list.ContentTypesEnabled == true)) 
{ 
    (...) 
    SharePointWebControls oSharePointWebControls = new SharePointWebControls(); 

    cntrl = oSharePointWebControls.GetSharePointControls(field, list, item, SPControlMode.Edit, ""); 
} 

public Control GetSharePointControls(SPField field, SPList list, SPListItem item, SPControlMode mode, string strType) 
{ 
    switch (field.FieldRenderingControl.ToString()) 
    { 
     case "Microsoft.SharePoint.WebControls.TextField": 
      return CreateTextFieldControl(field, list, item, mode); 
    } 
} 

#region Create SharePoint Controls 
private static Control CreateTextFieldControl(SPField field, SPList list, SPListItem item, SPControlMode mode) 
{ 
    TextField tf = new TextField(); 
    //tf.EnableViewState=false; 
    tf.ListId = list.ID; 
    if (item != null) 
    { 
     tf.ItemId = item.ID; 
    } 
    tf.FieldName = field.Title; 
    tf.ID = "Field_" + field.Id; 
    //tf.CssClass = "spsControl"; 
    tf.ControlMode = mode; 
    //check if the field has a default value 
    if (field.DefaultValue != "null" && field.DefaultValue != null) 
    { 
     tf.Text = field.DefaultValue.ToString(); 
    } 
    try 
    { 
     RequiredFieldValidator cntrlValidator = ((RequiredFieldValidator)tf.Controls[0].Controls[3]); 
    } 
    catch (Exception ex) 
    { 
    } 

    return tf; 
} 

我使用所有SharePoint控件工作和正確返回TF,但是當我在當前網站或當前Web 我不是這個異常發生:InvalidArgumentException的控制。 我猜這些控件不能在當前網站或網頁之外工作,而且我必須使用asp.net控件?是對的還是有另一種解決方案?在此先感謝...

+0

你能格式化代碼並將其移至問題本身嗎?您基本上需要僞造spcontext並將其他sitecollection加載到它。 – 2010-04-21 12:55:31

0

假Spcontext是解決您的問題。谷歌它,它可能會幫助你。

+0

它確實有幫助。謝謝 – Manale 2010-02-15 06:52:16

相關問題