`
lovnet
  • 浏览: 6703961 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
文章分类
社区版块
存档分类
最新评论

在DataGrid页眉上添加全选的CheckBox控件

阅读更多

很简单的方法,就是用js实现:

页面:

<asp:datagrid id="dgUserList" runat="server" Width="640px" BorderColor="White" PagerStyle-HorizontalAlign="Right"
AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False">
<AlternatingItemStyle BackColor="#F5F5F5"></AlternatingItemStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
<HeaderStyle HorizontalAlign="Center" ForeColor="White" BackColor="#4A95FD" Height="8"></HeaderStyle>
<PagerStyle HorizontalAlign="Right" Mode="NumericPages"></PagerStyle>
<Columns>
<asp:TemplateColumn>
<HeaderTemplate>
<asp:CheckBox id="chkAll" runat="server"></asp:CheckBox>
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox id="chkItem" runat="server"></asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="id" HeaderText="序号"></asp:BoundColumn>
<asp:BoundColumn DataField="username" HeaderText="用户名"></asp:BoundColumn>
<asp:BoundColumn DataField="workno" HeaderText="工号"></asp:BoundColumn>
<asp:BoundColumn DataField="dept" HeaderText="部门"></asp:BoundColumn>
</Columns>
<PagerStyle Visible="False" HorizontalAlign="Right" Mode="NumericPages"></PagerStyle>
</asp:datagrid>

后台代码:

private void dgUserList_PreRender(object sender, System.EventArgs e)
{
foreach (DataGridItem item in dgUserList.Controls[0].Controls)
{
if (item.ItemType == ListItemType.Header)
{
CheckBox chkAll=(CheckBox)item.FindControl("chkAll");
System.Text.StringBuilder strScript = new System.Text.StringBuilder("<script language='javascript'> \n");
strScript.Append(" function checkStatus() { \n");
strScript.Append(" var bAll = true; \n");
strScript.Append(" bAll = document.all('" + chkAll.ClientID + "').checked; \n");

for(int i=0; i<dgUserList.Items.Count ; i++)
{
strScript.Append(" document.all('" + dgUserList.Items[i].Cells[0].FindControl("chkItem").ClientID + "').checked = bAll; \n");
}
strScript.Append(" } \n");
strScript.Append("</script> \n");

if(!Page.IsClientScriptBlockRegistered("checkStatus"))
Page.RegisterClientScriptBlock("checkStatus",strScript.ToString());

chkAll.Attributes.Add("onclick","checkStatus()");
return;
}
}

<alternatingitemstyle backcolor="#F5F5F5"></alternatingitemstyle><itemstyle horizontalalign="Center"></itemstyle><headerstyle backcolor="#4A95FD" horizontalalign="Center" height="8" forecolor="White"></headerstyle><pagerstyle horizontalalign="Right" mode="NumericPages"></pagerstyle><columns><?xml:namespace prefix = asp /><templatecolumn><headertemplate><checkbox id="chkAll" runat="server"></checkbox></headertemplate><itemtemplate><checkbox id="chkItem" runat="server"></checkbox></itemtemplate></templatecolumn><boundcolumn headertext="序号" datafield="id"></boundcolumn><boundcolumn headertext="用户名" datafield="username"></boundcolumn><boundcolumn headertext="工号" datafield="workno"></boundcolumn><boundcolumn headertext="部门" datafield="dept"></boundcolumn></columns><pagerstyle horizontalalign="Right" mode="NumericPages" visible="False"></pagerstyle><alternatingitemstyle backcolor="#F5F5F5"></alternatingitemstyle><itemstyle horizontalalign="Center"></itemstyle><headerstyle backcolor="#4A95FD" horizontalalign="Center" height="8" forecolor="White"></headerstyle><pagerstyle horizontalalign="Right" mode="NumericPages"></pagerstyle><columns><templatecolumn><headertemplate><checkbox id="chkAll" runat="server"></checkbox></headertemplate><itemtemplate><checkbox id="chkItem" runat="server"></checkbox></itemtemplate></templatecolumn><boundcolumn headertext="序号" datafield="id"></boundcolumn><boundcolumn headertext="用户名" datafield="username"></boundcolumn><boundcolumn headertext="工号" datafield="workno"></boundcolumn><boundcolumn headertext="部门" datafield="dept"></boundcolumn></columns><pagerstyle horizontalalign="Right" mode="NumericPages" visible="False"></pagerstyle>

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics