vendredi 3 juillet 2020

How to save asp checkbox checked state in a Gridview control before and after postback/Bind Gridview

Hi am quite new to c# Webforms and i've been struggling with this problem in my project for almost 2weeks now. I have 2 Gridviews populated from an Active Directory and an SQl Table.

The Logic behind my WebApp is do edit a User given/Approved Directory, ie Add a new one, Remove an old one or confirm an old directory.

Gridview1 contains 5 BoundFields, 3 checkboxes in separate Itemfields.

Gridview1

The information in Gridview one is basically a User approved/given Email Directories from LDAP// The checkboxes are there to confirm, Remove or Add Email directories. In my script i set the Bestätigen/Confirm Checkedbox checked = true, because the user already has these directories.

These checkboxes has string values which will be stored in an Sql table and later be sumbitted to the IT departement to work with.

Gridview 2 contains Email directories that has been assigned to/Given to/approved for a user yet. it a has only one checkbox in Itemtemplate in my scripts and an event that adds a row to Gridview1 when checked.

Gridview2

Gridview1 -> added row from Gridview2 to Gridview1 Gridview1 Added row from Gridview2

Now what i've been struggling with is the checkbox manipulations in Gridview1. I've been trying to use the string in the Actioned_Checked column to choose which checkbox should be checked after every postback or whenever i rebind the Gridview1. Somehow am failing miserably in achieving that.

So the Plan is to add new Email Directories from Gridview2 and the String in Action_checked is NeuHinzufuegen/Addnew and the checkbox should be check right after the adding the new row from Gridview 2.

I also have an Event on the checkbox "Entfernen/Remove/Delete" which basically removes/deletes only newly added rows from Gridview2, if a row was added from Gridview2, the check box should only be checked, which only stores a string value "Remove/Delete" in my sql table later for the IT to unassign the user this Directory.

After every server communication and after Binding the Gridview to the new changes, am trying to figure out to keep old checkstates of the the checkboxes the the new changes didn't affect. i tried using the strings in the Action_checked column to do that but working like i want because these string are static. Or maybe i change the string in the column according to the checkbox checked.

i tried that as well but wasnt successful. i really hope am making sense here. Below is my code to give you a fair idea of what am trying to do.

// Gridview1

 <asp:GridView ID="gv_ZugeteilteEmailverzeiler" runat="server" AllowPaging ="true" PageSize="15" CssClass="table_class"
                        AutoGenerateColumns="false" OnPageIndexChanging="gv_ZugeteilteEmailverzeiler_PageIndexChanging"
                        BorderColor="#DEBA84" BackColor="Silver" HeaderStyle-Height="40px"     
                        HorizontalAlign="Center" CellPadding="2">
                        <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_EmailVerzeilerHinzufuegen" runat="server" AutoPostBack="true" Checked="false" ItemStyle-HorizontalAlign="Center" CssClass="checkboxClass" onclick="CheckBoxCheck(this);" />
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Entfernen" ItemStyle-Width="200px" ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center" >
                                <ItemTemplate>
                                    <asp:CheckBox ID="cb_EmailVerzeilerEntfernen" runat="server" AutoPostBack="true" ItemStyle-HorizontalAlign="Center" CssClass="checkboxClass" onclick="CheckBoxCheck(this);" onchange="return javascript:CheckBoxCheck(this);" OnCheckedChanged="cb_EmailVerzeilerEntfernen_CheckedChanged"/>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Bestätigen" ItemStyle-Width="200px" ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center">
                                <ItemTemplate>
                                    <asp:CheckBox ID="cb_EmailVerzeilerBestaetigen" runat="server" AutoPostBack="true" ItemStyle-HorizontalAlign="Center" class="checkboxClass" onclick="CheckBoxCheck(this);"/>
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                        <HeaderStyle ForeColor="Black" Font-Bold="True" BackColor="#CCCC00"></HeaderStyle>
                    </asp:GridView>

//Gridview2

