简体   繁体   中英

Visual Studio keeps replacing my custom control namespace in designer.cs

I try to create a custom control which inherits from GridView ( using tut http://msdn.microsoft.com/en-us/library/yhzc935f(v=vs.100).aspx ). After each build Visual Studio keeps replacing my reference to my custom namespace with its own in Default.aspx.designer.cs

protected global::System.Web.UI.WebControls.WebParts.GridViewCustom GridView1;

each time I did put

protected GridViewCustom.GridViewCustom GridView1;

Why ?

In assembly.cs I have added

using System.Web.UI;
...
[assembly: TagPrefix("GridViewCustom", "GridViewCustom")]

In default.aspx I have:

    <asp:GridViewCustom ID="GridView1" runat="server">
    </asp:GridViewCustom>

This is my source code for the control (source file is in App_Code):

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace GridViewCustom
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:GridViewCustom runat=server></{0}:GridViewCustom>")]
    public class GridViewCustom : System.Web.UI.WebControls.GridView
    {
        [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;
            }
        }

        protected override void RenderContents(HtmlTextWriter output)
        {
            output.Write(Text);
        }
    }
}

I'm having the same problem. One option is suggested by the autogenerated designer file -- remove the declaration from the .designer.cs file and explicitly declare it in your codebehind file.

Otherwise, you might try looking into the solution outlined in this question: Visual Studio 2010 keeps changing my winforms control .

Update: Having registered my controls globally in a web.config file , I just experimented with changing the tagPrefix declaration to something unique, as opposed to an existing prefix shared by other custom controls, and suddenly everything is fine. The strange thing is that I no longer see the declaration in the designer file, but it still works.

Still confusing.

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