上接扩展GridView控件(7) - 行的指定复选框选中时改变行的样式

简介:
5、重写OnPreRender方法,注册上面那段客户端脚本
  /// <summary> 
                 /// OnPreRender 
                 /// </summary> 
                 /// <param name="e"></param> 
                 protected  override  void OnPreRender(EventArgs e) 
                { 
                         base.OnPreRender(e); 
 
                         if ((!String.IsNullOrEmpty(ChangeRowCSSByCheckBox.CheckBoxID)    
                                && !String.IsNullOrEmpty(ChangeRowCSSByCheckBox.CssClassRowSelected)) 
                                || !String.IsNullOrEmpty(CssClassMouseOver)) 
                        { 
                                 // 注册实现改变行样式的客户端脚本 
                                 if (!Page.ClientScript.IsClientScriptBlockRegistered( "jsChangeRowClassName")) 
                                { 
                                        Page.ClientScript.RegisterClientScriptBlock( 
                                                 this.GetType(), 
                                                 "jsChangeRowClassName", JavaScriptConstant.jsChangeRowClassName 
                                                ); 
                                } 
                                 // 注册调用双击CheckBox函数的客户端脚本 
                                 if (!Page.ClientScript.IsStartupScriptRegistered( "jsInvokeDoubleClickCheckBox")) 
                                { 
                                        Page.ClientScript.RegisterStartupScript( 
                                                 this.GetType(), 
                                                 "jsInvokeDoubleClickCheckBox"@"<script type=""text/javascript "">yy_DoubleClickCheckBox();</script>" 
                                                ); 
                                } 
                        } 
                }
 
6、重写OnRowDataBound以通过调用相关的javascript函数实现我们想要的功能。

/// <summary> 
                 /// OnRowDataBound 
                 /// </summary> 
                 /// <param name="e"></param> 
                 protected  override  void OnRowDataBound(GridViewRowEventArgs e) 
                { 
                         if (e.Row.RowType == DataControlRowType.DataRow) 
                        { 
                                 if (!String.IsNullOrEmpty(ChangeRowCSSByCheckBox.CheckBoxID) && !String.IsNullOrEmpty(ChangeRowCSSByCheckBox.CssClassRowSelected)) 
                                { 
                                         foreach (TableCell tc  in e.Row.Cells) 
                                        { 
                                                 // 如果发现了指定的CheckBox 
                                                 if (tc.FindControl(ChangeRowCSSByCheckBox.CheckBoxID) !=  null
                                                { 
                                                        CheckBox chk = tc.FindControl(ChangeRowCSSByCheckBox.CheckBoxID)  as CheckBox; 
                                                         string cssClassUnchecked = ""; 
 
                                                         // 根据RowState的不同,取消行的选中后<tr>的不同样式(css类名) 
                                                         switch (e.Row.RowState) 
                                                        { 
                                                                 case DataControlRowState.Alternate: 
                                                                        cssClassUnchecked =  base.AlternatingRowStyle.CssClass; 
                                                                         break
                                                                 case DataControlRowState.Edit: 
                                                                        cssClassUnchecked =  base.EditRowStyle.CssClass; 
                                                                         break
                                                                 case DataControlRowState.Normal: 
                                                                        cssClassUnchecked =  base.RowStyle.CssClass; 
                                                                         break
                                                                 case DataControlRowState.Selected: 
                                                                        cssClassUnchecked =  base.SelectedRowStyle.CssClass; 
                                                                         break
                                                                 default
                                                                        cssClassUnchecked = ""; 
                                                                         break
                                                        } 
 
                                                         // 给行增加一个yy_selected属性,用于客户端判断行是否是选中状态 
                                                        e.Row.Attributes.Add( "yy_selected""false"); 
 
                                                         // 添加CheckBox的click事件的客户端调用代码 
                                                         string strOnclickScript = ""; 
                                                         if (!String.IsNullOrEmpty(chk.Attributes[ "onclick"])) 
                                                        { 
                                                                strOnclickScript += chk.Attributes[ "onclick"]; 
                                                        } 
                                                        strOnclickScript +=  ";if (this.checked) " 
                                                                +  "{yy_ChangeRowClassName('" + e.Row.ClientID +  "', '" + ChangeRowCSSByCheckBox.CssClassRowSelected +  "', true);" 
                                                                +  "yy_SetRowSelectedAttribute('" + e.Row.ClientID +  "', 'true')} " 
                                                                +  "else {yy_ChangeRowClassName('" + e.Row.ClientID +  "', '" + cssClassUnchecked +  "', true);" 
                                                                +  "yy_SetRowSelectedAttribute('" + e.Row.ClientID +  "', 'false')}"
                                                        chk.Attributes.Add( "onclick", strOnclickScript); 
 
                                                         break
                                                } 
                                        } 
                                } 
                        } 
 
                         base.OnRowDataBound(e); 
                }
 
控件使用
添加这个控件到工具箱里,然后拖拽到webform上,设置CheckBoxID属性为模板列的项复选框的ID,CssClassRowSelected属性设置为选中行的样式的CSS类名,则可以实现改变通过CheckBox选中的行的样式的功能。
ObjData.cs
using System; 
using System.Data; 
using System.Configuration; 
using System.Web; 
using System.Web.Security; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts; 
using System.Web.UI.HtmlControls; 
 
using System.ComponentModel; 
 
/// <summary> 
/// OjbData 的摘要说明 
/// </summary> 
public  class OjbData 

         public OjbData() 
        { 
                 // 
                 // TODO: 在此处添加构造函数逻辑 
                 // 
        } 
 
        [DataObjectMethod(DataObjectMethodType.Select,  true)] 
         public DataTable Select() 
        { 
                DataTable dt =  new DataTable(); 
                dt.Columns.Add( "no"typeof( string)); 
                dt.Columns.Add( "name"typeof( string)); 
 
                 for ( int i = 0; i < 30; i++) 
                { 
                        DataRow dr = dt.NewRow(); 
                        dr[0] =  "no" + i.ToString().PadLeft(2, '0'); 
                        dr[1] =  "name" + i.ToString().PadLeft(2, '0'); 
 
                        dt.Rows.Add(dr); 
                } 
 
                 return dt; 
        } 
}
 
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
        <title>SmartGridView</title> 
