GridView e CheckBox

Nell’ applicazione che ho appena terminato, ho dovuto rispolverare le nozioni base sulle GridView e su come inserire colonne "custom". In questo piccolo esempio, vediamo come inserire, per ogni riga, un controllo CheckBox.

<asp:GridView ID="GridView1" />
<Columns>
<asp:TemplateField>                
    <ItemTemplate>
        <asp:CheckBox ID="chkSeleziona" runat="server" Text=" " Width="18px" />
    </ItemTemplate>
</asp:TemplateField>

<asp:BoundField DataField="valore" HeaderText="IdDocumento" />
</Columns>
</asp:GridView>

Per recuperare tutti i "valori" selezionati:

protected void bntRecuperaId_Click(object sender, EventArgs e)
{
    StringBuilder elencoValori = new StringBuilder();

    for (int i = 0; i < GridView1.Rows.Count; i++)
    {
        CheckBox objCheckBox = (CheckBox)GridView1.Rows[i].FindControl("chkSeleziona");
        if (objCheckBox .Checked)
        {
            elencoValori .AppendFormat("{0},", GridView1.Rows[i].Cells[2].Text);
        }
    }
    Response.Write(elencoIdDoc.ToString());
}

These icons link to social bookmarking sites where readers can share and discover new web pages.
  • del.icio.us
  • digg
  • YahooMyWeb

5 Responses

  1. Tom Says:

    Have I got a gridview with a pagesize and when I going to the next page I lost the check and the value ?What Can I resolve it?

    Thanks

    Tom

  2. Zaragon Says:

    Hi Tom, you can see this exsample: http://forums.asp.net/p/1256599/2342141.aspx

    regards.

  3. Tom Says:

    Thanks I have seen this code but I don’t uderstood How I put this your code with this
    http://forums.asp.net/p/1256599/2342141.aspx
    Because in your code I get the values but the other code is the trace for checkbox.
    Please Could you have some example of full code ?
    Sorry for my english

    Thanks Tom

  4. Tom Says:

    Sorry I have understood

    Thanks

  5. Zaragon Says:

    i’m sorry, i haven’t full code

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.