Select some gridview rows to display in ASP.NET webform2

0

Good morning,

I have a gridview to which I insert data with a datatable which works perfectly, it complicates me when placing a row checkbox so that the user can select the row he wants to perform his management. This is the HTML code:

<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False"> <Columns> <asp:TemplateField> <HeaderTemplate> <asp:CheckBox ItemStyle-Width="150px" ID="cbSelect" runat="server" AutoPostBack="True" OnCheckedChanged="cbSelect_CheckedChanged" /> </HeaderTemplate> <ItemTemplate> <asp:CheckBox ItemStyle-Width="150px" ID="cbSelected" runat="server" AutoPostBack="True" OnCheckedChanged="cbSelected_CheckedChanged" /> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Poliza"> <ItemTemplate> <asp:Label ID="Poliza" runat="server" Text='<%# Bind("POLIZA") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:BoundField ItemStyle-Width="150px" DataField="RAMO" HeaderText="Ramo" /> <asp:BoundField ItemStyle-Width="150px" DataField="CERTIF" HeaderText="Certificado" /> <asp:BoundField ItemStyle-Width="150px" DataField="NOMRMO" HeaderText="Ramo" /> <asp:BoundField ItemStyle-Width="150px" DataField="CIA" HeaderText="N. Comp" /> <asp:BoundField ItemStyle-Width="150px" DataField="NOMCIA" HeaderText="Compañia" /> <asp:BoundField ItemStyle-Width="150px" DataField="RIF" HeaderText="Ident" /> <asp:BoundField ItemStyle-Width="150px" DataField="CCT" HeaderText="Num" /> </Columns> </asp:GridView>

and in the backend I have the code that fills the gridview and to perform the validations if a checkbox has been selected and if it is empty it works perfectly

My complication is in the button to transfer the selected rows to another webform because normally I could pass a "ButtonField" property with the "OnSelectedIndexChanged" property but how could I do this with the check box?

I have this in the click but I do not know how to advance:

protected void Button2_Click(object sender, EventArgs e)
     {
         List<string> test = new List<string>();

         foreach (GridViewRow gridViewRow in GridView2.Rows)
         {
             if (((CheckBox)gridViewRow.FindControl("cbSelected")).Checked)
             {
                 string ejecutivoId = ((Label)gridViewRow.FindControl("Poliza")).Text;
                 test.Add(ejecutivoId);
             }
         }
         if (test.Count > 0)
         {}

Thanks

    
asked by Hans 25.01.2017 в 21:56
source

2 answers

2

When you already have the List in the other form:

List<string> polizas = (List<string>)Session["Polizas"];

if (polizas != null && polizas.Any())
{
    // Si quieres un valor especifico del List.
    string variable1 = polizas[0];
    string variable2 = polizas[1];
    // etc...

    // Si quieres recorrer el List y obtener cada valor.
    foreach(string poliza in polizas)
    {
        UnTextBox.Text += poliza + ", ";
    }

    // Si quieres asignarlo a otro grid. Nota: así como tienes el List<string> el otro grid solo debe tener una columna.
    MiOtroGrid.DataSource= polizas;
    MiOtroGrid.DataBind();
}
    
answered by 26.01.2017 / 15:24
source
2

You could use a variable Session and store all the selected policies, already in the other form you recover this information.

    if (test.Count > 0)
    {
      Session["Polizas"] = test;
      Response.Redirect("Formulario2.aspx");
    }

On the other form in the event Page_Load you get it back .

List<string> polizas = Session["Polizas"] as List<string>;
    
answered by 26.01.2017 в 16:16