<asp:GridView ID="gv_MoeglicheEmailverzeiler" runat="server" AllowPaging ="true" PageSize="15" ClientIDMode="Static" 
                        AutoGenerateColumns="false" OnPageIndexChanging="gv_MoeglicheEmailverzeiler_PageIndexChanging" PagerStyle-CssClass="foo"
                        BorderColor="#DEBA84" BackColor="Silver" HeaderStyle-Height="40px" CssClass="tablesorter"  OnPreRender="gv_MoeglicheEmailverzeiler_PreRender"   
                        HorizontalAlign="Center" CellPadding="3">
                        <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_CheckOneMoglicheVerzeilerRow" 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>

//CheckBox_checkchanged Event to AddRow From Gridview2 to Gridview1

protected void CheckBox_CheckChanged(object sender, EventArgs e)
    {
        GetData();
        SetData();
        BindZugeteilteGridviewForGetDataMethod();
    }

// GetData Method

private void GetData()
    {
        DataTable dt;
        if (ViewState["SelectedRecords"] != null) // check if Datatable contains Selected Record
            dt = (DataTable)ViewState["SelectedRecords"];
        else
            dt = CreateDataTable();         

        for (int i = 0; i < gv_MoeglicheEmailverzeiler.Rows.Count; i++) // Loop through Gridview Mogliche Emailverteiler to get checkbox and column values
        {
            CheckBox chk = (CheckBox)gv_MoeglicheEmailverzeiler.Rows[i].Cells[6].FindControl("cb_CheckOneMoglicheVerzeilerRow"); // Checkbox Object
            
            if (gv_ZugeteilteEmailverzeiler != null && chk.Checked) // Check if Gridview Zugteilte Emailverteiler is not null and cb_moglicheverteiler is checked
            {

                for (int j = 0; j < gv_ZugeteilteEmailverzeiler.Rows.Count; j++) // Loop through Gridview Zugeteilte Emailverteiler
                {
                    string actionChecked = gv_ZugeteilteEmailverzeiler.Rows[j].Cells[6].Text; // get value of column "Action_checked"
                    
                    if ((chk.Checked && actionChecked == "Bestaetigen")|| (chk.Checked && actionChecked == "Bestaetigt") || (chk.Checked && actionChecked == "TRUE") || (chk.Checked && actionChecked == "FALSE") || (chk.Checked && actionChecked == "Entfernen"))
                    {
                        dt = AddRow(gv_MoeglicheEmailverzeiler.Rows[i], dt);

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


        }   

// SetData Method

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

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

                }
            }
        }         
    }

//Bind Gridview1 After Adding Row

private void BindZugeteilteGridviewForGetDataMethod()
    {
        DataTable dt = (DataTable)ViewState["SelectedRecords"];
        gv_ZugeteilteEmailverzeiler.DataSource = dt;
        gv_ZugeteilteEmailverzeiler.DataBind();
        CheckHinfeugenMoglichGridviewForDataMethod();

    }

// I tried to Write a a private Method to for the checkbox checkstates and how to recheck them after binding the gridview after every server response/Gridview Bind. In this Method i was trying to get all the column1/gruppenname that will be selected from Gridview2 which serves as unique ID in this case, i wanted to store them in a string List. After that i wanted to run a loop through the Gridview1 column1/gruppenname and compare the column vvalues to the selected string List gotten from Gridview1 and if there is a match then the checkbox Hinzufügen on the particular Row should be checked. But am failing at that too miserably.

// Method to manipulate checkboxes in Gridview1, Save their checkedStates and before and afte every Databind.

{
        List<string> checkedGruppennameListAusMoeglicheEmailvereiler = new List<string>();
        string mVCheckedRowGruppename = "";

        for (int i = 0; i < gv_MoeglicheEmailverzeiler.Rows.Count; i++)
        {
            CheckBox chk = (CheckBox)gv_MoeglicheEmailverzeiler.Rows[i].Cells[6].FindControl("cb_CheckOneMoglicheVerzeilerRow"); // Checkbox Object
            mVCheckedRowGruppename = gv_MoeglicheEmailverzeiler.Rows[i].Cells[1].Text;
            if (chk.Checked)
            {
                checkedGruppennameListAusMoeglicheEmailvereiler.Add(mVCheckedRowGruppename);
                listbox_adGruppenname.Items.Add(mVCheckedRowGruppename.ToString());
            }
 
        }          
       
    }



Aucun commentaire:

Enregistrer un commentaire