写了自定义控件,也给相应的网站项目添加了引用,但是从工具箱面板上把该自定义控件拖到页面时说:
呈现控件时出错初始化字符串格式不符合规范,是哪里错误了呢?

解决方案 »

  1.   

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