62,074
社区成员
发帖
与我相关
我的任务
分享
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
function changeColor(cb1, cb2, index, id) {
document.getElementById(cb2).checked = false;
var tr = document.getElementById(cb1).parentElement.parentElement;
tr.style.backgroundColor = index == 1 ? "red" : "blue";
var cbs = document.getElementById("<%= hf.ClientID %>");
if (document.getElementById(cb1).checked) {
if (cbs.value.indexOf(id + "," + cb2) > -1)
cbs.value.replace(id + "," + cb2, id + "," + cb1);
else if (cbs.value.indexOf(id + "," + cb1) < 0)
cbs.value += id + "," + cb1 + "|";
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound"
DataKeyNames="ID" PageSize="5" OnPageIndexChanging="GridView1_PageIndexChanging"
AllowPaging="true" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="CheckBox2" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="ID" DataField="ID" />
<asp:BoundField HeaderText="Name" DataField="Name" />
<asp:BoundField HeaderText="Description" DataField="Description" />
</Columns>
</asp:GridView>
<asp:HiddenField ID="hf" runat="server" />
</div>
</form>
</body>
</html>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Bind();
}
}
void Bind()
{
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("ID", typeof(int)));
dt.Columns.Add(new DataColumn("Name", typeof(string)));
dt.Columns.Add(new DataColumn("Description", typeof(string)));
for (int i = 0; i < 25; i++)
{
DataRow dr = dt.NewRow();
dr[0] = i;
dr[1] = "Name_" + i;
dr[2] = "Description_" + i;
dt.Rows.Add(dr);
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
Bind();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
CheckBox cb1 = e.Row.FindControl("CheckBox1") as CheckBox;
CheckBox cb2 = e.Row.FindControl("CheckBox2") as CheckBox;
string id = GridView1.DataKeys[e.Row.RowIndex].Value.ToString();
if (cb1 != null && cb2 != null)
{
cb1.Attributes.Add("onclick", "changeColor('" + cb1.ClientID + "','" + cb2.ClientID + "',1," + id + ");");
cb2.Attributes.Add("onclick", "changeColor('" + cb2.ClientID + "','" + cb1.ClientID + "',2," + id + ");");
}
if (!string.IsNullOrEmpty(hf.Value))
{
if (Array.IndexOf(hf.Value.Split('|'), id + "," + cb1.ClientID) > -1)
{
e.Row.BackColor = System.Drawing.Color.Red;
cb1.Checked = true;
}
else if (Array.IndexOf(hf.Value.Split('|'), id + "," + cb2.ClientID) > -1)
{
e.Row.BackColor = System.Drawing.Color.Blue;
cb2.Checked = true;
}
}
}
}
XX_RowDataBound(object sender, GridViewRowEventArgs e)
{
CheckBox myCheckBox = e.Row.FindControl("checkD");
if(myCheckBox.Checked == true)
{
e.Row.BackColor = Color.Yellow;
}
}
private void RePopulateValues()
{
ArrayList categoryIDList = (ArrayList)Session["CHECKED_ITEMS"];
if (categoryIDList != null && categoryIDList.Count > 0)
{
foreach (GridViewRow row in GridView1.Rows)
{
int index = (int)GridView1.DataKeys[row.RowIndex].Value;
if (categoryIDList.Contains(index))
{
CheckBox myCheckBox = (CheckBox)row.FindControl("checkD");
myCheckBox.Checked = true;
row.Style.Add("background-color", "color1");//这里设置颜色就行了
}
}
}
}
private void RememberOldValues()
{
ArrayList categoryIDList = new ArrayList();
int index = -1;
foreach (GridViewRow row in GridView1.Rows)
{
index = (int)GridView1.DataKeys[row.RowIndex].Value;
bool result = ((CheckBox)row.FindControl("checkD")).Checked;
// Check in the Session
if (Session["CHECKED_ITEMS"] != null)
categoryIDList = (ArrayList)Session["CHECKED_ITEMS"];
if (result)
{
if (!categoryIDList.Contains(index))
categoryIDList.Add(index);
}
else
categoryIDList.Remove(index);
}
if (categoryIDList != null && categoryIDList.Count > 0)
Session["CHECKED_ITEMS"] = categoryIDList;
}
private void RePopulateValues()
{
ArrayList categoryIDList = (ArrayList)Session["CHECKED_ITEMS"];
if (categoryIDList != null && categoryIDList.Count > 0)
{
foreach (GridViewRow row in GridView1.Rows)
{
int index = (int)GridView1.DataKeys[row.RowIndex].Value;
if (categoryIDList.Contains(index))
{
CheckBox myCheckBox = (CheckBox)row.FindControl("checkD");
myCheckBox.Checked = true;
}
}
}
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
RememberOldValues();
this.GridView1.PageIndex = e.NewPageIndex;
this.LoadProductData();
RePopulateValues();
}
window.onload=function(){
var grid = document.getElementById("<%=GridView1.ClientID%>");
for(var i=0;i<grid.rows.length;i++){
var inputs = grid.rows[i].document.getElementsByTagName("input");
for(var j=0;j<inputs.length;j++) {
if(inputs[j].type=="checkbox")
{
if(inputs[j].checked){
if(inputs[j].id.indexOf("CheckBox1")!=-1){
grid.rows[i].style.backgroundColor="color1";
}else
{
grid.rows[i].style.backgroundColor="color2";
}
}
}
}
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
// 数据是否在大于0行
if (e.Row.RowIndex >= 0)
{
//首先判断是否是数据行
if (e.Row.RowType == DataControlRowType.DataRow)
{
//当鼠标停留时更改背景色
e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#00A9FF'");
//当鼠标移开时还原背景色
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
//单击行的任意列会自动选中此行
e.Row.Attributes.Add("onclick", "__doPostBack('GridView1','Select$" + e.Row.RowIndex + "')");
}
}