I am working on creating buttons that are created dynamically based the results of a SQL Query:
private void createPagingButtons(DateTime firstDayofWeek, DateTime lastDayofWeek)
{
int i = 1;
SqlDataReader returnedQuery = getDefaultUser(firstDayofWeek, lastDayofWeek);
while (returnedQuery.Read())
{
string buttonName = returnedQuery["Person"].ToString();
System.Diagnostics.Debug.WriteLine(buttonName);
Button btn = new Button();
btn.ID = i.ToString();
btn.Click += new EventHandler(btn_Click);
btn.Text = buttonName;
pagingPanel.Controls.Add(btn);
i++;
}
}
The way I am trying to assign unique button ID's is by assigning them a number that is incremented each time the while loop iterates:
btn.ID = i.ToString();
But it's not working and I am getting an error: Multiple controls with the same ID '1' were found. FindControl requires that controls have unique IDs.
WHy is this happening and how can I fix it?