I am making a dynamic table that will add rows whenever the add row button is clicked. I create a button programmatically and add it to the table title. Below this button, as well as in the same column, there will be buttons for deleting rows.
I have a problem, but when I click the button, the event is not raised. Did I create the button correctly? If not, how can I do this? If so, do you know what the problem is?
area Add_Table_Header
TableHeaderCell thcOne = new TableHeaderCell();
TableHeaderCell thcTwo = new TableHeaderCell();
TableHeaderCell thcThree = new TableHeaderCell();
TableHeaderCell thcrFour = new TableHeaderCell();
TableHeaderCell thcFive = new TableHeaderCell();
TableCell thcRowAction = new TableCell();
thcOne.Text = "Who";
thcTwo.Text = "Date Started";
thcThree.Text = "Date Ended";
thcrFour.Text = "Causes?";
thcFive.Text = "Result";
Button addRowButton = new Button();
addRowButton.Text = "Add Row";
addRowButton.Click += new EventHandler(this.AddNewRow_Click);
thcRowAction.Controls.Add(addRowButton);
TableHeaderRow headerRow = new TableHeaderRow();
headerRow.Cells.Add(thcOne);
headerRow.Cells.Add(thcTwo);
headerRow.Cells.Add(thcThree);
headerRow.Cells.Add(thcrFour);
headerRow.Cells.Add(thcFive);
headerRow.Cells.Add(thcRowAction);
table.Rows.Add(headerRow);
#endregion
protected void AddNewRow_Click(object sender, EventArgs e)
{
if (ViewState["RowsCount"] != null)
{
numOfRows = Convert.ToInt32(ViewState["RowsCount"]);
GenerateTable(numOfRows);
}
}
The button appears again, but it does not enter the correct event method. Thanks for your help and time :)
By the way, when I do it declaratively, for example:
<asp:Button ID="BTNAdd" runat="server" Text="Add New Row" OnClick="AddNewRow_Click" />
the event will be logged and work completely normal.
:
, - , , , , , , , ?
:)