GridView技巧之改变行的背景颜色和删除确认对话框

    有时候我们希望当鼠标停在某一行时改变该行的背景颜色,还有就是当删除某一行时弹出确认对话框。我这里的删除弹出确认对话框是用LinkButton实现的,我知道在GridView里可以用自带的删除实现,但是总觉的他自带的有些不好用,呵呵,也许是我不会用。
   好了,下面就是我实现的代码,都运行过,没有错误。
 /// <summary>
    
/// 设置gridview的行,如删除时弹出确认对话框,还有设置鼠标停在行上时设置背景颜色
    
/// </summary>
    
/// <param name="gridview"></param>
    
/// <param name="bgcolor">有默认值</param>
    
/// <param name="bgflag">true表示设置背景颜色</param>

    public void SetGridViewDeleleteAndBackgroundColor(GridView gridview,string bgcolor,bool bgflag)
    
{
        
if (bgcolor == "")
            bgcolor 
= "#EAEAEA"//如果参数为空,则设置一个默认值

        
for (int i = 0; i < gridview.Rows.Count; i++)
        
{
            ((LinkButton)gridview.Rows[i].FindControl(
"LinkButtonDelete")).Attributes.Add("onclick""if(!confirm('您确认要执行吗?')) return false;");

            
if (bgflag)  //如果为true,表示要起用该功能
            {
                
//设置鼠标移动到行上的颜色
                if (gridview.Rows[i].RowType == DataControlRowType.DataRow)  //要先判断是否为gridview的数据行
                {
                    gridview.Rows[i].Attributes.Add(
"onmouseover""cl=this.style.backgroundColor;this.style.backgroundColor='"+bgcolor+"';"); //cl为保存先前的颜色,方便稍后恢复,要在前台定义变量哦。
                    gridview.Rows[i].Attributes.Add("onmouseout""this.style.backgroundColor=cl;");
                }

            }

        }
            
    }

    这里用的就是Attributes属性,其实这个属性几乎所有的服务器控件都有,就是用来设置客户端的操作,我经常用这个属性。调用该函数的代码就不用写了吧,太简单了。下面的时件中有。
下面是LinkButton的删除事件:
 protected void LinkButtonDelete_Click(object sender, EventArgs e)
    
{
               
int archivesid = Int32.Parse(((LinkButton)sender).CommandArgument.Trim()); //获取要删除的档案ID,这个值在数据源绑定时已经设置好的。
        ArchivesBLL archives = new ArchivesBLL();
        archives.DeleteArchive(archivesid); 
//调用逻辑层方法,可参考我的另一篇文章三层架构方法调用
        
//刷新列表
         DataTable dt = new DataTable();
        dt 
= archives.SelectArchivesAll(); //调用逻辑层方法
        this.GridView1.DataSource = dt;
        
this.GridView1.DataBind();

        
new OASBasicBLL().SetGridViewDeleleteAndBackgroundColor(this.GridView1, "", true);  //调用刚刚前面的那个函数
    }
  

posted on 2007-12-13 13:02  ringwang  阅读(1065)  评论(2编辑  收藏  举报