ASP.NET 回顾 -DataGrid 数据网格
来源:互联网 发布:淘宝上的手机怎么按揭 编辑:程序博客网 时间:2024/05/18 03:03
我爱北京天安门
天安门上太阳升
伟大领袖毛主席
指引我们向前进
1.1 基本简介
主要属性的含义
AutoGenerateColumns:自动产生列,如果你想自已通过DataGrid的<Columns>来自定义列,则必须把此值设为false
AllowSorting:允许排序(true),结合SortExpression来使用
AllowPaging:允许分页(true),结合PageSize, PagerStyle来使用,可以定义NumericPages, NextPrev二种模式,即数字与下一页。
PageSize:每页显示的记录数
DataKeyField:主键
DataMember:数据源中的数据成员,比如你有多个返回table,你可以指定某个table。
DataSource:绑定的数据源,一般是Dataview,datatable,dataset
AccessKey:按键
AllowCustomPaging:允许自定义分页
CurrentPageIndex:当前页码
SelectedIndex:当前选择的行
ShowHeader:是否显示表头
ShowFooter:是否显示表脚
1.2 DataGrid的通用功能
1.2.1 DataGrid的EditCommandColumn如何使用图片作为按钮?
答 : <asp: EditCommandColumn ButtonType="LinkButton" UpdateText="<img src='update.jpg' border='0'>" CancelText="<img src='cancel.jpg' border='0'>" EditText="<img src='edit.jpg' border='0'>"></asp:EditCommandColumn>
1.2.2 在我做的项目中同事经常对我说DataGrid的事件丢失了,为什么丢失呢?
答 :是因为在aspx中有绑定数据,且绑定数据又是一个超链接时,在aspx页面设计界面与代码界面切换时,就可能会导致某些事件的丢失,不仅仅是Datagrid,其他通过双击控件自动产生的事件或通过属性来产生的事件都有可能丢失。
最保险的方法是不要通过以上方法来产生控件的事件,如按扭控件就直接在控件的OnClick=” Button1_Click”,但要修改一下Button1_Click方法访问修饰符。
1.2.3. 利用footertext位置显示新增行,并进行新增操作。
这种方法就是利用了footertext这个属性,在footertext中可以这样写
footertext="<input type=text name=Class>"
运行的时候这就是一个输入框,类似的下面就是一个按钮
footertext="<input type=button value='增加'>"
输入和提交都有了,下面就是把数据提交到数据库中,我要利用一个隐藏的服务器控件
<asp:linkbutton id="AddNew" runat="server"></asp:linkbutton>
来帮忙(这个就是以前解决一个问题时用的方法),从工具箱里拽一个linkbutton出来,在它的属性中把Text属性设为空(也就是让运行的时候看不见),然后双击这个linkbutton(也就是增加它的事件关联),你可以在创建好的事件里面写你要往数据库中添加的数据
接受提交过来的数据要这样写Request.Form["Class"].ToString()
接下来是最主要的部分了,在页面中添加
<script language="javascript">
function AddNew()
{
__doPostBack('AddNew','');
}
</script>
然后把datagrid中foot里含有按钮的那列的footertext属性改成下面这样
footertext="<input type=button value='增加' onclick='javascript:return AddNew();'>"
这样就可以实现datagrid带增加新数据的功能了
1.2.4. DataGrid的数据编辑功能
更新数据库在 Web 应用程序中可能经常很棘手。针对这种情况,DataGrid控件提供了一些使更新更容易的内置支持。为了允许对行进行编辑,DataGrid 支持整型 EditItemIndex 属性,该属性指示网格的哪一行应该是可编辑的。设置了该属性后,DataGrid按该索引将行呈现为文本输入框,而不是简单的标签。值 -1(默认值)指示没有行是可编辑的。页可以在服务器端窗体中包含 DataGrid,并通过 DataGrid的对象模型获取对编辑数据的访问。
<asp:datagrid id="dg1"
runat="server"
AllowSorting="True"
AutoGenerateColumns="False"
AllowPaging="True"
PageSize="2"
OnPageIndexChanged="dg1_PageIndexChanged"
ShowFooter="True"
ShowHeader="true"
DataKeyField="id">
<asp:EditCommandColumn ButtonType="LinkButton" HeaderText="操作" UpdateText="确定" CancelText="取消" EditText="编辑"
footertext="<input type=button value='增加' onclick='javascript:return append();'>">
</asp:EditCommandColumn>
首先要设置DataGrid中事件属性,可以通过”属性”栏来设置,也可以直接在以上的DataGrid中添加OnEditCommand=””等方式来绑定事件。
在 DataGrid 标记本身上,将事件处理程序连到从 EditCommandColumn激发的每个命令。这些处理程序的 DataGridCommandEventArgs 参数使您得以直接访问由用来设置 DataGrid 的 EditItemIndex的客户端选择的索引。注意,需要重新绑定 DataGrid 以使更改生效
代码如下:
点”编辑”后将调用的方法
点” 取消”后将调用的方法
{
Response.Write("Edit");
this.dg1.EditItemIndex = (int)e.Item.ItemIndex;
Sort_Bind();
}
” 确定”后将调用的方法
{
Response.Write("Cancel");
this.dg1.EditItemIndex = -1;
this.Sort_Bind();
}
点
排序
{
Response.Write("update");
string id = this.dg1.DataKeys[(int)e.Item.ItemIndex].ToString();
string code = ((TextBox)e.Item.Cells[0].Controls[0]).Text;
string name = ((TextBox)e.Item.Cells[1].Controls[0]).Text;
string sql ="update notice_important set code='"+code+"',name='"+name+"' where id='"+id+"'";
SqlHelper.ExecuteNonQuery(GetConnectionStr(),CommandType.Text,sql);
this.dg1.EditItemIndex = -1;
this.Sort_Bind();
}
1.2.5
概述
Datagrid的数据排序功能,Datagrid本身并不能实现,它只是一个展现数据的HtmlTable,没有对数据查询进行操作功能,,我们可以对Datagrid的数据源,即DataView数据视图对象进行操作来实现我们的排序功能。
应用(sample代码)
a. Datagrid的AllowSorting属性要设为true
<asp:datagrid id="dg1" runat="server" AllowSorting="True"> …</asp:datagrid>
b. 在<Column>…</Column>中的各种列中要指SortExpression的属性值,即排序的字段。
<asp:BoundColumn DataField="code" SortExpression="code" HeaderText="code" >
c. 给Datagrid添加OnSortCommand事件调用的方法,并在方法中添加处理代码。
{
string strSortExpression = e.SortExpression.ToString();
string strSortDirection = "ASC";
if(strSortExpression == this.dg1.Attributes["SortExpression"])
{
strSortDirection = (this.dg1.Attributes["SortDirection"].ToString() == strSortDirection?"DESC":"ASC");
}
this.dg1.Attributes["SortExpression"] = strSortExpression;
this.dg1.Attributes["SortDirection"] = strSortDirection;
this.Sort_Bind();
}
private void Sort_Bind()
{
string sql = "select id,code,name from notice ";
DataView dv = new DataView();
dv = SqlHelper.ExecuteDataset(GetConnectionStr(),CommandType.Text,sql).Tables[0].DefaultView;
dv.Sort = this.dg1.Attributes["SortExpression"]+ " " +this.dg1.Attributes["SortDirection"]; //或者dv.Sort = “code asc”
this.dg1.DataSource = dv;
this.dg1.DataBind();
}
1.2.6 DataGrid的多选(CheckBox)/单选(radio)
对于多选我们会想到在DataGrid的<templatecolumn>中绑定<asp:check..>控件来实现,并通过对每一行进行遍历判断其是否checked。单选如何实现呢?首先要实现单选,其控件的name必须相同,如果DataGrid的<templatecolumn>中绑定,其控件的name是不同的,当然也达不到单选择效果。为此我们可以在DataGrid的<templatecolumn>中绑定一个label或Literal,在DataGrid的ItemDataBound事件中,添上如下代码:
Label1 .Text = "<input type=radio name='rd' value=" & e.Item.Cells(0).Text & ">"或
Literal1 .Text = "<input type=radio name='rd' value=" & e.Item.Cells(0).Text & ">"
确定被选中的check:
{
string str=string.empty;
if(((System.Web.UI.WebControls.RadioButton)item.FindControl("rb")).Checked)
{
str += item.Cells[2].Text;
}
Response.write(str);
}
1.2.7 简单的分页
这里讲DataGrid本身只带的分页功能,在博客园上看到有些网友做了一些包含分页、排序的DG控件,真的要感谢这些人的努力,不像我还在写这些全国人民都知道的东西,其实写这些也是以便以后要用时,可以很方便地拿来用,对于一些细节,作为一个软件开发人员,至少本人真的很容易遗忘。
a. AllowPaging="True" OnPageIndexChanged="dg1_PageIndexChanged" PageSize="5"
b. <PagerStyle BackColor="Blue" ForeColor="#ffffff" Mode="NextPrev"></PagerStyle>
Mode:
NextPrev :代表用”>”"<"等上一页、下一页来导航
NumericPages:代表用数字来导航
c.添加PageIndexChanged事件
{
this.dg1.CurrentPageIndex = e.NewPageIndex;
Sort_Bind();
}
- ASP.NET 回顾 -DataGrid 数据网格
- EasyUI Datagrid 数据网格
- EasyUI Datagrid 数据网格
- EasyUI DataGrid 数据网格
- EasyUI-1.Datagrid 数据网格
- 管理 ASP.NET DataGrid 数据列[图]
- asp.net DataGrid实现分页显示数据
- ASP.NET中使用DataGrid显示数据
- ASP.NET中实现DataGrid数据排序
- ASP.NET的DataGrid批量添加数据
- 另类DATAGRID数据编辑修改 datagrid .asp.net
- ASP.NET 2.0:弃用 DataGrid 吧,有新的网格控件了!
- ASP.NET 2.0:弃用 DataGrid 吧,有新的网格控件了!
- ASP.NET 2.0:弃用 DataGrid 吧,有新的网格控件了!---GridView
- MSDN: ASP.NET 2.0:弃用 DataGrid 吧,有新的网格控件了!
- ASP.NET 2.0:弃用 DataGrid 吧,有新的网格控件了!
- ASP.NET 2.0:弃用 DataGrid 吧,有新的网格控件了!
- ASP.NET 2.0:弃用 DataGrid 吧,有新的网格控件了!
- 如何自动执行QTP测试脚本,即自动启动QTP并执行软件测试过程?
- 如何提高当前进程的权限
- my Flickr
- 几个简单的正则表达式
- 好像现在很流行的
- ASP.NET 回顾 -DataGrid 数据网格
- 在struts中分页的一种实现
- 新年新址新情况
- 放假
- 不喜欢这样的生活...
- 最后一块钱(转)
- 烟花一月
- 成都,今天我将离开
- 姥姥走了