请教:根据GridView中不同CheckBox的选定而改变当前行的颜色,且在分页后能保存和恢复颜色;具体情况见内

臭你个臭臭 2009-03-11 01:55:11
描述:在一个GridView中我内嵌了两个CheckBox,且两个CheckBox是处于同一模板列中,分别为CheckBox1和CheckBox2.现在的问题是:当我选定CheckBox1时,当前行变为颜色1,选定CheckBox2时当前行颜色变为2,特别要注意到是,GridView具有分页功能,当PageIndexChanging事件发生时,要能保存颜色和恢复颜色(即回来后还是能看见由于选定CheckBox而变化的颜色)。
注:关于CheckBox在分页后保存状态及两个CheckBox之间选定逻辑问题已经解决,不用考虑,求代码,在线等
...全文
260 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
WeekWant 2009-03-12
  • 打赏
  • 举报
回复
你分页的时候不会保留1、2、3、4....页的checkBox的状态吗?
看上面你是保留的!!
那么当你触发翻页事件时,你肯定要把“那些保留”恢复到GridView中!
同时你需要把数据绑定到GridView中,也就是这个时候
你可以在GridView1_RowDataBound事件中做判断,设置颜色!
看不懂了~~晕,你到底是问啥?是上面的吗?
我姓区不姓区 2009-03-11
  • 打赏
  • 举报
回复
参考:

<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;
}
}
}

}
masterkill 2009-03-11
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 MicroDeviser 的回复:]
在RowDataBound的事件里,判断当前行的checkbox的选中状态就好OK了啊
[/Quote]
设置颜色我会,问题是怎么保存和恢复,我要分页的
MicroDeviser 2009-03-11
  • 打赏
  • 举报
回复
在RowDataBound的事件里,判断当前行的checkbox的选中状态就好OK了啊
liuyeede 2009-03-11
  • 打赏
  • 举报
回复
保存和恢复颜色是关键。
koukoujiayi 2009-03-11
  • 打赏
  • 举报
回复
有三种颜色设置:
在GridView的RowDataBound事件代码:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//设置两个CheckBox调用的js
e.Row.Attributes.Add("id", "tr" + e.Row.RowIndex.ToString());
CheckBox myC1 = (CheckBox)e.Row.Cells[0].FindControl("CheckBox1");
CheckBox myC2 = (CheckBox)e.Row.Cells[0].FindControl("CheckBox2");
myC1.Attributes.Add("onclick", "ChangeColor(" + myC1.ClientID + "," + myC2.ClientID + ",tr" + e.Row.RowIndex.ToString() + ");");
myC2.Attributes.Add("onclick", "ChangeColor(" + myC1.ClientID + "," + myC2.ClientID + ",tr" + e.Row.RowIndex.ToString() + ");");

//以下是分页绑定时设置的颜色
if (myC1.Checked & myC2.Checked)
e.Row.BackColor = System.Drawing.Color.Red;
else if (myC1.Checked & !myC2.Checked)
e.Row.BackColor = System.Drawing.Color.Green;
else if (!myC1.Checked & myC2.Checked)
e.Row.BackColor = System.Drawing.Color.Blue;
else
e.Row.BackColor = System.Drawing.Color.White;
}
}


js代码:这是点击时改变颜色

<script type="text/javascript">
function ChangeColor(obj1,obj2,tr)
{
if(obj1.checked & obj2.checked )
tr.style.backgroundColor='#ff0000';
else if(obj1.checked & !obj2.checked)
tr.style.backgroundColor='#00ff00';
else if(!obj1.checked & obj2.checked)
tr.style.backgroundColor='#0000ff';
else
tr.style.backgroundColor='#ffffff';
}
</script>



一切OK!!
WeekWant 2009-03-11
  • 打赏
  • 举报
回复
谢谢~
WeekWant 2009-03-11
  • 打赏
  • 举报
回复

XX_RowDataBound(object sender, GridViewRowEventArgs e)
{
CheckBox myCheckBox = e.Row.FindControl("checkD");

if(myCheckBox.Checked == true)
{
e.Row.BackColor = Color.Yellow;
}
}

试试这个事件~~
臭你个臭臭 2009-03-11
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 CutBug 的回复:]
C# code 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))

[/Quote]
还是不行
阿非 2009-03-11
  • 打赏
  • 举报
回复
说个思路,你尝试着自己写一下。
保存 的时候 有两个关键的要存,一是 主键,你要能区分出 你保存的是第几行,在整个记录集中,不单单是当前页面。
二,就是选择的是那个,不是有两个 CheckBox 么,要能区分哪个被选中。
哪行的CheckBox被选中,你保存哪行 就可以了。取消选中时,你从保存中清除即可。

你保存应该问题不大。剩下的工作 就是根据 保存的 来设置 行的颜色,你在RowDataBound 事件中 处理即可

就是判断 当前行的主键 和保存的主键 是否一致,一致 就根据 CheckBox 选中状态 来设置背景色。
CutBug 2009-03-11
  • 打赏
  • 举报
回复
 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");//这里设置颜色就行了
}
}
}
}
臭你个臭臭 2009-03-11
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 WeekWant 的回复:]
对啊!!
你把checkBox状态往GridView中绑定时,在去判断下!!
在把颜色赋了!不就行了??
状态是用JS记录的吗??
能不能共享下!!
谢谢了!!
[/Quote]

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();
}
臭你个臭臭 2009-03-11
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 CutBug 的回复:]
JScript codewindow.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…
[/Quote]
为什么不是在CheckBox的Click事件中处理呢?
WeekWant 2009-03-11
  • 打赏
  • 举报
回复
对啊!!
你把checkBox状态往GridView中绑定时,在去判断下!!
在把颜色赋了!不就行了??
状态是用JS记录的吗??
能不能共享下!!
谢谢了!!
臭你个臭臭 2009-03-11
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 Sandy945 的回复:]
状态 你都会保存了,设置背景色有难度么?
你怎么保存的状态?
[/Quote]
保存状态是建立一个ArrayList,然后遍历所有行,通过判断(CheckBox)row.FindControl("CheckBox1").Checked的True or False存入Session中(false则remove()),存好再获取状态,两个function;完成后再PageIndexChanging事件中调用
前辈我刚碰ASP.NET一个星期,代码基本上不会写,靠他人帮助,惭愧
阿非 2009-03-11
  • 打赏
  • 举报
回复
状态 你都会保存了,设置背景色有难度么?
你怎么保存的状态?
臭你个臭臭 2009-03-11
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 CutBug 的回复:]
JScript codewindow.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…
[/Quote]
没反应
CutBug 2009-03-11
  • 打赏
  • 举报
回复
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";
}
}
}

}
}
}
金大哈 2009-03-11
  • 打赏
  • 举报
回复
误解了
金大哈 2009-03-11
  • 打赏
  • 举报
回复

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 + "')");

}
}

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