jeudi 8 avril 2021

aspnet 2.0 GridView Get value CheckBox create dynamically

I created an asp page with a GridView with a text column and n columns with dynamic checkboxes.

 <asp:GridView ID="gridAssetto" runat="server" Width="100%" GridLines="Vertical" SkinID="grdRegular" 
                            EnableModelValidation="True" AllowPaging="false" 
                            OnRowCommand="gridAssetto_RowCommand" OnRowDataBound="gridAssetto_RowDataBound" 
                            OnDataBound="gridAssetto_DataBound">                    
        </asp:GridView>

In .cs

protected void gridAssetto_RowDataBound(object sender, GridViewRowEventArgs e)
        {
        if (e.Row.RowType == DataControlRowType.DataRow)
                    {
                        for (int i = 1; i < numCol; i++)
                        {
        
                            CheckBox cb = new CheckBox();
                            cb.ID = "cb_Aero" + e.Row.Cells[0].Text + "_Ane" + 
                            gridAssetto.HeaderRow.Cells[i].Text;
                            
                            cb.EnableViewState = true;
        
                            MutuallyExclusiveCheckBoxExtender cbExt = new MutuallyExclusiveCheckBoxExtender();
                            cbExt.ID = "cbExt" + i;
                            cbExt.TargetControlID = cb.ID;
                            cbExt.EnableViewState = true;
                            cbExt.Key = e.Row.RowIndex.ToString();
                            e.Row.Cells[i].Controls.Add(cb);
                            e.Row.Cells[i].Controls.Add(cbExt);
        } 

I take the data from the database and set the checkboxes correctly with the method

foreach (GridViewRow row in gridAssetto.Rows)
        {
             CheckBox chk = (CheckBox)row.FindControl(cbID);
        
             if (chk != null)
               chk.Checked = true;
        }

when instead I want to store the information on the database I have to read the checkboxes modified by the user, at click button the FindControl method does not work, as if the checkboxes in the grid were no longer there

foreach (GridViewRow row in gridAssetto.Rows)
                    {
                        for (int xx = 1; xx < row.Cells.Count; xx++)
                        {
                            if (row.RowType == DataControlRowType.DataRow)
                            {
                                string idcell = row.Cells[xx].ID;
    
                                CheckBox chk = (CheckBox)row.Cells[xx].FindControl(cbID);
    
                                if (chk != null)
                                {
                                    if (chk.Checked)
                                    {
                                        my code ...
                                    }
                                }
                            }
                        }
                    }

the chk is always null! In the page html, before clic button, I have this control:

<input id="ctl00_Contenitore_TabGestione_TabAssetto_gridAssetto_ctl05_cb_AeroG001769_Ane1128" type="checkbox" name="ctl00$Contenitore$TabGestione$TabAssetto$gridAssetto$ctl05$cb_AeroG001769_Ane1128">

but when i read the check in debug code, i have the value

row.Cells[xx].ID is null and

row.Cells[xx].UniqueID is

"ctl00$Contenitore$TabGestione$TabAssetto$gridAssetto$ctl03$ctl00"

Thanks



Aucun commentaire:

Enregistrer un commentaire