PDA

View Full Version : [CLOSED] Child Controls and ID property



Zdenek
Feb 02, 2015, 7:57 AM
Hello
From the past I remember some problems having when child controls does not have ID property set Unfortunatelly I don't remember which one, but anyway - any idea why following controls is failing?



public class TimePickerControl : Container
{
private TextField _timeField;
private ComboBox _timeZoneField;

protected override void OnInit(EventArgs e)
{
_timeField = new TextField()
{
//Name = ID + "TimeField",
ItemID = "TimeField",
Vtype = "timeField",

MarginSpec = "0 2 0 0",
Width = 38
};

_timeZoneField = new ComboBox
{
Editable = false,
Width = 40,
ForceSelection = true,
//Name = ID + "TimeZoneField",
ItemID = "TimeZoneField",

};

if (string.IsNullOrEmpty(ID) == false) // IS THIS A GOOD IDEA?
{
_timeField.ID = ID = ID + "_TimeFieldTime";
_timeZoneField.ID = ID + "_TimeZoneField";
}

var listItem = new ListItem("AM", "AM") {};
_timeZoneField.Items.Add(listItem);
_timeZoneField.Items.Add(new ListItem("PM", "PM"));

if(Page.IsPostBack==false)
_timeZoneField.SelectedItems.Add(listItem);


Layout = "column";
Items.Add(_timeField);
Items.Add(_timeZoneField);

base.OnInit(e);

}
}

when added to the page



<%@ Page Language="C#" %>
<%@ Register TagPrefix="alc" Namespace="ALA.ExtControls.ComplexControls" Assembly="ALA.ExtControls" %>
<%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
<!DOCTYPE html>

<html>
<head runat="server">
<title>Ext.NET v2 Example</title>
</head>
<body>
<ext:ResourceManager runat="server" />
<alc:TimePickerControl runat="server" ID="dateEditor" />
</body>
</html>


I'm getting

" Control with an ID of "App.dateEditor_TimeFieldTime" has already been initialized"


strange is that if I comment out timeField and leave just timezone field inside, it works without problem

so 2 quetions here
1) is it a good idea to set client controls IDs
2) why I'm getting error doing that

Thanks

Daniil
Feb 03, 2015, 2:07 PM
Hi @Zdenek,

This line looks wrong:

_timeField.ID = ID = ID + "_TimeFieldTime";

You apply the same ID to the _timeField's control and for the TimePickerControl control itself.

The line is identical to:

ID = ID + "_TimeFieldTime";x
_timeField.ID = ID;