简体   繁体   中英

Creating a Basic Control Designer

I'm trying to follow the walk-through:

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

basically I have created a new website with asp.net razor 4

and added this code :

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; }
    }
}



}

how come im getting the type or namespace design does not exist ?

You need to add an Assembly reference to your project. In your Solution Explorer right click on References -> Add Reference -> Assemblies -> Framework -> then check the box next to System.Web and hit Okay to add that DLL to your project.

Do the same for System.Design .


Edit : To do this for a website. When you added a .cs file to your website solution, visual studio probably prompted you to have this file moved to an App_Code folder, assuming you said yes, as you should. Then right click on that folder and click add reference to add System.Design as System.Web is by default included in websites.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM