如何在不使用spcontext.current的情况下在sharepoint中使用控件

时间:2010-02-10 07:02:19

标签: asp.net sharepoint controls

Sharepoint控件需要SPContext.current.site/web才能工作,但是我使用site = new spsite(siteID)打开了很多站点;我想使用控件。那么你有任何想法或可用类在sharepoint中使用asp.net控件吗?

2 个答案:

答案 0 :(得分: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,但是当我不在当前站点或当前网站时 发生此异常:控件中的InvalidArgumentException。 我想这些控件在当前站点或Web之外不起作用,我必须使用asp.net控件?是对还是有另一种解决方案?提前谢谢......

答案 1 :(得分:0)

假Spcontext是解决您问题的方法。 谷歌,它可能会帮助你。