2010-02-10 61 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

相关问题