2009-06-24 60 views
1

我已創建自定義字段類型,以將可用用戶列入Dropdownlist。我的Field類繼承自SPFieldChoice,FieldControl類繼承自DropDownChoiceField。 FieldControl類作爲內部現在我已經寫代碼,自定義字段類型下拉列表共享點

this.ddlInfoBox = new DropDownList();  
SPSite site = SPContext.GetContext(this.Context).Site; 
SPWeb cWeb = SPContext.Current.Web; 
Hashtable ht = new Hashtable(); 
SPUserCollection spUsers = cWeb.AllUsers; 
foreach (SPUser item in spUsers) 
{  
    SPFieldUser o= new SPFieldUser(
    ht.Add(item.Name, item); 
} 
this.ddlInfoBox.DataTextField = "Key"; 
this.ddlInfoBox.DataValueField = "Value"; 
this.ddlInfoBox.DataSource = ht; 
this.ddlInfoBox.DataBind(); 

即我想要呈現下拉列表與用戶名。現在Value屬性,

public override object Value 
{ 
    get 
    { 
     EnsureChildControls();     
     return this.ddlInfoBox.SelectedValue; 
    } 

    set 
    { 
     EnsureChildControls();     
     this.ddlInfoBox.SelectedValue = (string)this.ItemFieldValue; 
    } 
} 

讓說我有自定義字段類型來完成,當我在一個名爲「temp」的列表中使用這個領域,試圖創建一個列表項它爲我提供這個網站的使用者,當我創建一個項目時,數據將以選定用戶的字符串形式存儲,並且當我嘗試將該列表視爲Web部件時,將用戶值過濾爲[我]。即分配給我的文檔,它不允許我這樣做,因爲它是字符串值而不是SPFieldUser值。 如何將此字符串值存儲爲正常人和組開箱即用的字段類型存儲區的SPFieldUser?

+0

爲什麼要從Value屬性中調用EnsureChildControls?請小心,因爲這可能會影響ASP.NET頁面的生命週期。 – 2009-06-24 10:45:38

回答

1

難道你不能派生自SPFieldUser而不是SPFieldChoice?而對於DropDownChoiceField使用UserField?

0
SPUserCollection userscollection = rootWeb.SiteUsers; 
SPUser user = userscollection.Web.EnsureUser("Domain\username"); 
SPFieldUserValue userval = 
    new SPFieldUserValue(user.ParentWeb, user.ID, user.LoginName);