自定义GridView控件(WebBarGridView)

网摘:
using  System;
using  System.Collections.Generic;
using  System.Text;
using  System.Web.UI.WebControls;
using  System.Drawing;
using  System.ComponentModel;
using  System.Web.UI.HtmlControls;

namespace  WebBar.Controls
{
    
public   class  WebBarGridView : GridView
    {
        
private  HtmlInputCheckBox cb;
        
private  Button HideButton  =   new  Button();

        [Browsable(
true )]
        [Description(
" 双击时激发 " )]
        
public   event  EventHandler DblClick;

        
#region  自定义属性
        [Browsable(
true )]
        [Category(
" Appearance " )]
        [Description(
" 隔行背景色(单号行的背景) " )]
        
public  Color SingleBackGroundColor
        {
            
get
            {
                
return  ViewState[ " SingleBackGroundColor " !=   null   ?  (Color)ViewState[ " SingleBackGroundColor " ] : Color.Empty;
            }
            
set
            {

                ViewState[
" SingleBackGroundColor " =  value;
            }

        }

        [Browsable(
true )]
        [Category(
" Appearance " )]
        [Description(
" 隔行背景色(双号行的背景) " )]
        
public  Color DoubleBackGroundColor
        {
            
get
            {
                
return  ViewState[ " DoubleBackGroundColor " !=   null   ?  (Color)ViewState[ " DoubleBackGroundColor " ] : Color.Empty;
            }
            
set
            {

                ViewState[
" DoubleBackGroundColor " =  value;
            }

        }

        [Browsable(
true )]
        [Category(
" Appearance " )]
        [Description(
" 选中行背景色 " )]
        
public  Color ClickBackGroundColor
        {
            
get
            {
                
return  ViewState[ " ClickBackGroundColor " !=   null   ?  (Color)ViewState[ " ClickBackGroundColor " ] : Color.Empty;
            }
            
set
            {

                ViewState[
" ClickBackGroundColor " =  value;
            }

        }

        [Browsable(
true )]
        [Category(
" Appearance " )]
        [Description(
" 鼠标经过时背景色 " )]
        
public  Color MouseOverColor
        {
            
get
            {
                
return  ViewState[ " MouseOverColor " !=   null   ?  (Color)ViewState[ " MouseOverColor " ] : Color.Empty;
            }
            
set
            {

                ViewState[
" MouseOverColor " =  value;
            }

        }
        [Description(
" 双击事件后取出该行的行号 " )]
        
public   int  SelectRowIndex
        {
            
get
            {
                
int  index  =   - 1 ;
                
try
                {
                    HtmlInputHidden hidden 
=  (HtmlInputHidden) this .FindControl( " hdSelectedRowId " );
                    index 
=   int .Parse(hidden.Value);
                }
                
catch
                {
                    index 
=   - 1 ;
                }
                
return  index;
            }
            
set
            {
                
try
                {
                    HtmlInputHidden hidden 
=  (HtmlInputHidden) this .FindControl( " hdSelectedRowId " );
                    hidden.Value 
=  value.ToString();
                }
                
catch
                {
                    
base .SelectedIndex  =  value;
                }
            }
        }
        
#endregion

        
#region  自定义方法
        
///   <summary>
        
///  返回选中的选号数组
        
///   </summary>
        
///   <returns> string[] </returns>
         public   string [] SelectRows()
        {
            
string  selectIndex  =   "" ;
            
foreach  (GridViewRow row  in   this .Rows)
            {
                
if  (row.RowType  ==  DataControlRowType.DataRow)
                {
                    HtmlInputCheckBox cb 
=  (HtmlInputCheckBox)row.Cells[ 0 ].FindControl( " cb "   +  row.RowIndex.ToString());
                    
if  (cb  ==   null continue ;
                    
if  (cb.Checked)
                    {
                        selectIndex 
+=   " , "   +  cb.Value;
                    }
                }
            }
            
if  (selectIndex != "" ) { selectIndex  =  selectIndex.Substring( 1 ); }  else  {  return   null ; }
            
string [] re  =  selectIndex.Split( char .Parse( " , " ));
            
return  re;
        }
       
        
protected   void  CreateSelectedRowItems()
        {
            
if  ( this .Rows.Count  >   0 )
            {
                HtmlInputHidden hdSelectedRowId 
=  (HtmlInputHidden) this .FindControl( " hdSelectedRowId " );
                
if  (hdSelectedRowId  ==   null )
                {
                    hdSelectedRowId 
=   new  HtmlInputHidden();
                    hdSelectedRowId.ID 
=   " hdSelectedRowId " ;
                    
this .Controls.Add(hdSelectedRowId);
                }
                
if  (((Button) this .FindControl( " HideButton " ))  ==   null )
                {
                    
this .HideButton.ID  =   " HideButton " ;
                    
this .HideButton.Width  =   0 ;
                    
this .Controls.Add( this .HideButton);
                }
            }
        }

        
private   void  OnClick( object  sender,EventArgs e)
        {
            
try
            {
                
this .DblClick( this , e);
            }
            
catch
            {
            }
        }
        
#endregion

        
///   <summary>
        
///  重写初实化事件处理方法
        
///   </summary>
        
///   <param name="e"></param>
         protected   override   void  OnInit(EventArgs e)
        {
            
this .HideButton.Click  +=   new  EventHandler( this .OnClick);
            TemplateField gc 
=   new  TemplateField();
            gc.HeaderText 
=   " <input type='checkbox' id='cbAll' οnclick='checkAll(this.form);' />全选 " ;
            
this .Columns.Insert( 0 , gc);
            
base .OnInit(e);
        }
        
protected   override   void  OnRowCreated(GridViewRowEventArgs e)
        {
            
if  (e.Row.RowType  ==  DataControlRowType.DataRow)
            {
                cb 
=   new  HtmlInputCheckBox();
                cb.ID 
=   " cb "   +  e.Row.RowIndex.ToString();
                cb.Value 
=  e.Row.RowIndex.ToString();
                
// cb.Attributes["onclick"] = "checkOne('" + r.ClientID.ToString() + "_cb" + r.RowIndex.ToString() + "','" + r.ClientID.ToString() + "','" + (r.RowIndex % 2).ToString() + "');";
                
// cb.Attributes["onclick"] = "checkOne('" + this.ID + "_ctl" + (e.Row.RowIndex + 2).ToString("00") + "_cb" + e.Row.RowIndex.ToString() + "','" + this.ID + "_ctl" + (e.Row.RowIndex + 2).ToString("00") + "','" + (e.Row.RowIndex % 2).ToString() + "');";
                
// cb.Attributes["onclick"] = "checkOne('" + this.ID + "_ctl" + (e.Row.RowIndex + 2).ToString("00") + "_cb" + e.Row.RowIndex.ToString() + "',this.parent,'" + (e.Row.RowIndex % 2).ToString() + "');";
                cb.Attributes[ " onclick " =   " checkOneByBoxClick(this,this.parent,' "   +  (e.Row.RowIndex  %   2 ).ToString()  +   " '); " ;
                e.Row.Cells[
0 ].Controls.Add(cb);
            }
            
base .OnRowCreated(e);
        }

        
///   <summary>
        
///  重写行数据绑定事件处理方法
        
///   </summary>
        
///   <param name="e"></param>
         protected   override   void  OnRowDataBound(GridViewRowEventArgs e)
        {

            
if  (SingleBackGroundColor  !=   null )
            {
                
if  (e.Row.RowType  ==  DataControlRowType.DataRow)
                {

                    
if  (e.Row.RowIndex  !=   - 1 )
                    {
                        
// e.Row.Attributes["onmouseover"] = "javascript:this.style.background='" + ColorTranslator.ToHtml(MouseOverColor) + "'";
                        
// e.Row.Attributes["onmouseout"] = "onmouseoutchangestyle('" + e.Row.ClientID.ToString() + "_cb" + e.Row.RowIndex.ToString() + "','" + e.Row.ClientID.ToString() + "');";
                        
// e.Row.Attributes["onclick"] = "checkOne('"+e.Row.ClientID.ToString()+"_cb"+e.Row.RowIndex.ToString()+"','"+e.Row.ClientID.ToString()+"','"+(e.Row.RowIndex % 2).ToString()+"');";
                        e.Row.Attributes[ " onclick " =   " checkOne(' "   +  e.Row.ClientID.ToString()  +   " _cb "   +  e.Row.RowIndex.ToString()  +   " ',this,' "   +  (e.Row.RowIndex  %   2 ).ToString()  +   " '); " ;
                        e.Row.Attributes[
" ondblclick " =   " dblClick(' "   +  e.Row.RowIndex.ToString()  +   " ',' "   +   this .ClientID  +   " _hdSelectedRowId') " ;
                        
if  (e.Row.RowIndex  %   2   ==   0 ) { e.Row.BackColor  =   this .SingleBackGroundColor; }  else  { e.Row.BackColor  =   this .DoubleBackGroundColor; }
                        
// e.Row.Attributes.Add("onClick", "javascript:__doPostBack('" + this.ClientID + "','Select$" + e.Row.RowIndex + "');");
                    }
                }
            }
            
base .OnRowDataBound(e);

        }
        
protected   override   void  OnLoad(EventArgs e)
        {
            
this .CreateSelectedRowItems();
            
base .OnLoad(e);
        }
        
protected   override   void  OnPreRender(EventArgs e)
        {
            
this .CreateSelectedRowItems();
            
string  js  =   @" <script type='text/javascript'>
                          function checkOne(checkBoxId,row,rowIndex)
                          {
                            if(document.getElementById(checkBoxId)==null){return;}
                            if(document.getElementById(checkBoxId).checked==true)
                            {
                                document.getElementById(checkBoxId).checked=false;
                                if(rowIndex=='1')
                                {
                                    row.style.backgroundColor='
"   +  ColorTranslator.ToHtml(DoubleBackGroundColor)  +   @" ';
                                }
                                else
                                {
                                    row.style.backgroundColor='
"   +  ColorTranslator.ToHtml(SingleBackGroundColor)  +   @" ';
                                }
                            }
                            else
                            {
                                document.getElementById(checkBoxId).checked=true;
                                row.style.backgroundColor='
"   +  ColorTranslator.ToHtml(ClickBackGroundColor)  +   @" ';
                            }
                            
                          }
                          function checkOneByBoxClick(checkBox,row,rowIndex)
                          {
                            if(checkBox==null){return;}
                            if(checkBox.checked==true)
                            {
                                checkBox.checked=false;
                               
                            }
                            else
                            {
                                checkBox.checked=true;
                            }
                            
                          }
                        function checkAll(form)
                        {
                          for (var i=0;i<form.elements.length;i++) 
                          {
                              var e = form.elements[i];
                              if (e.name != 'cbAll')
                              {
                                e.checked = form.cbAll.checked;
                              }
                          }
                        }
                        function dblClick(rowIndex,objHiddenId)
                        {
                          document.getElementById(objHiddenId).value=rowIndex;
                          __doPostBack('
"   +   this .ID  +   @" :HideButton','');
                        }
                        </script>
" ;
            
this .Page.ClientScript.RegisterClientScriptBlock( this .GetType(),  " ClientScript " , js);
            
base .OnPreRender(e);
        }
    }
}

转载于:https://www.cnblogs.com/EasyData/archive/2008/06/06/1215260.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值