PDA

View Full Version : [CLOSED] Reading dynamically created items in fieldset



debarry
Jul 01, 2014, 8:56 PM
Hello!
I have a fieldset like this:


<ext:FieldSet ID="FieldSetIndex" runat="server" ColumnWidth="0.4" Title="Escolha uma ou mais op??es de filtro:" MarginSpec="10" ButtonAlign="Right">
<Defaults>
<ext:Parameter Name="Width" Value="240" />
<ext:Parameter Name="LabelWidth" Value="80" />
</Defaults>
<Items>
<ext:SelectBox ID="DDIdClass" runat="server" DisplayField="ClassTitulo" ValueField="IdClass" FieldLabel="Classifica??o" ondirectselect="DDIdClass_DirectSelect" >
<Store>
<ext:Store ID="Store3" runat="server" DataSourceID="SqlDataSourceClassList">
<Model>
<ext:Model ID="ModelClass" runat="server">
<Fields>
<ext:ModelField Name="IdClass" />
<ext:ModelField Name="ClassTitulo" />
</Fields>
</ext:Model>
</Model>
</ext:Store>
</Store>
</ext:SelectBox>
<ext:TextField ID="TbDocTitulo" runat="server" FieldLabel="Titulo"></ext:TextField>
<ext:DateField ID="DocData1" runat="server" FieldLabel="De:" Format="dd/m/yyyy" />
<ext:DateField ID="DocData2" runat="server" FieldLabel="At?:" Format="dd/m/yyyy"/>
</Items>
</ext:FieldSet>

I can add items to it from code behind on the DDIdClass_DirectSelect event... like this:



(...)
var tf = new TextField()
{
ID = "ID_" + varData["IdCampo"].ToString(),
Name = Convert.ToString(varData["CamTitulo"].ToString()),
FieldLabel = Convert.ToString(varData["CamTitulo"].ToString())
};
FieldSetIndex.Items.Add(tf);
(...)
FieldSetIndex.Render();


But, when I loop the Items on FieldSetIndex control the dinamically added items dont show up... why?




protected void BtFiltrar_OnDirectClick(object sender, DirectEventArgs e){

(...)

foreach (var Control in FieldSetIndex.Items)
{
if (Control.XType.ToString() == "textfield")
{
TextField tf = (TextField)Control;
if (tf.ID.StartsWith("ID_"))
{

}
}
}

I can only see 4 items on FieldSetIndex.Items.Count property - where is the item created dinamically?
Am I missing something?

Thank you!

geoffrey.mcgill
Jul 01, 2014, 10:23 PM
I'm guessing the full scenario of your page since you've only posted a few snippets, but... it appears you're adding the TextFields during one request (DirectEvent), and foreach looping through the .Items in another DirectEvent request.

Because ASP.NET is stateless, what is done in one Request is forgotten in the next. If you want access to all the items in the collection, the Controls must be recreated during each request and added to the Page's Controls Collection. The <TextField>'s in markup are recreated on each request, and the TextFields added only during the one DirectEvent request are not.

We've built some functionality to somewhat work-around this "feature" of ASP.NET (and web applications in general), but you'll need to know the .ID of the Control. You can get an instance of dynamically created Fields by calling the following:


var field = X.GetCmp<TextField>("THE_ID");

Hope this helps.

Daniil
Jul 08, 2014, 6:44 AM
Just to clarify a bit.

This

X.GetCmp<TextField>("THE_ID")
doesn't return a real control instance. It is only a proxy.

It loads the data from POST to that control. For example, in the case with a TextField, it will retrieve a Text from POST.

Except POST there won't be any other real information. For example, you won't be able to access the TextField. Or, for example, if you get a Panel via
X.GetCmp<Panel>("Panel_ID")
it won't have any children in the Items collection. Again, only the data from POST, if any.

So, loading the data from POST to a proxy control is one side of the X.GetCmp<>() feature.

Another one that a proxy control is useful to set properties and call methods.

For example, this sets the TextField's Text. It will actually appear on client.

var field = X.GetCmp<TextField>("TextField_ID");
field.Text = "New Text";

This collapses the Panel on client.

var panel = X.GetCmp<Panel>("Panel_ID");
panel.Collapse();