开发者

Problem with Generate Local Resource and My simple custom control

I have created simple controls that are based on dot net controls . For example there is a simple GridView control that is based on dot net GridView control , I just set some setting in my control to use it in my .aspx pages , for example I set the Width of GridView in the constructor method :

// constructor of my custom class
public GridView(): base()
        {
      开发者_如何转开发      this.Width = new Unit(100, UnitType.Percentage);
        }

and also I've added some custom properties :

    public int SelectedID
    {
        get
        {
            if (ViewState["SelectedID" + this.ID] == null)
                ViewState["SelectedID" + this.ID] = "-1";
            return Convert.ToInt32(ViewState["SelectedID" + this.ID]);
        }
        set
        {
            ViewState["SelectedID" + this.ID] = value;
        }
    }

The *Problem* : when I use Tools>Generate Local Resource in VS2010

the aspx markup before I use this tool is like this :

<RPC:GridView ID="grdData" runat="server" onrowcommand="grdData_RowCommand">

but this tool adds any public property or any setting to my aspx markup , like this :

<RPC:GridView ID="grdData" runat="server" onrowcommand="grdData_RowCommand"                                                 
                                                meta:resourcekey="grdDataResource1" SelectedID="-1" Width="100%">

I don't like VS2010 add my settings (like width) and my custom properties (like SelectedID) to aspx markup , this prevent me having the ability of changing my custom control code and reflect changes in all aspx pages that include this control , for example if

I change the width of my control to 50% , it doesn't reflect to any pages

Please tell me what should I do to fix my problem

Thank you very much for your feedbacks


This is a slightly complicated topic to address in one answer here to be honest! There are more than one approaches you can take to resolve this problem. It all depends on the kind of properties your control has and if it is a templated control or not. As a quick fix try decorating your public properties with the following attribute

 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]

or if you don't want the user to be able to set the public property at all via HTML markup then use

 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]

another attribute declaration which will be helpful with

[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]

is

PersistenceMode(PersistenceMode.Attribute)


I've found doing any initialisation in the ctor causes major headaches for local resource generation (even corruption). Use the DefaultValue attribute on properties and/or use OnLoad if possible. (As a side note use CSS rather than explicitly setting control width).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