All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
数据库索引应用(ms-sql)_mssql学习_编程技术
ASP.NET技巧:DataGrid传统分页方式_[Asp.Net教程]
asp.net2.0母版页嵌套使用实例
PHPer:让我们拥抱敏捷吧_php资料_编程技术
基于JSON的高级AJAX开发技术_.net资料_编程技术
PHP实现发表文章时自动保存图片_[PHP教程]
php+mysq 修改用户密码(用password加密)_[PHP教程]
asp.net,ajax客户端编程+jquery-实现泛型数据的客户端数据调用、添加、删除_[Asp.Net教程]
构建安全的Xml,Web,Service系列
ASP.NET防止SQL注入攻击常用方法_[Asp.Net教程]
Reading number is top 10 pictures
Female model behind the bitterness, often being overcharged5
The little girl with long hair1
美女和狗狗1
中国女孩大胆自拍,显露完美身材3
日本小萝莉2
Beauty is thus produced
怀春少女-石一伊
男人帮杂志里的惹火性感美女1
邪恶搞笑内涵图
全球十大灵异酒店
Download software ranking
Unix video tutorial14
株洲本地在线棋牌游戏
尖东毒玫瑰B
jdk1.5
Boxer's Top ten classic battle6
天龙八部十二宫服务端
asp.net技术内幕
C#与.NET技术平台实战演练
塘西风月痕
Macromedia Dreamweaver 8
delv published in(发表于) 2014/1/16 9:33:14 Edit(编辑)
扩展GridView控件(七)——改变通过CheckBox选中的行的样式_[Asp.Net教程]

扩展GridView控件(七)——改变通过CheckBox选中的行的样式_[Asp.Net教程]

扩展GridView控件(七)——改变通过CheckBox选中的行的样式_[Asp.Net教程]























GridView既强大又好用。为了让它更强大、更好用,我们来写一个继承自GridView的控件。
[源码下载]
http://files.cnblogs.com/webabcd/yycontrols.rar




介绍
在GridView中如果每行都有复选框的话,选中了某个复选框则修改该复选框所在行的样式,这是经常要用到的功能,因此我们来扩展一下GridView控件。





GridView既强大又好用。为了让它更强大、更好用,我们来写一个继承自GridView的控件。
[源码下载]
1、新建一个继承自GridView的类。
复制C#代码保存代码///


/// 继承自GridView
///

[ToolboxData(@"<{0}:SmartGridView runat='server'>")]
public class SmartGridView : GridView
{
}
2、新建一个ChangeRowCSSByCheckBox实体类,有两个属性
复制C#代码保存代码using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;




namespace YYControls.SmartGridView
{
///


/// 通过行的CheckBox的选中与否来修改行的样式
/// 实体类
///

[TypeConverter(typeof(ExpandableObjectConverter))]
public class ChangeRowCSSByCheckBox
{
private string _checkBoxID;
///
/// 根据哪个ChecxBox来判断是否选中了行,指定该CheckBox的ID
///

[
Description("根据哪个ChecxBox来判断是否选中了行,指定该CheckBox的ID"),
Category("扩展"),
DefaultValue(""),
NotifyParentProperty(true)
]
public string CheckBoxID
{
get { return _checkBoxID; }
set { _checkBoxID = value; }
}




private string _cssClassRowSelected;
///


/// 选中行的样式的 CSS 类名
///

[
Description("选中行的样式的 CSS 类名"),
Category("扩展"),
DefaultValue(""),
NotifyParentProperty(true)
]
public string CssClassRowSelected
{
get { return _cssClassRowSelected; }
set { _cssClassRowSelected = value; }
}




///


/// ToString()
///

///
public override string ToString()
{
return "ChangeRowCSSByCheckBox";
}
}
}
3、在继承自GridView的类中加一个复杂对象属性,该复杂对象就是第2步创建的那个ChangeRowCSSByCheckBox
复制C#代码保存代码private ChangeRowCSSByCheckBox _changeRowCSSByCheckBox;
///
/// 通过行的CheckBox的选中与否来修改行的样式
///

[
Description("通过行的CheckBox的选中与否来修改行的样式"),
Category("扩展"),
DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
PersistenceMode(PersistenceMode.InnerProperty)
]
public virtual ChangeRowCSSByCheckBox ChangeRowCSSByCheckBox
{
get
{
if (_changeRowCSSByCheckBox == null)
{
_changeRowCSSByCheckBox = new ChangeRowCSSByCheckBox();
}
return _changeRowCSSByCheckBox;
}
}
4、新建一个JavaScriptConstant类,把我们要用到的javascript存在一个常量里
复制C#代码保存代码using System;
using System.Collections.Generic;
using System.Text;




namespace YYControls.SmartGridView
{
///


/// javascript
///

public class JavaScriptConstant
{
internal const string jsChangeRowClassName = @"";
}
}
5、重写OnPreRender方法,注册上面那段客户端脚本
复制C#代码保存代码///
/// OnPreRender
///

///
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",
@"");
}
}
}
6、重写OnRowDataBound以通过调用相关的javascript函数实现我们想要的功能。
复制C#代码保存代码///


/// OnRowDataBound
///

///
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的不同,取消行的选中后

的不同样式(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
复制C#代码保存代码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;




///


/// OjbData 的摘要说明
///

public class OjbData
{
public OjbData()
{
//
// TOD 在此处添加构造函数逻辑
//
}




[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
复制ASPX代码保存代码<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default" %>







SmartGridView






AutoGenerateColumns="false">



<%# Container.DataItemIndex + 1 %>












TypeName="OjbData">








转自【webabcd-.NET】













































添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.