I have a fairly simple application which returns a list of failed reports with an ID, Name and Time Bound fields and Checkbox template field on the left.
I have a 'Reschedule' button which when pressed, should pick up the rows where the checkbox has been ticked and process them.
The GridView loads up successfully and I can select/unselect the CheckBoxes but when I press the 'Reschedule' button and return to the code, the checkboxes no longer exist.
I know this is related to Dynamic Controls/Postback and that the Checkboxes need to be re-created and I've tried numerous suggestions to previous similar questions but nothing has worked
GridView - AutoGenerate Columns False (tried true)
Button - OnClientClick="" (tried return false)
The fields are initially created and bound to a data table (the data table has 3 columns mapping to the 3 Bound fields) like this:-
TemplateField tfield = new TemplateField();
failedSchedulesGridView.Columns.Add(tfield);
BoundField bfield1 = new BoundField();
bfield1.HeaderText = "SI_ID";
bfield1.DataField = "si_id";
failedSchedulesGridView.Columns.Add(bfield1);
BoundField bfield2 = new BoundField();
bfield2.HeaderText = "SI_NAME";
bfield2.DataField = "si_name";
failedSchedulesGridView.Columns.Add(bfield2);
BoundField bfield3 = new BoundField();
bfield3.HeaderText = "SI_UPDATE_TS";
bfield3.DataField = "si_update_ts";
failedSchedulesGridView.Columns.Add(bfield3);
failedSchedulesGridView.DataSource = dt;
failedSchedulesGridView.DataBind();
Page_Load As can be seen I've tried recreating the GridView columns here but it didn't work and is commented out
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
//TemplateField tfield = new TemplateField();
//failedSchedulesGridView.Columns.Add(tfield);
//BoundField bfield1 = new BoundField();
//bfield1.HeaderText = "SI_ID";
//bfield1.DataField = "si_id";
//failedSchedulesGridView.Columns.Add(bfield1);
//BoundField bfield2 = new BoundField();
//bfield2.HeaderText = "SI_NAME";
//bfield2.DataField = "si_name";
//failedSchedulesGridView.Columns.Add(bfield2);
//BoundField bfield3 = new BoundField();
//bfield3.HeaderText = "SI_UPDATE_TS";
//bfield3.DataField = "si_update_ts";
//failedSchedulesGridView.Columns.Add(bfield3);
}
failedSchedulesGridView.DataSource = dt;
failedSchedulesGridView.DataBind();
}
OnRowDataBound
protected void OnRowDataBound(Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType != DataControlRowType.Header)
{
cbx++;
CheckBox cb = new CheckBox();
cb.ID = "cb" + cbx;
e.Row.Cells[0].Controls.Add(cb);
}
}
The Code fails when I try to access the Checkboxes after the 'Reschedule' button is pressed because the checbox is not found :-
protected void ReschedulePB2_Click(object sender, EventArgs e)
{
int i = 0;
foreach (GridViewRow row in failedSchedulesGridView.Rows)
{
i++;
string cbName = "cb" + i;
CheckBox cb = (CheckBox)row.Cells[0].FindControl(cbName);
if (cb.Checked)