jeudi 27 octobre 2016

checkboxes postback on gridview

I put a checkbox in gridview inside of itemtemplate with autopostback=true. My gridview was binded with sp. Every click on the checkbox save the id of my row in a arraylist in the event Checkbox_CheckedChanged. My problem this event id fired for every row in my gridview. Only I need is that event occur only once. this is my code.

<asp:UpdatePanel ID="upGrillaPadre" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
        <asp:GridView ID="grvResultado" runat="server" CssClass="table table-condensed table-datatable grilla" GridLines="None" DataKeyNames="Id" AutoGenerateColumns="False" OnRowCommand="grvResultado_RowCommand1" data-pdf="true" data-imprimir="true">
            <Columns>
                <asp:TemplateField>
                   <HeaderTemplate>
                       <input id="chkHeader" type="checkbox" runat="server" onclick="grilla_checked_all(this);" />
                   </HeaderTemplate>
                   <ItemTemplate>
                       <asp:CheckBox ID="chkRows" runat="server" onclick="grilla_checked(this);" OnCheckedChanged="chkRows_CheckedChanged" AutoPostBack="true" />
                   </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="Id" HeaderText="Secuestro Nº"></asp:BoundField>
                <asp:BoundField DataField="NroSumario" HeaderText="Sumario Nº"></asp:BoundField>
                <asp:BoundField DataField="ApellidoNombre" HeaderText="Apellido y Nombre"></asp:BoundField>
                <asp:BoundField DataField="NroCaja" HeaderText="Nº Caja"></asp:BoundField>
                <asp:BoundField DataField="Fechaingreso" HeaderText="Fecha ingreso"></asp:BoundField>
                <asp:BoundField DataField="FechaSecuestro" HeaderText="Fecha Secuestro"></asp:BoundField>
                <asp:BoundField DataField="Autos" HeaderText="Autos"></asp:BoundField>
                <asp:BoundField DataField="Cooperacion" HeaderText="Nº Coop."></asp:BoundField>
                <asp:BoundField DataField="ElementosSecuestrados" HeaderText="Elem. Secuestrados"></asp:BoundField>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:LinkButton ID="grvEliminar" runat="server" CssClass="btn btn-info btn-sm fa fa-eraser" CommandName="eliminar" CommandArgument='<%#Container.DataItemIndex %>'></asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
             </Columns>
        </asp:GridView>
        </div>
    </ContentTemplate>
 </asp:UpdatePanel>

//CodeBehind

 protected void chkRows_CheckedChanged(object sender, EventArgs e)
    {
        ArrayList arr = new ArrayList();

        if (Session["seleccionados"] == null)
        {

            Session.Add("selecconados", arr);

        }
        else
        {
            if (Session["seleccionados"] != "")
            { 
            arr = (ArrayList)Session["seleccionados"];
                 }
        }

        CheckBox cbk = (CheckBox)sender;
        GridViewRow row = (GridViewRow)((CheckBox)sender).Parent.Parent;
        bool but = cbk.Checked ? true : false;

        if (but)
        {

            arr.Add(grvResultado.Rows[row.RowIndex].Cells[1].Text);

        }
        else
        {
            int valor = 0;
            for (int i = 0; i < arr.Count; i++)
            {
                valor = i;
                if(arr[i].ToString()  == grvResultado.Rows[row.RowIndex].Cells[1].Text)
            {
                arr.RemoveAt(valor);
            }


            }

        }

        Session["seleccionados"] = arr;

        upGrillaPadre.Update();



    }




Aucun commentaire:

Enregistrer un commentaire