</head> 
<body> 
        <form id="form1" runat="server"> 
                <div> 
                        <yyc:SmartGridView ID="SmartGridView1" runat="server" DataSourceID="ObjectDataSource1" 
                                AutoGenerateColumns="false"> 
                                <Columns> 
                                        <asp:TemplateField> 
                                                <itemtemplate> 
                                                        <%# Container.DataItemIndex + 1 %> 
                                                 </itemtemplate> 
                                        </asp:TemplateField> 
                                        <asp:TemplateField ItemStyle-Width="100px"> 
                                                <itemtemplate> 
                                                        <asp:checkbox id="checkitem" runat="server" /> 
                                                </itemtemplate> 
                                        </asp:TemplateField> 
                                        <asp:BoundField DataField="no" HeaderText="序号" /> 
                                        <asp:BoundField DataField="name" HeaderText="名称" /> 
                                </Columns> 
                                <ChangeRowCSSByCheckBox CheckBoxID="checkitem" CssClassRowSelected="SelectedRow" /> 
                        </yyc:SmartGridView> 
                        <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="Select" 
                                TypeName="OjbData"></asp:ObjectDataSource> 
                </div> 
        </form> 
</body> 
</html>
 
/*测试版的实现 结束*/
 




     本文转自webabcd 51CTO博客,原文链接:http://blog.51cto.com/webabcd/345526,如需转载请自行联系原作者


相关文章
|
6月前
|
前端开发
复选框样式修改(复选框变为圆形)
复选框样式修改(复选框变为圆形)
|
6月前
如何实现更改窗体标题栏的样式
如何实现更改窗体标题栏的样式
55 0
|
6月前
[Qt5&控件] 选项卡tabWidget控件隐藏&增加tab个数
[Qt5&控件] 选项卡tabWidget控件隐藏&增加tab个数
465 0
|
6月前
|
索引
[Qt5&控件] 下拉框ComBoBox和层叠窗口StackedWidget控件组合使用
[Qt5&控件] 下拉框ComBoBox和层叠窗口StackedWidget控件组合使用
157 0
|
C++
Qt动态添加控件并设置大小位置等属性
Qt动态添加控件并设置大小位置等属性
1022 0
|
API Windows 容器
MFC应用程序——标签控件_IP控件_时间控件_List Control控件_Tree Control控件_命令按钮_列表框_组合框_图片_滚动控件(上)
MFC应用程序——标签控件_IP控件_时间控件_List Control控件_Tree Control控件_命令按钮_列表框_组合框_图片_滚动控件
206 0
MFC应用程序——标签控件_IP控件_时间控件_List Control控件_Tree Control控件_命令按钮_列表框_组合框_图片_滚动控件(上)
MFC应用程序——标签控件_IP控件_时间控件_List Control控件_Tree Control控件_命令按钮_列表框_组合框_图片_滚动控件(下)
MFC应用程序——标签控件_IP控件_时间控件_List Control控件_Tree Control控件_命令按钮_列表框_组合框_图片_滚动控件
169 0
|
C# 前端开发
wpf中的datagrid绑定操作按钮是否显示或者隐藏
如图,需要在wpf中的datagrid的操作那列有个确认按钮,然后在某些条件下确认按钮可见,某些情况下不可见的,放在mvc里直接在cshtml页面中if..else就行了。 但是在wpf里不行。。网上搜索了好久才找到解决方法,原来只是binding那个visiable属性就行了,
6896 0
|
C#
WPF关于控件 父级控件,子级控件,控件模板中的控件,等之间的相互访问
原文:WPF关于控件 父级控件,子级控件,控件模板中的控件,等之间的相互访问 1,在菜单中访问 弹出菜单的控件 var mi = sender as MenuItem;//菜单条目 MenuItem var cm = mi.
986 0