Web User Control Populate Items via XML

2019-06-23 19:40发布

问题:

I have a web user control name as Chart Control, and I have a drop down List inside Chart Control I want to populate DropDown List of Chart Control like this:

<UC:ChartControl ID="abc" runat="server">
        <ChartDropDownItems>
            <asp:ListItem Text="Graph_Amount_Items_Sold" Value="0"></asp:ListItem>
            <asp:ListItem Text="Graph_Ranking" Value="1"></asp:ListItem>
            <asp:ListItem Text="Graph_Share_Amount_Items_Sold" Value="2"></asp:ListItem>
            <asp:ListItem Text="Graph_Share_Unit_items_Sold" Value="3" Selected="True"></asp:ListItem>
            <asp:ListItem Text="Graph_Unit_items_Sold" Value="4"></asp:ListItem>
        </ChartDropDownItems>
    </UC:ChartControl>

in .cs code

 [DefaultProperty("ChartDropDownItems"),
    ParseChildren(true, "ChartDropDownItems"), PersistChildren(false),
    ToolboxData("<{0}:ChartControl runat=\"server\"> </{0}:ChartControl>")]

    public partial class ChartControl : System.Web.UI.UserControl
    {
     private List<ListItem> lst;
            [Browsable(true)]
            [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
            [PersistenceMode(PersistenceMode.InnerProperty)]

            public List<ListItem> ChartDropDownItems
            {
                set
                {
                    lst = value;
                    Bind_ddChartChange();
                }
            }
     private void Bind_ddChartChange()
            {

                if (lst != null)
                {
                    ddChartChange.DataSource = lst;
                    ddChartChange.DataTextField = "Text";
                    ddChartChange.DataValueField = "Value";
                    ddChartChange.DataBind();
                    ListItem li = lst.Find(x => x.Selected == true);
                    ddChartChange.SelectedValue = li.Value;

                }
            }
}

When I compile and run it works fine for me, but at design time it says

"Error Creating Control"
"Type'System.Web.UI.Control' does not have a public property named 'ChartDropDownItems'"

I want to work it even at design time. Can any body suggest me accordingly ? Thanks.

回答1:

Ok, after hours of google-fu, I have concluded that it's either not possible to see inner child tags in the designer, or no one has bothered to give a good answer of how to do so.

According to wonkim00's answer, which is a quote from this MSDN site:

Note: Templated ASP.NET user controls are not supported in the Visual Studio designer. However, you can compile and run this example in Visual Studio. To do so, when you create ASP.NET pages to test this code, replace all the designer-generated code in the pages with the code and markup in the example listings.

However, as you can tell, this is a notice for templates, but after me trying it out with many other datatypes (Lists, Collections, ArrayLists), It appears to be the same.

There might be an attribute that we all are missing to get it viewable in the designer, but it seems to be quite hidden (please, anyone, feel free to prove me wrong).

I just find this strange that the code compiles and works, but you can't get designer to shut up about missing properties.

Links I used:

  • ASP NET Custom Control with PersistenceMode.InnerProperty including Server Controls
  • Aleris's Answer for Custom elements in ASP.NET with custom child-elements
  • MSDN How to: Create Templated ASP.NET User Controls (see comments)
  • How do I get design mode to work for templated user controls?
  • How to Fix Visual Studio Error 'System.Web.UI.UserControl' does not contain a definition for

Maybe those will help, maybe not. Sorry for the bad news, but it's the best I could find.



回答2:

Make sure your class is called ChartControl and that its derived from UserControl not from Control this could be the reason for this error, hope this helps.