protected void btnCountSelectedRows_Click(object sender, EventArgs e)
{
if (GridView1.Rows.Count > 0)
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox cbx = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
if (cbx != null)
{
if (cbx.Checked)
{
string firstColumnValue = GridView1.Rows[i].Cells[0].Text;
Response.Write(firstColumnValue);
}
}
}
}
}
Try This one using JavaScript:
Create a public Static method in YourForm.aspx.cs
public static String CreateCheckBox(Int32 index)
{
return "<input type="\"checkbox\"" id="\"Chk_"" index="" hold=" /> " önclick="\"checkSelection()\"" />";
}
Create a item template in gridView... like this
<TemplateField HeaderText="">
<ItemTemplate HorizontalAlign="Left" />
<ItemTemplate>
<%#YourNamespace.YourForm.CreateCheckBox(Container.DisplayIndex)%>
</ItemTemplate>
</TemplateField>
Add the javascript function (On click) of the checkboxes
function checkSelection() {
var nMarked = 0;
for (var i = 0; i < document.forms[0].elements.length; i++) {
if (document.forms[0].elements[i].checked &&
document.forms[0].elements[i].name.indexOf('Chk_') > -1) {
nMarked = nMarked + 1;
}
}
if (nMarked == 0) {
alert("no element has been selected");
return;
}
else
{
alert("You have selected " + nMarked + " checkboxes");
}
}
tell me what hapened