0
votes

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)
1
Why are you creating columns in c# you can do in aspx page right ? - Krishna
I'm not proficient in ASP, I've recently cross-trained from COBOL to C# and my ASP skills are limited to what Visual Studio generates for me. - S Close

1 Answers

0
votes

try this

Write this in your aspx page

<asp:GridView ID="failedSchedulesGridView" runat="server" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound"  Width="850px" onrowcommand="GridView1_RowCommand">
        <Columns>
            <asp:TemplateField >
                <HeaderTemplate>
                    <asp:CheckBox ID="cbHeader" runat="server" />
                </HeaderTemplate>
                <ItemTemplate>
                    <asp:CheckBox ID="cbItem" runat="server" />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="si_id" HeaderText="SI_ID" SortExpression="si_id" />
            <asp:BoundField DataField="si_name" HeaderText="SI_NAME" SortExpression="si_name" />
            <asp:BoundField DataField="si_update_ts" HeaderText="SI_UPDATE_TS" SortExpression="si_update_ts" />
        </Columns>
</asp:GridView>

your page load should look like this

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        failedSchedulesGridView.DataSource = dt;
        failedSchedulesGridView.DataBind();
    }
}

your reschedule click would be

protected void ReschedulePB2_Click(object sender, EventArgs e)
{

    foreach (GridViewRow row in failedSchedulesGridView.Rows)
    {
        CheckBox cb = (CheckBox)row.Cells[0].FindControl("cbItem");

        if (cb.Checked)