ASP.NET GridView的使用详解
来源:互联网 发布:小众微信淘客软件 编辑:程序博客网 时间:2024/05/21 06:17
这是要实现的功能:
第一步:拖入GridView控件 并且完成查询所有数据的方法 通过this.GridView1.DataSource 获取集合数据 GridView1.DataBind() 绑定数据
第二步:实现全选功能
1. 页面代码:
代码
<asp:TemplateField HeaderText="全选">
<HeaderTemplate>
<input type="checkbox" id="CheckBox1" name="CheckBox1" onclick="GetAllCheckBox(this)" />
全选
</HeaderTemplate>
<ItemTemplate>
<input type="checkbox" name="CheckBox2" />
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" Width="100px" />
</asp:TemplateField>
<HeaderTemplate>
<input type="checkbox" id="CheckBox1" name="CheckBox1" onclick="GetAllCheckBox(this)" />
全选
</HeaderTemplate>
<ItemTemplate>
<input type="checkbox" name="CheckBox2" />
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" Width="100px" />
</asp:TemplateField>
2. 实现全选的JS代码:
代码
<script type="text/javascript" language="javascript">
function GetAllCheckBox(checkAll){
var items = document.getElementsByName("CheckBox2");
for(var i=0;i<items.length;i++){
items[i].checked = checkAll.checked;
}
}
</script>
function GetAllCheckBox(checkAll){
var items = document.getElementsByName("CheckBox2");
for(var i=0;i<items.length;i++){
items[i].checked = checkAll.checked;
}
}
</script>
第三步:实现光棒效果 突出显示被选中的行
编写RowDataBound事件 代码
代码
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
/**
* 实现光棒效果
* */
if (e.Row.RowType == DataControlRowType.DataRow)//判断是否是数据行 去除第一行(标题行)
{
e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#6699ff'");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor");
}
}
{
/**
* 实现光棒效果
* */
if (e.Row.RowType == DataControlRowType.DataRow)//判断是否是数据行 去除第一行(标题行)
{
e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#6699ff'");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor");
}
}
四。数据绑定的其他知识:
绑定方法:Bind Eval 语法:<%# Bind("字段名") %>
字段格式化:{0:D} 设置显示的内容是货币类型 {0:D}设置显示的内容是数字 {0:yy--mm--dd} 设置显示是日期格式
五。 经验:在查询所有数据的时候如果实体类存在外键关系 建议使用DataTable或DataSet 获取数据 避免出现 使用SqlDataReader 没有关闭的异常
代码
/// <summary>
/// 通过SQL语句查询所有的书籍
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public static List<Book> getBooksBySQL(string sql)
{
List<Book> list = new List<Book>();
DataTable dt = DBHelper.GetDataSet(sql);
foreach (DataRow reader in dt.Rows)
{
Book book = new Book();
book.Author = reader["author"].ToString();
book.AuthorDescription = reader["aurhorDescription"].ToString();
book.Clicks = (int)reader["Clicks"];
book.ContentDescription = (string)reader["ContentDescription"];
book.EditorComment = reader["EditorComment"].ToString();
book.Id = (int)reader["Id"];
book.Isbn = reader["Isbn"].ToString();
book.PublishDate = (DateTime)reader["PublishDate"];
book.Title = reader["Title"].ToString();
book.Toc = reader["Toc"].ToString();
book.UnitPrice = (decimal)reader["UnitPrice"];
book.WordsCount = (int)reader["WordsCount"];
book.Category = CategoryService.getCategoryByCategoryId((int)reader["categoryid"]);//PK 外键
book.Publisher = PublisherService.getPublisherByPublisherId((int)reader["publisherid"]);//PK 外键
list.Add(book);
}
return list;
}
/// 通过SQL语句查询所有的书籍
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public static List<Book> getBooksBySQL(string sql)
{
List<Book> list = new List<Book>();
DataTable dt = DBHelper.GetDataSet(sql);
foreach (DataRow reader in dt.Rows)
{
Book book = new Book();
book.Author = reader["author"].ToString();
book.AuthorDescription = reader["aurhorDescription"].ToString();
book.Clicks = (int)reader["Clicks"];
book.ContentDescription = (string)reader["ContentDescription"];
book.EditorComment = reader["EditorComment"].ToString();
book.Id = (int)reader["Id"];
book.Isbn = reader["Isbn"].ToString();
book.PublishDate = (DateTime)reader["PublishDate"];
book.Title = reader["Title"].ToString();
book.Toc = reader["Toc"].ToString();
book.UnitPrice = (decimal)reader["UnitPrice"];
book.WordsCount = (int)reader["WordsCount"];
book.Category = CategoryService.getCategoryByCategoryId((int)reader["categoryid"]);//PK 外键
book.Publisher = PublisherService.getPublisherByPublisherId((int)reader["publisherid"]);//PK 外键
list.Add(book);
}
return list;
}
代码
/// <summary>
/// 根据分类ID查询分类信息
/// </summary>
/// <param name="categoryId"></param>
/// <returns></returns>
public static Category getCategoryByCategoryId(int categoryId)
{
string sql = "select * from categories where id = @id";
Category cate = new Category();
SqlParameter[] para = new SqlParameter[]
{
new SqlParameter("@id",categoryId)
};
DataTable table = DBHelper.GetDataSet(sql,para);
foreach (DataRow reader in table.Rows)
{
cate.Id = (int)reader["id"];
cate.Name = (string)reader["name"];
}
return cate;
}
/// <summary>
/// 根据分类ID查询分类信息
/// </summary>
/// <param name="categoryId"></param>
/// <returns></returns>
public static Category getCategoryByCategoryId(int categoryId)
{
string sql = "select * from categories where id = @id";
Category cate = new Category();
SqlParameter[] para = new SqlParameter[]
{
new SqlParameter("@id",categoryId)
};
DataTable table = DBHelper.GetDataSet(sql,para);
foreach (DataRow reader in table.Rows)
{
cate.Id = (int)reader["id"];
cate.Name = (string)reader["name"];
}
return cate;
}
代码
/// <summary>
/// 根据出版社编号ID查询出版社信息
/// </summary>
/// <param name="publisherId"></param>
/// <returns></returns>
public static Publisher getPublisherByPublisherId(int publisherId)
{
string sql = "select * from publishers where id = @id";
Publisher pub = new Publisher();
SqlParameter[] para = new SqlParameter[]
{
new SqlParameter("@id",publisherId)
};
DataTable table = DBHelper.GetDataSet(sql,para);
foreach (DataRow reader in table.Rows)
{
pub.Id = (int)reader["id"];
pub.Name = (string)reader["name"];
}
return pub;
}
/// <summary>
/// 根据出版社编号ID查询出版社信息
/// </summary>
/// <param name="publisherId"></param>
/// <returns></returns>
public static Publisher getPublisherByPublisherId(int publisherId)
{
string sql = "select * from publishers where id = @id";
Publisher pub = new Publisher();
SqlParameter[] para = new SqlParameter[]
{
new SqlParameter("@id",publisherId)
};
DataTable table = DBHelper.GetDataSet(sql,para);
foreach (DataRow reader in table.Rows)
{
pub.Id = (int)reader["id"];
pub.Name = (string)reader["name"];
}
return pub;
}
- ASP.NET GridView的使用详解
- Asp.net中GridView使用详解
- Asp.net中GridView使用详解
- Asp.net中GridView使用详解(引)
- Asp.net中GridView使用详解
- Asp.net中GridView使用详解
- Asp.net中GridView使用详解
- Asp.net中GridView使用详解
- Asp.net中GridView使用详解
- Asp.net中GridView使用详解
- Asp.net中GridView使用详解(引)
- Asp.net中GridView使用详解
- Asp.net中GridView使用详解(引)
- Asp.net中GridView使用详解
- Asp.net中GridView使用详解(引)
- Asp.net中GridView使用详解
- Asp.net中GridView使用详解
- Asp.net中GridView使用详解
- C# RGB to HSL / HSL
- 泛谈Flash文件系统
- 谈谈大型高并发高负载网站的系统架构
- 腾讯 C++笔试题
- flex中dataGrid的编辑功能
- ASP.NET GridView的使用详解
- Installing Qt 4.6.2 on Embedded Linux
- Struts1第一天
- Struts1第二天
- oracle 在分组内排序的方法
- Struts1第三天
- svn install svn安装配置
- 设计模式-Adapter模式戏说
- cbuttonst 的使用技巧