Reputation: 121
i used this below code but it is not working..if i select header check box then all other check box should select and how to get the particular row id ,if check box is selected.
protected void headerLevelCheckBox_CheckedChanged(object sender, EventArgs e)
{
CheckBox headerChkBox = ((CheckBox)gvApproach.HeaderRow.FindControl("headerLevelCheckBox"));
if (headerChkBox.Checked == true)
{
foreach(GridViewRow gvRow in gvApproach.Rows)
{
CheckBox rowChkBox = ((CheckBox)gvRow.FindControl("rowLevelCheckBox"));
rowChkBox.Checked = true;//((CheckBox)sender).Checked;
}
}
else
{
foreach (GridViewRow gvRow in gvApproach.Rows)
{
CheckBox rowChkBox = ((CheckBox)gvRow.FindControl("rowLevelCheckBox"));
rowChkBox.Checked = false;
}
}
}
i am using vs 2008,c#
without using javascript..
Upvotes: 0
Views: 4997
Reputation: 3952
If you want to do it using javascript,
You should use the checkBox's tooltip attribute for holding records' ids. For Example :
<asp:CheckBox ID="rowLevelCheckBox" runat="server" ToolTip='<%#Eval("ID")%>' />
Then ,You should use javascript code when click checkbox of header.
<script type="text/javascript">
function SelectAll(element) {
if ($(element).attr("checked")) {
$("input[type=checkbox]").attr("checked", "true");
}
else {
$("input[type=checkbox]").attr("checked", "");
}
}
</script>
<HeaderTemplate>
<div style="text-align: center; width: 100px; margin: auto">
<input type="checkbox" name="SelectAllCheckBox" onclick="SelectAll(this)">Select All</div>
</HeaderTemplate>
finally, in Code Behind.
Do you want to get selected ids, you can use this code.
public List<string> GetAllSelectedIds()
{
List<string> selectedIds= new List<string>();
for (int i = 0; i < grid.Rows.Count; i++)
{
GridViewRow row = grid.Rows[i];
if (((CheckBox)row.FindControl("rowLevelCheckBox")).Checked)
{
string rowLevelCheckBoxStr = ((CheckBox)row.FindControl("rowLevelCheckBox")).ToolTip;
selectedIds.Add(rowLevelCheckBoxStr);
}
}
return selectedIds;
}
Upvotes: 0
Reputation: 121
aspx code...and to get the id of particular ROW IN GRID VIEW i used labe control as PhotoId (PK)
<asp:GridView ID="gvApproach" runat="server" CellPadding="4" AutoGenerateColumns="False"
GridLines="None">
<Columns>
<asp:TemplateField HeaderText="PhotoId" Visible="false">
<ItemTemplate>
<asp:Label ID="lblPhotoId" runat="server" Text='<%#Eval("PhotoId") %>' Visible="false"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox runat="server" ID="headerLevelCheckBox" AutoPostBack="true" oncheckedchanged="headerLevelCheckBox_CheckedChanged" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox runat="server" ID="rowLevelCheckBox" AutoPostBack="true"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
in code behind....
protected void headerLevelCheckBox_CheckedChanged(object sender, EventArgs e)
{
CheckBox headerChkBox = ((CheckBox)gvApproach.HeaderRow.FindControl("headerLevelCheckBox"));
if (headerChkBox.Checked == true)
{
foreach(GridViewRow gvRow in gvApproach.Rows)
{
CheckBox rowChkBox = ((CheckBox)gvRow.FindControl("rowLevelCheckBox"));
rowChkBox.Checked = true;
}
}
else
{
foreach (GridViewRow gvRow in gvApproach.Rows)
{
CheckBox rowChkBox = ((CheckBox)gvRow.FindControl("rowLevelCheckBox"));
rowChkBox.Checked = false;
}
}
}
Upvotes: 3
Reputation: 2075
To be more specific you should use something like
headerChkBox onclick="changeCheckboxes(this)"
function changeCheckboxex(mainCheck)
{
$(yourDivContainer).children("INPUT[type='checkbox']").attr('checked', yourValue);
}
Hope it helps
Upvotes: 0
Reputation:
I suggest that you don't do that on the server, and to do it on the client instead. Use javascript. Do you have jQuery present on that page (it'll be easier with that library)? Maybe this tutorial will help: https://web.archive.org/web/20210304130642/https://www.4guysfromrolla.com/articles/120810-1.aspx
Upvotes: 2