lundi 7 septembre 2020

How to save ASP checkbox state in Checkstate and retrieve value after postba

Am developing an ASP Webform App and having some challenges with checkbox Viewstate and Postback. My App has 2 Gridviews. Gridview has about 8 columns and 3 of those columns has 3 TemplateFields with each TemplateField having its own ASP checkbox with a different IDs. Gridview displays data from an Active Directory which has already been assigned to a user will be updated, deleted, or unchanged. i want to use the checkboxes checked states or values for the updates.

Please see Markup for Gridview A

<asp:GridView ID="gv_ZugeteilteEmailverteiler" runat="server" AllowPaging ="true" PageSize="15" CssClass="table table-striped table-bordered" AutoGenerateColumns="false" Width="100%" BorderColor="#DEBA84" BackColor="Silver" HeaderStyle-Height="40px" OnPreRender="gv_ZugeteilteEmailverteiler_PreRender"     
    HorizontalAlign="Center" CellPadding="0">
    <Columns>
    <asp:TemplateField HeaderText="Reihe" ItemStyle-Width="200px" ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center">
    <ItemTemplate>
    <%# Container.DataItemIndex + 1 %>   
    </ItemTemplate>
    </asp:TemplateField>
    <asp:BoundField DataField="gruppenname" HeaderText="Gruppenname" NullDisplayText="n/a" ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center" ItemStyle-Width="250px"/>
    <asp:BoundField DataField="standort" HeaderText="Standort" NullDisplayText="n/a" ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center" ItemStyle-Width="250px"/>
    <asp:BoundField DataField="beschreibung" HeaderText="Beschreibung" NullDisplayText="n/a" ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center" ItemStyle-Width="250px"/>             
    <asp:BoundField DataField="genehmigt" HeaderText="Genehmigt" NullDisplayText="n/a" ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center" ItemStyle-Width="400px"/>
    <asp:BoundField DataField="zielobjekt" HeaderText="ZielObjekt" NullDisplayText="n/a" ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center" ItemStyle-Width="250px"/>
    <asp:BoundField DataField="ACTION_CHECKED" HeaderText="Action_Checked" NullDisplayText="n/a" ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center" ItemStyle-Width="250px" />
    <asp:TemplateField HeaderText="Neu-Hinzufügen" ItemStyle-Width="200px" ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center">                                                          
    <ItemTemplate>
    <asp:CheckBox ID="cb_EmailverteilerHinzufuegen" runat="server" AutoPostBack="true" Checked="false" ItemStyle-HorizontalAlign="Center" CssClass="checkboxClass" onclick="CheckBoxCheck(this);" onchange="return javascript:CheckBoxCheck(this);" OnCheckedChanged="cb_EmailverteilerHinzufuegen_CheckedChanged"/>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Entfernen" ItemStyle-Width="200px" ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center" >
    <ItemTemplate>
    <asp:CheckBox ID="cb_EmailverteilerEntfernen" runat="server" AutoPostBack="true" ItemStyle-HorizontalAlign="Center" CssClass="checkboxClass" onclick="CheckBoxCheck(this);" onchange="return javascript:CheckBoxCheck(this);" OnCheckedChanged="cb_EmailverteilerEntfernen_CheckedChanged"/>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Bestätigen" ItemStyle-Width="200px" ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center">
    <ItemTemplate>
    <asp:CheckBox ID="cb_EmailverteilerBestaetigen" runat="server" AutoPostBack="true" ItemStyle-HorizontalAlign="Center" class="checkboxClass" onclick="CheckBoxCheck(this);" onchange="return javascript:CheckBoxCheck(this);" OnCheckedChanged="cb_EmailverteilerBestaetigen_CheckedChanged"/>
    </ItemTemplate>
    </asp:TemplateField>                                       
    </Columns>
    <HeaderStyle ForeColor="Black" Font-Bold="True" BackColor="#CCCC00"></HeaderStyle>
    </asp:GridView>

The Gridview B also has about 6 columns and one checkbox in a Template field. The Data in Gridview B are Directories from the AD that can be added to the user's list of already given directories shown in Gridview A. Hence when the checkbox in Gridview B is clicked, the entire row selected or checked will be added to Gridview A.

