62,254
社区成员
发帖
与我相关
我的任务
分享<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns=False Width="230px">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:Button ID="Button1" runat="server" Text="全选" OnClick="Button1_Click" />
</HeaderTemplate>
<ItemTemplate>
<%#Eval("id") %>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="title" />
</Columns>
</asp:GridView>protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.Add("title");
dt.Columns.Add("id");
dt.Rows.Add("sdafsa","1");
dt.Rows.Add("sdafsa","2");
dt.Rows.Add("sdafsa","3");
dt.Rows.Add("sdafsa","4");
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Button bt = (Button)sender;
if (bt.Text == "全选")
for (int i = 0; i < GridView1.Rows.Count; i++)
{
GridView1.Rows[i].Cells[0].Style.Add("background-color", "red");
bt.Text = "取消";
}
else
for (int i = 0; i < GridView1.Rows.Count; i++)
{
GridView1.Rows[i].Cells[0].Style.Add("background-color", "white");
bt.Text = "全选";
}
}protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.Add("title");
dt.Columns.Add("id");
dt.Rows.Add("sdafsa", "1");
dt.Rows.Add("sdafsa", "2");
dt.Rows.Add("sdafsa", "3");
dt.Rows.Add("sdafsa", "4");
GridView1.DataSource = dt;
GridView1.DataBind();
}
Button bt = new Button();
bt.ID = "bt1";
bt.Text = "测试";
bt.CommandArgument = "1";
bt.Click += new EventHandler(bt_Click);
GridView1.HeaderRow.Cells[1].Controls.Add(bt);
}
protected void GridView1_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
//Button bt = new Button();
//bt.ID = "bt1";
//bt.Text = "测试";
//bt.CommandArgument = "1";
//bt.Click+=new EventHandler(bt_Click);
//e.Row.Cells[1].Controls.Add(bt);
}
}
protected void bt_Click(object sender, EventArgs e)
{
Button bt = (Button)sender;
if (bt.CommandArgument == "1")
for (int i = 0; i < GridView1.Rows.Count; i++)
{
GridView1.Rows[i].Cells[0].Style.Add("background-color", "red");
bt.CommandArgument = "0";
}
else
for (int i = 0; i < GridView1.Rows.Count; i++)
{
GridView1.Rows[i].Cells[0].Style.Add("background-color", "white");
bt.CommandArgument = "1";
}
}<asp:GridView ID="GridView1" runat="server" Width="230px" OnRowDataBound="GridView1_OnRowDataBound">
</asp:GridView>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
namespace WebApplication2
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.Add("title");
dt.Columns.Add("id");
dt.Rows.Add("sdafsa", "1");
dt.Rows.Add("sdafsa", "2");
dt.Rows.Add("sdafsa", "3");
dt.Rows.Add("sdafsa", "4");
GridView1.DataSource = dt;
GridView1.DataBind();
}
Button bt = new Button();
bt.ID = "bt1";
bt.Text = "测试";
bt.CommandArgument = "1";
bt.Click += new EventHandler(bt_Click);
GridView1.HeaderRow.Cells[1].Controls.Add(bt);
}
protected void bt_Click(object sender, EventArgs e)
{
}
protected void GridView1_OnDataBound(object sender, EventArgs e)
{
}
}
}
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
ondatabound="GridView1_OnDataBound">
</asp:GridView>
</div>
</form>
</body>