Posted by Unknown on 12:47 AM
Labels:

To add a checkall option inside a gridview:

StringBuilder str = new StringBuilder();

// Select the checkboxes from the GridView control

for (int i = 0; i < GridView1.Rows.Count; i++)
{
GridViewRow row = GridView1.Rows[i];
bool isChecked = ((CheckBox) row.FindControl("chkSelect")).Checked;

if (isChecked)
{
// Column 2 is the name column

str.Append(GridView1.Rows[i].Cells[2].Text);
}
}


in the .aspx source(A checkbox is added in the template field)
Also add a checkbox in the header template of the same cell.When thid checkbox is selected all others are selected.

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="175px">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<input type = checkbox id = "chkid" name = SelectAll onclick = selectall<); />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox2" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>


Now to add the checkall option,we use javascript

<script language = javascript>
function selectall()
{
var gridview = document.getElementById("GridView1");
for(var i = 1;i < gridview.rows.length; i++)
{
gridview.rows[i].cells[0].firstChild.checked = true;
}
}
</script>

0 comments:

Post a Comment