当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > asp.net TemplateField模板中的Bind方法和Eval方法

ASP.NET
asp.net服务器上几种常见异常的解决方案.
Asp.net 下载功能的解决方案
asp.net 页面传值的几个方法
asp.net Cookie跨域、虚拟目录等设置方法
aspnet_isapi.dll设置图文方法.net程序实现伪静态
ASP.NET Web应用程序的安全解决方案浅析
asp.net 图片的读写入库实现代码
asp.net cookie的读写实例
浅析ASP.NET生成随机密码函数
asp.net 防止用户通过后退按钮重复提交表单
ASP.NET 调用百度搜索引擎的代码
asp.net用url重写URLReWriter实现任意二级域名 新
asp.net用url重写URLReWriter实现任意二级域名 高级篇
asp.net 下载文件时根据MIME类型自动判断保存文件的扩展名
asp.net 文件上传 实时进度
asp.net+jquery Gridview的多行拖放, 以及跨控件拖放
ASP.NET 页面之间传递值方式优缺点比较
asp.net 页面转向 Response.Redirect, Server.Transfer, Server.Execute的区别
ASP.NET 返回随机数实现代码
asp.net FreeTextBox配置详解

ASP.NET 中的 asp.net TemplateField模板中的Bind方法和Eval方法


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-13   浏览: 81 ::
收藏到网摘: n/a

在TemplateField模板中为了能够有限制的或者取出数据库中某列的值时,可以用Bind和Eval方法来实现。以下是Bind方法的格式,Eval的格式也是和Bind一样的。 Bind("列的名称","显示的格式文") 比如我们要取个日期型的数据,在数据库中列名是updated,数值是2008/06/01。但是想2008年06月01日这样显示,我们可以这样来写Bind("updated", "{0:yyyy年MM月dd日}"),Eval也是如此。
2者都能读取数据中的值,并显示。当我们使用编辑更新操作时,Bind能够自动的将修改的值更新到数据库中,并显示出修改后的值。但是用了Eval却只能得到错误画面,新的数据没有更新到数据库中。
从这点看来,Bind方法和Eval方法的区别就是:Bind方法在读取和更新数据这2方面都是可以,但是Eval方法只能读取显示数据。所以,我们在选择Bind方法和Eval方法的时候,必须要有争对性,当数据肯定需要更新操作的时候我们应该使用Bind,只是显示数据,不会有任何操作的就可以使用Eval方法。
在更新操作中我们可以在GridView1_RowUpdating事件中操作,例子如下:
复制代码 代码如下:

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//更新行GridViewRow
GridViewRow row = this.GridView1.Rows[e.RowIndex];
//查找更新的控件
DropDownList present = (DropDownList)row.FindControl("ddlPresent");
TextBox price = (TextBox)row.FindControl("txtPrice");
TextBox updated = (TextBox)row.FindControl("txtUpdated");
//更新
e.NewValues["present"] = present.SelectedValue;
e.NewValues["price"] = price.Text;
e.NewValues["updated"] = updated.Text;
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//更新行GridViewRow
GridViewRow row = this.GridView1.Rows[e.RowIndex];
//查找更新的控件
DropDownList present = (DropDownList)row.FindControl("ddlPresent");
TextBox price = (TextBox)row.FindControl("txtPrice");
TextBox updated = (TextBox)row.FindControl("txtUpdated");
//更新
e.NewValues["present"] = present.SelectedValue;
e.NewValues["price"] = price.Text;
e.NewValues["updated"] = updated.Text;
}

如果我们能充分理解Bind方法和Eval方法,其实也就没必要向上面那样去写,都是可以自动完成的。上面的方法除了比较复杂的操作才会用到,这也是一个使用技巧。