一个对象A里 包含另一个对象B时 要在gridview里帮定 对象B的一个属性
gridview 第一列是 checkbox, 有全选按钮。
checkbox列是模版列
class A{
private string id;
private string name;
private B b;
}
class B {
private string id;
private string name;
}
<asp:GridView ID="GridView1" runat="server"
onselectedindexchanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:BoundField DataField="id" HeaderText="ID" />
<asp:BoundField DataField="name" HeaderText="name" />
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("b.Name")%> '></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
gridview 第一列是 checkbox, 有全选按钮。
checkbox列是模版列
<asp:GridView ID="GridViewUser" runat="server" AllowPaging="True"
AutoGenerateColumns="False" Caption="用户信息列表" CaptionAlign="Top">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="CheckBoxSelectAll" runat="server" AutoPostBack="True" OnCheckedChanged="CheckBoxSelectAll_CheckedChanged" />全选
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBoxSelect" runat="server" Text='<%# Eval("id")%> ' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="id" HeaderText="ID" />
<asp:BoundField DataField="name" HeaderText="姓名" />
<asp:BoundField DataField="email" HeaderText="Email" />
<asp:BoundField DataField="phone" HeaderText="电话" />
</Columns>
</asp:GridView>
<div style=" text-align:center">
<asp:Button ID="deleteUser" runat="server" Text="删除"
onclick="deleteUser_Click" />
</div>
/// <summary>
/// 删除选择的用户
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void deleteUser_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in GridViewUser.Rows)
{
CheckBox chk = (CheckBox)row.FindControl("CheckBoxSelect");
if (chk.Checked) {
UserDao userDao = new UserDao();
userDao.deleteUser(int.Parse(chk.Text));
}
}
getAllUser();
}
/// <summary>
/// 设置全选按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void CheckBoxSelectAll_CheckedChanged(object sender, EventArgs e)
{
CheckBox a = (CheckBox)sender;
if (a.Checked)
{
foreach (GridViewRow row in GridViewUser.Rows)
{
CheckBox chk = (CheckBox)row.FindControl("CheckBoxSelect");
chk.Checked = true;
}
}
else
{
foreach (GridViewRow row in GridViewUser.Rows)
{
CheckBox chk = (CheckBox)row.FindControl("CheckBoxSelect");
chk.Checked = false;
}
}
}