Please see the Markup for Gridview B

<asp:GridView ID="gv_MoeglicheEmailverteiler" runat="server" ClientIDMode="Static"  
AutoGenerateColumns="false" BorderColor="#DEBA84" BackColor="Silver" HeaderStyle-Height="40px" OnPreRender="gv_MoeglicheEmailverteiler_PreRender"   
HorizontalAlign="Center" CellPadding="3" CssClass="tablesorter">
<Columns>                
<asp:TemplateField HeaderText="Reihe" ItemStyle-Width="200px" ItemStyle-HorizontalAlign="Center">   
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>   
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="gruppenname" HeaderText="Gruppenname" NullDisplayText="n/a" ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center" ItemStyle-Width="250px"/>                           
<asp:BoundField DataField="standort" HeaderText="Standort" NullDisplayText="n/a" ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center" ItemStyle-Width="250px"/>
<asp:BoundField DataField="beschreibung" HeaderText="Beschreibung" NullDisplayText="n/a" ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center" ItemStyle-Width="250px"/>             
<asp:BoundField DataField="genehmigt" HeaderText="Genehmigt" NullDisplayText="n/a" ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center" ItemStyle-Width="250px"/>
<asp:BoundField DataField="zielobjekt" HeaderText="ZielObjekt" NullDisplayText="n/a" ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center" ItemStyle-Width="250px"/>
<asp:BoundField DataField="ACTION_CHECKED" HeaderText="Action_Checked" NullDisplayText="n/a" ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center" ItemStyle-Width="250px" />
<asp:TemplateField ItemStyle-Width="300px"  HeaderText="Action" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:CheckBox ID="cb_CheckOneMoglicheverteilerRow" runat="server" ItemStyle-HorizontalAlign="Center" class="checkboxClass" AutoPostBack="true" onclick = "Check_Click(this);" OnCheckedChanged="CheckBox_CheckChanged"/>
</ItemTemplate>        
</asp:TemplateField>
</Columns>
<HeaderStyle ForeColor="Black" Font-Bold="True" BackColor="#CCCC00"></HeaderStyle>                       
</asp:GridView>

PROBLEM: Each of the checkboxes in both Gridviews fires events. Example the Checkbox in Gridview B's Event is to add the checked Row to Gridview A. I managed to save the Gridviews ViewState and retrieve them after every Postback or event and Bind them succesfully.

My problem is, the checkboxes in Gridview A, i cannot or dont know how i could save their checkedstates in a ViewState and restore them after Postback or after an Event or after page refreshes. They all go unchecked after page refreshes or after a Postback.

Here are the methods to add a row from Gridview B to Gridview A and Remove Row from Gridview A

// Create Datatable and save in Viewstate, I was thinking i could add the checkboxes columns to the datatable here as well, but i dont know how to get their values, because the Gridview columns values i get from a database boundfield.

    private DataTable CreateDataTable()
    {
      DataTable dt = new DataTable();
      if (ViewState["SelectedRecords"] != null)
      {
         dt = (DataTable)ViewState["SelectedRecords"];
      }
      else
      {
       dt.Columns.Add("gruppenname");
       dt.Columns.Add("standort");
       dt.Columns.Add("beschreibung");
       dt.Columns.Add("genehmigt");
       dt.Columns.Add("zielobjekt");
       dt.Columns.Add("Action_Checked");
       dt.AcceptChanges();
      }
        return dt;
   }

// Method to add Row to from Gridview A to Gridview B

private DataTable AddRow(GridViewRow gvRow, DataTable dt)
  {
            DataRow[] dr = dt.Select("gruppenname = '" + gvRow.Cells[1].Text + "'");

            if (dr.Length <= 0)
            {

                dt.Rows.Add();
                dt.Rows[dt.Rows.Count - 1]["gruppenname"] = gvRow.Cells[1].Text;
                dt.Rows[dt.Rows.Count - 1]["standort"] = gvRow.Cells[2].Text;
                dt.Rows[dt.Rows.Count - 1]["beschreibung"] = gvRow.Cells[3].Text;
                dt.Rows[dt.Rows.Count - 1]["genehmigt"] = gvRow.Cells[4].Text;
                dt.Rows[dt.Rows.Count - 1]["zielobjekt"] = gvRow.Cells[5].Text;
                dt.Rows[dt.Rows.Count - 1]["Action_Checked"] = gvRow.Cells[6].Text;
                dt.AcceptChanges();
            }

            return dt;
  }

// Method to remove Row from Gridview A

private DataTable RemoveRow(GridViewRow gvRow, DataTable dt)
 {
            DataRow[] dr = dt.Select("gruppenname = '" + gvRow.Cells[1].Text + "'");
            if (dr.Length > 0)
            {

                dt.Rows.Remove(dr[0]);
                dt.AcceptChanges();
            }
            return dt;

  }

// Now this is how i call them // Method to Get Data from checked Row in Gridview Moglicheverteiler to add to Gridview Zugeteilte Emailverteiler

    private void GetData()
        {
            DataTable dt;
            if (ViewState["SelectedRecords"] != null) 
                dt = (DataTable)ViewState["SelectedRecords"];
            else
                dt = CreateDataTable();         

            for (int i = 0; i < gv_MoeglicheEmailverteiler.Rows.Count; i++)
            {
              CheckBox chk = (CheckBox)gv_MoeglicheEmailverteiler.Rows[i].Cells[6].FindControl("cb_CheckOneMoglicheverteilerRow");  string test = gv_MoeglicheEmailverteiler.Rows[i].Cells[1].Text;

if (gv_ZugeteilteEmailverteiler != null && chk.Checked) 
{
for (int j = 0; j < gv_ZugeteilteEmailverteiler.Rows.Count; j++) 
                    {
                        string actionChecked = gv_ZugeteilteEmailverteiler.Rows[j].Cells[6].Text;                     
                        if ((chk.Checked && actionChecked == "Bestaetigen")|| (chk.Checked && actionChecked == "Bestaetigt") || (chk.Checked && actionChecked == "TRUE") || (chk.Checked && actionChecked == "FALSE") || (chk.Checked && actionChecked == "Entfernen"))
                        {
                            dt = AddRow(gv_MoeglicheEmailverteiler.Rows[i], dt);

                            dt = AddRow(gv_ZugeteilteEmailverteiler.Rows[j], dt);
                            
                        }
                        else if (chk.Checked)
                        {
                            dt = AddRow(gv_MoeglicheEmailverteiler.Rows[i], dt);                          
                        }                      
                    }
                }
            }

            ViewState["SelectedRecords"] = dt;               
        }

        
        private void SetData()
        {
         
            if (ViewState["SelectedRecords"] != null)
            {
                DataTable dt = (DataTable)ViewState["SelectedRecords"];
                for (int i = 0; i < gv_MoeglicheEmailverteiler.Rows.Count; i++)
                {
                    CheckBox chk = (CheckBox)gv_MoeglicheEmailverteiler.Rows[i].Cells[6].FindControl("cb_CheckOneMoglicheverteilerRow");
                    if (chk != null)
                    {

                        DataRow[] dr = dt.Select("gruppenname = '" + gv_MoeglicheEmailverteiler.Rows[i].Cells[1].Text + "'");
                        chk.Checked = dr.Length > 0;

                    }
                }
            }         
        }

So in short am trying to figure out how i can save the checkboxes values into the viewstate just as i did with the Gridview Columns. Any ideas out there will be really appreciated. Ibeen stocked on the problem for months.

    // Bind ZugeteiltGridview For GetData Method
    private void BindZugeteilteGridviewForGetDataMethod()
    {
        DataTable dt = (DataTable)ViewState["SelectedRecords"];
        gv_ZugeteilteEmailverteiler.DataSource = dt;
        gv_ZugeteilteEmailverteiler.DataBind();
        //CheckAddedRowFromMoglicheEmailverteiler();
    }



Aucun commentaire:

Enregistrer un commentaire