Checkbox in gridview

sabato 13 luglio 2013 - 11.02

antonio1981 Profilo | Newbie

Ciao a tutti, ho un problema con delle checkbox in una gridview il codice è il seguente in vb.net:


<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-

transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" OnRowDataBound="GridView1_RowDataBound" AutoGenerateColumns="false"

runat="server">
<Columns >
<asp:TemplateField >
<ItemTemplate >
<asp:CheckBox ID="chk" runat ="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField ="IDARTICOLO" HeaderText ="IDARTICOLO" />
<asp:BoundField DataField="NOME" HeaderText ="NOME" />
</Columns>
</asp:GridView>
<br />
<br />
<asp:Button ID="Button2" runat="server" Text="Assegna" />
</div>
</form>
</body>
</html>



Devo riuscire al click del botto "Button2" a vedere se le checkbox sono state cliccate o no.
Ho provato così, ma visualizza sempre le checkbox nello stato false anche quando sono cliccate:

Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click

For Each gvr As GridViewRow In GridView1.Rows
If (CType(gvr.FindControl("chk"), CheckBox)).Checked = True Then
Response.Write("Trovato")
End If
Next gvr

End Sub



Ho provato anche con il Gridview _RowDataBound:


Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
For Each gvr As GridViewRow In GridView1.Rows
If (CType(gvr.FindControl("chk"), CheckBox)).Checked = True Then
Response.Write("Trovato")
End If
Next gvr
End Sub


Come posso fare a trovare le checkbox quando sono cliccate?
Grazie in anticipo a chi mi risponderà.

ridaria Profilo | Expert

Ok, seguimi:

iNNANZITUTTO INSERISCI NELLA GRIGLIA IL CHECK BOX come in un template field:

<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" Checked="True" />
</ItemTemplate>
</asp:TemplateField>


Il click lo devi fare da un pulsante presente nella griglia esempio un buttonField:

<asp:ButtonField ButtonType="Button" CommandName="Modifica" Visible="true" />


A questo punto nell'evento GridView1_RowCommand1 vai a cercare il chek box che sta nella riga in cui c'è il button che hai cliccato:

Protected Sub GridView1_RowCommand1(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand




Dim index As Integer = Convert.ToInt32(e.CommandArgument)
Dim row As GridViewRow = GridView1.Rows(index)

idrecord = CInt(row.Cells(0).Text)

Dim clsDB As New ClasseDB_MySQL


Select Case e.CommandName
Case "Elimina"

Dim check As CheckBox = row.FindControl("CheckBox1")
Response.Write(check.Checked)

.............

Fammi sapere

Ciao

Riccardo D'Aria

antonio1981 Profilo | Newbie

Ti ringrazio....ho provato tutto ok!

ridaria Profilo | Expert

;-)

OK se tutto ok, chiudi il post cliccando su accetta :-)

GRAZIE e ciao
Riccardo D'Aria
Partecipa anche tu! Registrati!
Hai bisogno di aiuto ?
Perchè non ti registri subito?

Dopo esserti registrato potrai chiedere
aiuto sul nostro Forum oppure aiutare gli altri

Consulta le Stanze disponibili.

Registrati ora !
Copyright © dotNetHell.it 2002-2025
Running on Windows Server 2008 R2 Standard, SQL Server 2012 & ASP.NET 3.5