写了自定义控件,也给相应的网站项目添加了引用,但是从工具箱面板上把该自定义控件拖到页面时说: 
呈现控件时出错初始化字符串格式不符合规范,是哪里错误了呢?
一下是自定义控件代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using System.Data;
using System.Xml;namespace WebControlLibrary1
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:WebCustomControl1 runat=server></{0}:WebCustomControl1>")]
    public class showAd : WebControl
    {
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Text
        {
            get
            {
                String s = (String)ViewState["Text"];
                return ((s == null) ? String.Empty : s);
            }            set
            {
                ViewState["Text"] = value;
            }
        }
        [Bindable(true)]
        [Category("appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string type
        {
            get
            {
                String s = (String)ViewState["type"];
                return ((s == null) ? String.Empty : s);
            }
            set
            {
                ViewState["type"] = value;
            }
        }        [Bindable(true)]
        [Category("appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string adid
        {
            get
            {
                String s = (String)ViewState["adid"];
                return ((s == null) ? String.Empty : s);
            }
            set
            {
                ViewState["adid"] = value;
            }
        }        [Bindable(true)]
        [Category("appearance")]
        [DefaultValue("~/App_Data/maikou.mdb")]
        [Localizable(true)]
        public string dbpath
        {
            get
            {
                String s = (String)ViewState["dbpath"];
                return ((s == null) ? String.Empty : s);
            }
            set
            {
                ViewState["dbpath"] = value;
            }
        }        [Bindable(true)]
        [Category("appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public String showNum
        {
            get
            {
                String s = (String)ViewState["shownum"];
                return ((s == null) ? String.Empty : s);
            }
            set
            {
                ViewState["shownum"] = value;
            }
        }        [Bindable(true)]
        [Category("appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string cssStyle
        {
            get
            {
                String s = (String)ViewState["cssStyle"];
                return ((s == null) ? String.Empty : s);
            }
            set
            {
                ViewState["cssStyle"] = value;
            }
        }        protected override void RenderContents(HtmlTextWriter output)
        {
            string connstr = MapPathSecure("~/App_Data/maikou.mdb");
            if (dbpath != null)
            {
                connstr = dbpath;
            }            OleDbConnection conn = new OleDbConnection(connstr);
            conn.Open();            string sql;
            if (type != null && adid != null)
            {
                sql = "select * from ads where types='" + type + "' and id='" + adid + "' order by id desc";
            }
            else if (type != null)
            {
                sql = "select * from ads where types='" + type + "' order by id desc";
            }
            else if (adid != null)
            {
                sql = "select * from ads where id='" + adid + "' order by id desc";
            }
            else
            {
                sql = "select * from ads order by id desc";
            }            OleDbDataAdapter oda = new OleDbDataAdapter(sql, conn);
            DataSet ds = new DataSet();
            int count = oda.Fill(ds, "adtable");
            DataTable thistable = ds.Tables["adtable"];
            if (count > 0)
            {
                if (count > Convert.ToInt32(showNum))
                {
                    count = Convert.ToInt32(showNum);
                    StringBuilder build = new StringBuilder();
                    build.Append("<div style=\"border:1px solid #ffcc99;\">");
                    for (int i = 0; i < count; i++)
                    {
                        build.Append("<div style=\"border:1px dashed #cccccc;\"><a href=\"" + thistable.Rows[i]["url"].ToString() + "\" target=\"black\">" + thistable.Rows[i]["name"].ToString() + "</a></div>");
                    }
                    build.Append("</div>");
                    Text = build.ToString();
                }
            }
            else
            {
                Text = "暂时没有数据";
            }            output.Write(Text);        }
    }
}

解决方案 »

  1.   

    RenderContents
    在StringBuilder build = new StringBuilder();下断点调试,
    看是哪行Append的东西有问题了,多数是带些非法自发什么的,譬如是\错误使用
      

  2.   

    不错是这里错误了,应该改为:
    build.Append("<div style=\‘border:1px solid #ffcc99;\’>");
                        for (int i = 0; i < count; i++)
                        {
                            build.Append("<div style=\‘border:1px dashed #cccccc;\’><a href=\‘" + thistable.Rows[i]["url"].ToString() + "\’ target=\‘black\’>" + thistable.Rows[i]["name"].ToString() + "</a></div>");
                        }
      

  3.   

    代码太多,没有仔细看你的代码。不过,控件的设计时界面正规地方式是要使用一个DesignerAttribute来通知设计器的。参考:http://www.google.cn/search?hl=zh-CN&rls=com.microsoft%3A*%3AIE-SearchBox&rlz=1I7GGLL_zh-CN&newwindow=1&q=asp.net+DesignerAttribute+%E8%AE%BE%E8%AE%A1&aq=f&oq=。由它所标记的类型的实例来获得在设计器上的html代码,而不是运行你的对象。而如果你觉得这样麻烦,不去设置,那么设计器就会实际去实例化并运行你的对象。那么你的对象就不能去Render,而要检测控件的属性.Site.DesignMode ,如果是设计模式就应该去给一个“假的”显示,而不能去真的去连接数据库等等操作。
      

  4.   

    么你的对象就不能去Render  -->  么你的对象就要修改Render中的流程,首先要判断是否在设计环境中