Giorgio Giorgio - 6 months ago 71
ASP.NET (C#) Question

Type UserControl does not have a public property named Columns

I'm trying to develop a

UserControl
containing a
GridView
inside a
Panel


<asp:Panel runat="server" ID="panel">
<asp:GridView ID="gridView" runat="server" AutoGenerateColumns="false">
</asp:GridView>
</asp:Panel>


Code behind

public partial class DropDownMultiColumn : UserControl
{
[System.Web.UI.IDReferenceProperty(typeof(DataControlFieldCollection))]
[PersistenceMode(PersistenceMode.InnerProperty)]

public virtual DataControlFieldCollection Columns
{
get { return gridView.Columns; }
set
{
gridView.Columns.Clear();
foreach (DataControlField c in value)
{
gridView.Columns.Add(c);
}
}
}
}


I've included the control into a ASPX page, using the
Columns
public property

<dd:DropDownMultiColumn id="_dd_InsertStore" runat="server" DataSourceID="_dsTest" >
<Columns>
<asp:BoundField DataField="MGCOD" HeaderText="MGCOD"></asp:BoundField>
<asp:BoundField DataField="MGDSC" HeaderText="MGDSC"></asp:BoundField>
</Columns>
</dd:DropDownMultiColumn>


But I get the following error in Design section:


Type 'System.Web.UI.UserControl' does not have a public property named 'Columns'


I've tried to implement this solution, but it doesn't work. Any ideas?

Answer Source

Here follows a working workaround for the issue.

Declare a property with JSON content

Declare Columns as an attribute, containing required data in JSON format

<dd:DropDownMultiColumn id="_ddInsertStore" runat="server" DataSourceID="_dsTest" Columns="
    [{ 'DataField':'MGCOD', 'HeaderText':'MGCOD' },
    { 'DataField':'MGDSC', 'HeaderText':'MGDSC' }]">
</dd:DropDownMultiColumn>


Convert JSON to BoundField

Within OnLoad event, deserialize JSON and build BoundField data

protected override void OnLoad(EventArgs e)
{
    List<Column> columns = JsonConvert.DeserializeObject<List<Column>>(Columns);

    gridView.Columns.Clear();
    foreach (Column column in columns)
    {
        BoundField boundField = new BoundField();
        boundField.DataField = column.DataField;
        boundField.HeaderText = (string) column.HeaderText;
        gridView.Columns.Add(boundField);
    }
}

Declare a Column class, having the required properties.

public class Column
{
    public string DataField { get; set; }
    public string HeaderText { get; set; }
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download