通过脚本实现DataGrid控件中CheckBox全选
脚本部分如下:
后台代码部分省略......
脚本部分如下:
<script>
function selectAll(oCheckBox)
{
var datagrid = document.getElementById("<%=dgTaskList.ClientID%>");
for(i=1;i<datagrid.rows.length;i++)
{
datagrid.rows[i].cells[0].getElementsByTagName("INPUT")[0].checked=oCheckBox.checked;
}
}
</script>
function selectAll(oCheckBox)
{
var datagrid = document.getElementById("<%=dgTaskList.ClientID%>");
for(i=1;i<datagrid.rows.length;i++)
{
datagrid.rows[i].cells[0].getElementsByTagName("INPUT")[0].checked=oCheckBox.checked;
}
}
</script>
<asp:datagrid id="dgTaskList" runat="server" Width="100%" DataKeyField="PDID" AllowCustomPaging="False"
AutoGenerateColumns="False">
<HeaderStyle CssClass="headerStyle" ForeColor="white" HorizontalAlign="Center" />
<ItemStyle BackColor="white" />
<AlternatingItemStyle BackColor="#EEEEEE" />
<Columns>
<asp:TemplateColumn HeaderText="选择" ItemStyle-HorizontalAlign="center" ItemStyle-Width="60px">
<HeaderTemplate>
<input id="checkBox1" type="checkbox" onclick="selectAll(this)" runat="server"><label>全选</label>
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox Runat="server" ID="ck"></asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="ProductID" HeaderText="ID码" ReadOnly="True" ItemStyle-Width="100px"></asp:BoundColumn>
<asp:BoundColumn DataField="CHName" HeaderText="中文名称" ReadOnly="true" ItemStyle-Width="80px"></asp:BoundColumn>
<asp:BoundColumn DataField="ENName" HeaderText="英文名称" ReadOnly="True" ItemStyle-Width="120px"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="描述信息" ItemStyle-Width="280" ItemStyle-HorizontalAlign="Left">
<ItemTemplate>
<pre style="table-layout:fixed;word-wrap:break-word;margin:5,0,2,0;text-align:left;text-valign:middle;"><%#DataBinder.Eval(Container.DataItem, "Describe" ) %></pre>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="tbDescribe" Runat="server" TextMode="MultiLine" Rows=5 Width="280" Text='<%#DataBinder.Eval(Container.DataItem, "Describe" ) %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="执行" ItemStyle-Width="55px" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Label Runat="server" ID="lbUpdate" Text='<%# Convert.ToBoolean(DataBinder.Eval(Container.DataItem,"IsUpdate"))?"<font size=2>已执行</font>":"<font color=red>未执行</font>"%>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="挂网" ItemStyle-Width="55px" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Label Runat="server" ID="lbUpload" Text='<%# Convert.ToBoolean(DataBinder.Eval(Container.DataItem,"IsUpload"))?"<font size=2>已挂网</font>":"<font color=red>未挂网</font>"%>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
<asp:EditCommandColumn ButtonType="PushButton" UpdateText="更新" CancelText="取消" EditText="编辑" itemstyle-horizontalalign="Center"
itemstyle-width="90" HeaderText="操作"></asp:EditCommandColumn>
</Columns>
</asp:datagrid>
AutoGenerateColumns="False">
<HeaderStyle CssClass="headerStyle" ForeColor="white" HorizontalAlign="Center" />
<ItemStyle BackColor="white" />
<AlternatingItemStyle BackColor="#EEEEEE" />
<Columns>
<asp:TemplateColumn HeaderText="选择" ItemStyle-HorizontalAlign="center" ItemStyle-Width="60px">
<HeaderTemplate>
<input id="checkBox1" type="checkbox" onclick="selectAll(this)" runat="server"><label>全选</label>
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox Runat="server" ID="ck"></asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="ProductID" HeaderText="ID码" ReadOnly="True" ItemStyle-Width="100px"></asp:BoundColumn>
<asp:BoundColumn DataField="CHName" HeaderText="中文名称" ReadOnly="true" ItemStyle-Width="80px"></asp:BoundColumn>
<asp:BoundColumn DataField="ENName" HeaderText="英文名称" ReadOnly="True" ItemStyle-Width="120px"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="描述信息" ItemStyle-Width="280" ItemStyle-HorizontalAlign="Left">
<ItemTemplate>
<pre style="table-layout:fixed;word-wrap:break-word;margin:5,0,2,0;text-align:left;text-valign:middle;"><%#DataBinder.Eval(Container.DataItem, "Describe" ) %></pre>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="tbDescribe" Runat="server" TextMode="MultiLine" Rows=5 Width="280" Text='<%#DataBinder.Eval(Container.DataItem, "Describe" ) %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="执行" ItemStyle-Width="55px" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Label Runat="server" ID="lbUpdate" Text='<%# Convert.ToBoolean(DataBinder.Eval(Container.DataItem,"IsUpdate"))?"<font size=2>已执行</font>":"<font color=red>未执行</font>"%>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="挂网" ItemStyle-Width="55px" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Label Runat="server" ID="lbUpload" Text='<%# Convert.ToBoolean(DataBinder.Eval(Container.DataItem,"IsUpload"))?"<font size=2>已挂网</font>":"<font color=red>未挂网</font>"%>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
<asp:EditCommandColumn ButtonType="PushButton" UpdateText="更新" CancelText="取消" EditText="编辑" itemstyle-horizontalalign="Center"
itemstyle-width="90" HeaderText="操作"></asp:EditCommandColumn>
</Columns>
</asp:datagrid>
后台代码部分省略......