创建基本控件设计器

时间:2014-03-31 20:42:57

标签: c#

我正在尝试按照步骤进行:

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

基本上我用asp.net razor 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; }
    }
}



}

为什么我的类型或命名空间设计不存在?

1 个答案:

答案 0 :(得分:0)

您需要为项目添加程序集引用。在您的解决方案资源管理器中,右键单击引用 - &gt;添加参考 - &gt;装配 - &gt;框架 - &gt;然后选中System.Web旁边的框并点击Okay将该DLL添加到您的项目中。

System.Design执行相同操作。


修改:为网站执行此操作。当您向网站解决方案添加.cs文件时,Visual Studio可能会提示您将此文件移动到App_Code文件夹,假设您说是,则应该如此。然后右键单击该文件夹并点击添加引用以添加System.Design,因为System.Web默认包含在网站中。