i have one button outside grid ...
my question is that if i click on that button all the checkbox in grid should be checked...
how can i do this....
plz provide me code by example..
thanks
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Amit ChoudharyPosted Apr 1, 2010, 7:58 AM
First we have the .aspx markup
<script type="text/javascript">
function SelectAll(id) {
var frm = document.forms[0];
for (i=0;i
if (frm.elements[i].type == "checkbox") {
frm.elements[i].checked = document.getElementById(id).checked;
}
}
}
script>
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" Width="400px">
<Columns>
<asp:TemplateField>
<AlternatingItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
AlternatingItemTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
ItemTemplate>
<HeaderTemplate>
<asp:CheckBox ID="cbSelectAll" runat="server" Text="Select All" />
HeaderTemplate>
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Left" />
asp:TemplateField>
Columns>
asp:GridView>
protected void GridView1_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e) {
if (e.Row.RowType == DataControlRowType.Header) {
//adding an attribute for onclick event on the check box in the header
//and passing the ClientID of the Select All checkbox
((CheckBox)e.Row.FindControl("cbSelectAll")).Attributes.Add("onclick", "javascript:SelectAll('" + ((CheckBox)e.Row.FindControl("cbSelectAll")).ClientID + "')");
and you are done.
Please mark as answer if it helps....
BangaruBabu PuretiPosted Apr 1, 2010, 7:26 AM
for (int i = 0; i < gv.Rows.Count; i++)
{
CheckBox chkrow = (CheckBox)gv.Rows[i].FindControl("chkbox");
chkrow.Checked = true;
}
Lalit MPosted Apr 1, 2010, 7:14 AM