Hi,
I have a asp page that loads controls dynamicly and would like to add events to these components. I have done some research on the net but could not get to the root of the problem. I think it might be something i leave out due to me being new to .Net development.
When i add a button in design time to a form and set the event to my created event procedure it fires the event but not from my dynamicly created buttons.
Here is my code.
++++++++++++++++++++++++CODE START++++++++++++++++++++++++
procedure TDefault.CreateTable(Container: Control);
var
l_Options : tStringList;
l_TableColumns : tStringList;
l_QuestionData : tStringList;
l_QuestionRows : tStringList;
l_ControlIndex : String;
l_Index : Integer;
l_Index2 : Integer;
l_TextBox : TextBox;
l_CheckBox : CheckBox;
l_AddButton : Button;
l_UpdateButton : Button;
l_Table : Table;
l_TableRow : TableRow;
l_TableCell : TableCell;
begin
l_Options := nil;
l_TableColumns := nil;
l_QuestionData := nil;
l_QuestionRows := nil;
l_ControlIndex := '1';
try
l_Options := TStringList.Create;
l_TableColumns := TStringList.Create;
l_QuestionData := TStringList.Create;
l_QuestionRows := TStringList.Create;
l_Options.Add('TableColumns=Name;Surname;Position;Inactive;');
l_Options.Add('CheckBoxColumn=Inactive');
//if ControlType = 'ListBox' then begin
if True then
l_TableColumns.Text := stringreplace(l_Options.Values['TableColumns'], ';', #13#10, [rfReplaceAll]);
l_Table := Table.Create;
l_Table.ID := 'DataTable' + l_ControlIndex;
//l_Table.CellPadding := 2;
l_Table.BorderStyle := BorderStyle.Solid;
l_Table.BorderWidth := 2;
//Create Column Row
l_TableRow := TableRow.Create;
l_TableRow.ID := 'DataColumnRow' + l_ControlIndex;
// Create Column Cells
for l_Index := 0 to l_TableColumns.Count - 1 do begin
l_TableCell := TableCell.Create;
l_TableCell.ID := l_TableColumns.Strings[l_Index] + l_ControlIndex;
l_TableCell.CssClass := 'TableCell';
l_TableCell.Text := l_TableColumns.Strings[l_Index];
l_TableCell.BorderStyle := BorderStyle.Solid;
l_TableCell.BorderWidth := 2;
l_TableRow.Cells.Add(l_TableCell);
end;
{// Create Checkbox Cell
if l_Options.IndexOfName('CheckBoxColumn') > -1 then begin
l_TableCell := TableCell.Create;
l_TableCell.ID := 'CheckBoxColumn' + l_ControlIndex;
l_TableCell.CssClass := 'TableCell';
l_TableCell.Text := l_Options.Values['CheckBoxColumn'];
l_TableCell.BorderStyle := BorderStyle.Solid;
l_TableCell.BorderWidth := 2;
l_TableRow.Cells.Add(l_TableCell);
end;}
// Create Update Button Cell
l_TableCell := TableCell.Create;
l_TableCell.ID := 'UpdateColumn' + l_ControlIndex;
l_TableCell.CssClass := 'TableCell';
l_TableCell.Text := 'Update';
l_TableCell.BorderStyle := BorderStyle.Solid;
l_TableCell.BorderWidth := 2;
l_TableRow.Cells.Add(l_TableCell);
//Add Column Row To Table
l_Table.Rows.Add(l_TableRow);
//Add Column Row To Table
l_Table.Rows.Add(l_TableRow);
//Get Data from DB
GetQuestionData( 'QuestionID', l_QuestionData);
for l_Index := 0 to l_QuestionData.Count -1 do begin
l_QuestionRows.Text := StringReplace(l_QuestionData.Strings[l_Index], ';', #13#10, [rfReplaceAll]);
//Create Data Row
l_TableRow := TableRow.Create;
l_TableRow.ID := 'DataColumnRow_' + inttostr(l_Index) + '_' + l_ControlIndex;
// Get Data for each Column
for l_Index2 := 0 to l_QuestionRows.Count - 1 do begin
if l_Options.Values['CheckBoxColumn'] = l_QuestionRows.Names[l_Index2] then begin
l_TableCell := TableCell.Create;
l_TableCell.ID := 'DataCheckBoxCell' + 'QuestionID' + IntToStr(l_Index2) + l_ControlIndex;
l_TableCell.CssClass := 'TableCell';
//Create Checkbox
l_CheckBox := CheckBox.Create;
l_CheckBox.ID := 'DataCheckBox' + 'QuestionID' + IntToStr(l_Index2) + l_ControlIndex;
l_CheckBox.Text := '';
l_CheckBox.Checked := l_QuestionRows.ValueFromIndex[l_Index2] = '1';
l_TableCell.Controls.Add(l_CheckBox);
l_TableCell.BorderStyle := BorderStyle.Solid;
l_TableCell.BorderWidth := 2;
l_TableRow.Cells.Add(l_TableCell);
end
else begin
l_TableCell := TableCell.Create;
l_TableCell.ID := 'Data' + l_TableColumns.Strings[l_Index2] + l_ControlIndex;
l_TableCell.CssClass := 'TableCell';
l_TableCell.Text := l_QuestionRows.ValueFromIndex[l_Index2];
l_TableCell.BorderStyle := BorderStyle.Solid;
l_TableCell.BorderWidth := 2;
l_TableRow.Cells.Add(l_TableCell);
end;
end;
// Create Update Button Cell
l_TableCell := TableCell.Create;
l_TableCell.ID := 'UpdateColumn' + l_ControlIndex;
l_TableCell.CssClass := 'TableCell';
l_TableCell.BorderStyle := BorderStyle.Solid;
l_TableCell.BorderWidth := 2;
//Create AddButton
l_UpdateButton := Button.Create;
l_UpdateButton.CommandName := 'Update';
l_UpdateButton.ID := 'UpdateButton_Row' + Inttostr(l_Index) + '_' + l_ControlIndex;
l_UpdateButton.Text := 'Update';
Include(l_UpdateButton.Command, Self.UpdateButton_Click);
Self.UpdateButton_Click(l_UpdateButton, NIL);
l_TableCell.Controls.Add(l_UpdateButton);
l_TableRow.Cells.Add(l_TableCell);
l_Table.Rows.Add(l_TableRow);
end;
//Create Add Row
l_TableRow := TableRow.Create;
l_TableRow.ID := 'DataColumnRow' + l_ControlIndex;
//Add Extra Cells For Adding Values
for l_Index := 0 to l_TableColumns.Count - 1 do begin
if l_TableColumns.Strings[l_Index] = l_Options.Values['CheckBoxColumn'] then begin
// Create Checkbox Cell
l_TableCell := TableCell.Create;
l_TableCell.ID := 'AddCheckBoxCell_' + l_TableColumns.Strings[l_Index];
l_TableCell.BorderStyle := BorderStyle.Solid;
l_TableCell.BorderWidth := 2;
//Create Checkbox
l_CheckBox := CheckBox.Create;
l_CheckBox.ID := 'AddCheckBox_' + l_TableColumns.Strings[l_Index];
l_CheckBox.Text := '';
l_TableCell.Controls.Add(l_CheckBox);
l_TableRow.Cells.Add(l_TableCell);
end
else begin
l_TableCell := TableCell.Create;
l_TableCell.ID := 'AddTextBoxCell_' + l_TableColumns.Strings[l_Index];
l_TableCell.CssClass := 'TableCell';
l_TableCell.BorderStyle := BorderStyle.Solid;
l_TableCell.BorderWidth := 2;
//Create Textbox
l_TextBox := TextBox.Create;
l_TextBox.ID := 'AddTexBox_' + l_TableColumns.Strings[l_Index];
l_TextBox.CssClass := 'TableTextbox';
l_TableCell.Controls.Add(l_TextBox);
l_TableRow.Cells.Add(l_TableCell);
end;
end;
// Create Update Button Cell
l_TableCell := TableCell.Create;
l_TableCell.ID := 'UpdateColumn' + l_ControlIndex;
l_TableCell.CssClass := 'TableCell';
l_TableCell.BorderStyle := BorderStyle.Solid;
l_TableCell.BorderWidth := 2;
//Create AddButton
l_AddButton := Button.Create;
l_AddButton.ID := 'AddButton_' + l_ControlIndex;
l_AddButton.Text := 'Add';
Include(l_AddButton.Command, AddButton_Click);
l_TableCell.Controls.Add(l_AddButton);
l_TableRow.Cells.Add(l_TableCell);
l_TableRow.Cells.Add(l_TableCell);
l_Table.Rows.Add(l_TableRow);
( Container as Panel ).Controls.Add(l_Table);
//((Container.FindControl('QuestionPanel' + l_ControlIndex) as Panel ).FindControl('QuestionControlPanel' + l_ControlIndex) as Panel ).Controls.Add(l_Table);
finally
if l_Options <> nil then l_Options.Free();
if l_TableColumns <> nil then l_TableColumns.Free();
if l_QuestionData <> nil then l_QuestionData.Free();
if l_QuestionRows <> nil then l_QuestionData.Free();
end;
end;
++++++++++++++++++++++++CODE START++++++++++++++++++++++++
Thanks
Kaboose
I have a asp page that loads controls dynamicly and would like to add events to these components. I have done some research on the net but could not get to the root of the problem. I think it might be something i leave out due to me being new to .Net development.
When i add a button in design time to a form and set the event to my created event procedure it fires the event but not from my dynamicly created buttons.
Here is my code.
++++++++++++++++++++++++CODE START++++++++++++++++++++++++
procedure TDefault.CreateTable(Container: Control);
var
l_Options : tStringList;
l_TableColumns : tStringList;
l_QuestionData : tStringList;
l_QuestionRows : tStringList;
l_ControlIndex : String;
l_Index : Integer;
l_Index2 : Integer;
l_TextBox : TextBox;
l_CheckBox : CheckBox;
l_AddButton : Button;
l_UpdateButton : Button;
l_Table : Table;
l_TableRow : TableRow;
l_TableCell : TableCell;
begin
l_Options := nil;
l_TableColumns := nil;
l_QuestionData := nil;
l_QuestionRows := nil;
l_ControlIndex := '1';
try
l_Options := TStringList.Create;
l_TableColumns := TStringList.Create;
l_QuestionData := TStringList.Create;
l_QuestionRows := TStringList.Create;
l_Options.Add('TableColumns=Name;Surname;Position;Inactive;');
l_Options.Add('CheckBoxColumn=Inactive');
//if ControlType = 'ListBox' then begin
if True then
l_TableColumns.Text := stringreplace(l_Options.Values['TableColumns'], ';', #13#10, [rfReplaceAll]);
l_Table := Table.Create;
l_Table.ID := 'DataTable' + l_ControlIndex;
//l_Table.CellPadding := 2;
l_Table.BorderStyle := BorderStyle.Solid;
l_Table.BorderWidth := 2;
//Create Column Row
l_TableRow := TableRow.Create;
l_TableRow.ID := 'DataColumnRow' + l_ControlIndex;
// Create Column Cells
for l_Index := 0 to l_TableColumns.Count - 1 do begin
l_TableCell := TableCell.Create;
l_TableCell.ID := l_TableColumns.Strings[l_Index] + l_ControlIndex;
l_TableCell.CssClass := 'TableCell';
l_TableCell.Text := l_TableColumns.Strings[l_Index];
l_TableCell.BorderStyle := BorderStyle.Solid;
l_TableCell.BorderWidth := 2;
l_TableRow.Cells.Add(l_TableCell);
end;
{// Create Checkbox Cell
if l_Options.IndexOfName('CheckBoxColumn') > -1 then begin
l_TableCell := TableCell.Create;
l_TableCell.ID := 'CheckBoxColumn' + l_ControlIndex;
l_TableCell.CssClass := 'TableCell';
l_TableCell.Text := l_Options.Values['CheckBoxColumn'];
l_TableCell.BorderStyle := BorderStyle.Solid;
l_TableCell.BorderWidth := 2;
l_TableRow.Cells.Add(l_TableCell);
end;}
// Create Update Button Cell
l_TableCell := TableCell.Create;
l_TableCell.ID := 'UpdateColumn' + l_ControlIndex;
l_TableCell.CssClass := 'TableCell';
l_TableCell.Text := 'Update';
l_TableCell.BorderStyle := BorderStyle.Solid;
l_TableCell.BorderWidth := 2;
l_TableRow.Cells.Add(l_TableCell);
//Add Column Row To Table
l_Table.Rows.Add(l_TableRow);
//Add Column Row To Table
l_Table.Rows.Add(l_TableRow);
//Get Data from DB
GetQuestionData( 'QuestionID', l_QuestionData);
for l_Index := 0 to l_QuestionData.Count -1 do begin
l_QuestionRows.Text := StringReplace(l_QuestionData.Strings[l_Index], ';', #13#10, [rfReplaceAll]);
//Create Data Row
l_TableRow := TableRow.Create;
l_TableRow.ID := 'DataColumnRow_' + inttostr(l_Index) + '_' + l_ControlIndex;
// Get Data for each Column
for l_Index2 := 0 to l_QuestionRows.Count - 1 do begin
if l_Options.Values['CheckBoxColumn'] = l_QuestionRows.Names[l_Index2] then begin
l_TableCell := TableCell.Create;
l_TableCell.ID := 'DataCheckBoxCell' + 'QuestionID' + IntToStr(l_Index2) + l_ControlIndex;
l_TableCell.CssClass := 'TableCell';
//Create Checkbox
l_CheckBox := CheckBox.Create;
l_CheckBox.ID := 'DataCheckBox' + 'QuestionID' + IntToStr(l_Index2) + l_ControlIndex;
l_CheckBox.Text := '';
l_CheckBox.Checked := l_QuestionRows.ValueFromIndex[l_Index2] = '1';
l_TableCell.Controls.Add(l_CheckBox);
l_TableCell.BorderStyle := BorderStyle.Solid;
l_TableCell.BorderWidth := 2;
l_TableRow.Cells.Add(l_TableCell);
end
else begin
l_TableCell := TableCell.Create;
l_TableCell.ID := 'Data' + l_TableColumns.Strings[l_Index2] + l_ControlIndex;
l_TableCell.CssClass := 'TableCell';
l_TableCell.Text := l_QuestionRows.ValueFromIndex[l_Index2];
l_TableCell.BorderStyle := BorderStyle.Solid;
l_TableCell.BorderWidth := 2;
l_TableRow.Cells.Add(l_TableCell);
end;
end;
// Create Update Button Cell
l_TableCell := TableCell.Create;
l_TableCell.ID := 'UpdateColumn' + l_ControlIndex;
l_TableCell.CssClass := 'TableCell';
l_TableCell.BorderStyle := BorderStyle.Solid;
l_TableCell.BorderWidth := 2;
//Create AddButton
l_UpdateButton := Button.Create;
l_UpdateButton.CommandName := 'Update';
l_UpdateButton.ID := 'UpdateButton_Row' + Inttostr(l_Index) + '_' + l_ControlIndex;
l_UpdateButton.Text := 'Update';
Include(l_UpdateButton.Command, Self.UpdateButton_Click);
Self.UpdateButton_Click(l_UpdateButton, NIL);
l_TableCell.Controls.Add(l_UpdateButton);
l_TableRow.Cells.Add(l_TableCell);
l_Table.Rows.Add(l_TableRow);
end;
//Create Add Row
l_TableRow := TableRow.Create;
l_TableRow.ID := 'DataColumnRow' + l_ControlIndex;
//Add Extra Cells For Adding Values
for l_Index := 0 to l_TableColumns.Count - 1 do begin
if l_TableColumns.Strings[l_Index] = l_Options.Values['CheckBoxColumn'] then begin
// Create Checkbox Cell
l_TableCell := TableCell.Create;
l_TableCell.ID := 'AddCheckBoxCell_' + l_TableColumns.Strings[l_Index];
l_TableCell.BorderStyle := BorderStyle.Solid;
l_TableCell.BorderWidth := 2;
//Create Checkbox
l_CheckBox := CheckBox.Create;
l_CheckBox.ID := 'AddCheckBox_' + l_TableColumns.Strings[l_Index];
l_CheckBox.Text := '';
l_TableCell.Controls.Add(l_CheckBox);
l_TableRow.Cells.Add(l_TableCell);
end
else begin
l_TableCell := TableCell.Create;
l_TableCell.ID := 'AddTextBoxCell_' + l_TableColumns.Strings[l_Index];
l_TableCell.CssClass := 'TableCell';
l_TableCell.BorderStyle := BorderStyle.Solid;
l_TableCell.BorderWidth := 2;
//Create Textbox
l_TextBox := TextBox.Create;
l_TextBox.ID := 'AddTexBox_' + l_TableColumns.Strings[l_Index];
l_TextBox.CssClass := 'TableTextbox';
l_TableCell.Controls.Add(l_TextBox);
l_TableRow.Cells.Add(l_TableCell);
end;
end;
// Create Update Button Cell
l_TableCell := TableCell.Create;
l_TableCell.ID := 'UpdateColumn' + l_ControlIndex;
l_TableCell.CssClass := 'TableCell';
l_TableCell.BorderStyle := BorderStyle.Solid;
l_TableCell.BorderWidth := 2;
//Create AddButton
l_AddButton := Button.Create;
l_AddButton.ID := 'AddButton_' + l_ControlIndex;
l_AddButton.Text := 'Add';
Include(l_AddButton.Command, AddButton_Click);
l_TableCell.Controls.Add(l_AddButton);
l_TableRow.Cells.Add(l_TableCell);
l_TableRow.Cells.Add(l_TableCell);
l_Table.Rows.Add(l_TableRow);
( Container as Panel ).Controls.Add(l_Table);
//((Container.FindControl('QuestionPanel' + l_ControlIndex) as Panel ).FindControl('QuestionControlPanel' + l_ControlIndex) as Panel ).Controls.Add(l_Table);
finally
if l_Options <> nil then l_Options.Free();
if l_TableColumns <> nil then l_TableColumns.Free();
if l_QuestionData <> nil then l_QuestionData.Free();
if l_QuestionRows <> nil then l_QuestionData.Free();
end;
end;
++++++++++++++++++++++++CODE START++++++++++++++++++++++++
Thanks
Kaboose