2014-03-31 74 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