Apr 13, 2012, 7:28 PM
saving dynamically-created controls' values
I have the following window in which I'm creating controls from the code-behind and adding them to the FieldSet:
<ext:Window ID="winMapping" runat="server" AutoHeight="true" Closable="false" Width="400" AutoShow="false" Modal="true" Hidden="true" Layout="FitLayout" Icon="TableRelationship" OnLoad="winMapping_OnLoad" Maximizable="true">
<Items>
<ext:FormPanel ID="fpMapping" runat="server" AutoHeight="true" Padding="5" MonitorValid="true" MonitorPoll="500">
<Defaults>
<ext:Parameter Name="MsgTarget" Value="side" />
</Defaults>
<Items>
<ext:Hidden ID="hidSCFElementId" runat="server" />
<ext:Hidden ID="hidSourceSystemMappingId" runat="server" />
<ext:Hidden ID="hidRequestType" runat="server" />
<ext:FieldSet ID="fsSegmentFieldPairs" runat="server" Title="Segment/Field Pairs" Layout="FormLayout" Collapsible="false">
<Items>
<ext:Hidden ID="hidFieldPairId_1" runat="server" />
<ext:TextField ID="txtSegment_1" runat="server" FieldLabel="Segment/Table 1" Width="200" AllowBlank="false" />
<ext:TextField ID="txtFieldName_1" runat="server" FieldLabel="Field Name 1" Width="200" AllowBlank="false" />
</Items>
<BottomBar>
<ext:Toolbar runat="server">
<Items>
<ext:Button ID="btnAddPair" runat="server" Text="Add Pair" Icon="Add" StandOut="true">
<DirectEvents>
<Click OnEvent="btnAddPair_Click" />
</DirectEvents>
</ext:Button>
</Items>
</ext:Toolbar>
</BottomBar>
</ext:FieldSet>
</Items>
<Buttons>
<ext:Button ID="btnSaveMapping" runat="server" Text="Save" Icon="Disk" AutoPostBack="false">
<Listeners>
<Click Handler="saveMappings(#{fsSegmentFieldPairs});" />
</Listeners>
</ext:Button>
</Buttons>
</ext:FormPanel>
</Items>
</ext:Window>
Here's a snippet of code depicting how I add dynamic controls to the FieldSet: hid = new Hidden();
hid.ID = String.Format("hidFieldPairId_{0}", intIndex + 1);
hid.AddTo(this.fsSegmentFieldPairs);
txt = new TextField();
txt.ID = String.Format("txtSegment_{0}", intIndex + 1);
txt.FieldLabel = String.Format("Segment/Table {0}", intIndex + 1);
txt.Width = 200;
txt.AllowBlank = false;
txt.AddTo(this.fsSegmentFieldPairs);
txt = new TextField();
txt.ID = String.Format("txtFieldName_{0}", intIndex + 1);
txt.FieldLabel = String.Format("Field Name {0}", intIndex + 1);
txt.Width = 200;
txt.AllowBlank = false;
txt.AddTo(this.fsSegmentFieldPairs);
When I click the Window's "Save" button, the function shown below is invoked on the client. The code recognizes that I have added new controls (fsSegmentFieldPairs.items.length has the correct number). None of the dynamically-created controls have a value attribute on them. What am I doing wrong? How do I retrieve the value from these newly created controls? var saveMappings = function (fsSegmentFieldPairs) {
var ctrl = null;
var intSourceSystemMappingFieldId = null;
var strSegment, strField, strListItem;
var arrCtrl = [];
for (var intIndex = 0; intIndex < fsSegmentFieldPairs.items.length; intIndex++) {
ctrl = fsSegmentFieldPairs.items.items[intIndex];
if (ctrl.id.indexOf("hidFieldPairId_") > -1) {
intSourceSystemMappingFieldId = ctrl.value;
// if there is no value, then this pair needs to be added to the DB
if (ctrl.value !== undefined && ctrl.value !== null) {
intSourceSystemMappingFieldId = parseInt(ctrl.value);
}
}
else if (ctrl.id.indexOf("txtSegment_") > -1) {
strSegment = ctrl.value;
}
else if (ctrl.id.indexOf("txtFieldName_") > -1) {
strField = ctrl.value;
strListItem = intSourceSystemMappingFieldId + "|" + strSegment + "|" + strField;
arrCtrl.push(strListItem);
}
}
for (var intIndex = 0; intIndex < arrCtrl.length; intIndex++) {
alert(arrCtrl[intIndex]);
}
};