2014-03-31 23 views
1

我試圖按照演練:創建基本控件設計

Walkthrough: Creating a Basic Control Designer for a Web Server Control

基本上我已經創建了一個新網站,asp.net剃刀4

,並添加以下代碼:

using System; 
using System.ComponentModel; 
using System.ComponentModel.Design; 
using System.Drawing; 
using System.Web.UI; 
using System.Web.UI.Design; 
using System.Web.UI.Design.WebControls; 
using System.Web.UI.WebControls; 

namespace Samples.AspNet.CS.Controls 
{ 

    public class SimpleCompositeControl : CompositeControl 
    { 


    private String _prompt = "Please enter your date of birth: "; 
public virtual String Prompt 
{ 
    get 
    { 
     object o = ViewState["Prompt"]; 
     return (o == null) ? _prompt : (string)o; 
    } 
    set 
    { 
     ViewState["Prompt"] = value; 
    } 
} 

public virtual DateTime DOB 
{ 
    get 
    { 
     object o = ViewState["DOB"]; 
     return (o == null) ? DateTime.Now : (DateTime)o; 
    } 
    set 
    { 
     ViewState["DOB"] = value; 
    } 
} 





protected override void CreateChildControls() 
{ 
    Label lab = new Label(); 

    lab.Text = Prompt; 
    lab.ForeColor = System.Drawing.Color.Red; 
    this.Controls.Add(lab); 

    Literal lit = new Literal(); 
    lit.Text = "<br />"; 
    this.Controls.Add(lit); 

    TextBox tb = new TextBox(); 
    tb.ID = "tb1"; 
    tb.Text = DOB.ToString(); 
    this.Controls.Add(tb); 

    base.CreateChildControls(); 
} 




[Designer(typeof(SimpleCompositeControlDesigner))] 
public class SimpleCompositeControl : CompositeControl 


public class SimpleCompositeControlDesigner : CompositeControlDesigner 
{ 
    // Set this property to prevent the designer from being resized. 
    public override bool AllowResize 
    { 
     get { return false; } 
    } 
} 



} 

如何得到類型或名稱空間設計不存在?

回答

0

您需要爲您的項目添加一個Assembly引用。在您的解決方案資源管理器中,右鍵單擊引用 - >添加引用 - >程序集 - >框架 - >然後選中System.Web旁邊的框,然後點擊確定將該DLL添加到您的項目中。

System.Design做同樣的事情。


編輯:要爲網站做到這一點。當您將.cs文件添加到您的網站解決方案時,Visual Studio可能會提示您將此文件移動到App_Code文件夾中,假設您的答案是肯定的,則應如您所願。然後右鍵單擊該文件夾並單擊添加引用來添加System.Design,因爲System.Web默認包含在網站中。

+0

當我去解決方案資源管理器,我沒有選項參考,我所做的只是創建一個新的網站 – user2439070

+0

@ user2439070我已經更新了我的解決方案,爲網站解決方案做到這一點 – BenVlodgi