1 / 26

数据绑定控件 ( 上 )

数据绑定控件 ( 上 ). 项目七. 回顾. Global.asax 文件包含常用的Application_Start、Application_End、Session_Start、Session_End等事件 Application 对象是存储于服务器的全局变量 Cookie 存储信息于客户端 Session 对象用于在服务器端存储用户的信息,在用户结束会话时被清除 新用户访问应用程序时会激活 Session_Start 事件,而用户退出应用程序时会触发 Session_End 事件. 目标. 理解数据绑定 使用 DataList 控件 使用数据视图排序和筛选

talon
Download Presentation

数据绑定控件 ( 上 )

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 数据绑定控件 (上) 项目七

  2. 回顾 • Global.asax 文件包含常用的Application_Start、Application_End、Session_Start、Session_End等事件 • Application 对象是存储于服务器的全局变量 • Cookie 存储信息于客户端 • Session 对象用于在服务器端存储用户的信息,在用户结束会话时被清除 • 新用户访问应用程序时会激活 Session_Start 事件,而用户退出应用程序时会触发 Session_End 事件

  3. 目标 • 理解数据绑定 • 使用 DataList 控件 • 使用数据视图排序和筛选 • 在ASP.NET 中使用ADO.NET的事务处理

  4. 数据绑定简介 2-1 数据 FORM 检索到的数据 控件 数据 输出结果 数据绑定是将数据链接到显示该数据的控件的过程 欢迎 “ ”

  5. 数据绑定简介 2-2 用于绑定控件的表达式置于 <%#......%> 标记之间 数据绑定 数据源 简单属性 方法的结果 表达式

  6. 简单属性绑定 代码视图 //定义成员变量 protected static int vote1 = 0; protected static int vote2 = 0; protected static int vote3 = 0; private void btnVote1_Click(object sender, System.EventArgs e) { vote1 += 1; this.DataBind(); } private void btnVote2_Click(object sender, System.EventArgs e) { vote2 += 1; this.DataBind(); } private void btnVote3_Click(object sender, System.EventArgs e) { vote3 += 1; this.DataBind(); } HTML视图 运行结果 <asp:Image id="imgVote1" runat="server" Height="12px" Width="<%#vote1%>" ImageUrl="red.bmp"></asp:Image> <asp:Label id="lblVote1" Text="<%#vote1%>" runat="server"></asp:Label> <asp:Image id="imgVote2" runat="server" Height="12px" Width="<%#vote2%>" ImageUrl="red.bmp"></asp:Image> <asp:Label id="lblVote2" Text="<%#vote2%>" runat="server"></asp:Label> <asp:Image id="imgVote3" runat="server" Height="12px" Width="<%#vote3%>" ImageUrl="red.bmp"></asp:Image> <asp:Label id="lblVote3" Text="<%#vote3%>" runat="server"></asp:Label>

  7. 表达式绑定 代码视图 //定义成员变量 protected static int vote1 = 0; protected static int vote2 = 0; protected static int vote3 = 0; private void btnVote1_Click(object sender, System.EventArgs e) { vote1 += 1; this.DataBind(); } private void btnVote2_Click(object sender, System.EventArgs e) { vote2 += 1; this.DataBind(); } private void btnVote3_Click(object sender, System.EventArgs e) { vote3 += 1; this.DataBind(); } 运行结果 HTML视图 <asp:Image id="imgVote1" runat="server" Height="12px" Width="<%#4*vote1%>" ImageUrl="red.bmp"></asp:Image> <asp:Label id="lblVote1" Text="<%#vote1%>" runat="server"></asp:Label> <asp:Image id="imgVote2" runat="server" Height="12px" Width="<%#4*vote2%>" ImageUrl="red.bmp"></asp:Image> <asp:Label id="lblVote2" Text="<%#vote2%>" runat="server"></asp:Label> <asp:Image id="imgVote3" runat="server" Height="12px" Width="<%#4*vote3%>" ImageUrl="red.bmp"></asp:Image> <asp:Label id="lblVote3" Text="<%#vote3%>" runat="server"></asp:Label>

  8. 方法的结果绑定 建立一个新方法 protected string GetVotePercent(int vote) { int sumVote = vote1 + vote2 + vote3; if (sumVote == 0) { return "0%"; } else { decimal percent = 100 * (Convert.ToDecimal(vote) /Convert.ToDecimal(sumVote)); return percent.ToString("n2") + "%"; } } 调用方法的结果绑定 <asp:Image id="imgVote1" runat="server" Height="12px" Width="<%#vote1%>" ImageUrl="red.bmp"></asp:Image> <asp:Label id="lblVote1" Text="<%#GetVotePercent(vote1)%>" runat="server"></asp:Label> <asp:Image id="imgVote2" runat="server" Height="12px" Width="<%#vote2%>" ImageUrl="red.bmp"></asp:Image> <asp:Label id="lblVote2" Text="<%#GetVotePercent(vote2)%>" runat="server"></asp:Label> <asp:Image id="imgVote3" runat="server" Height="12px" Width="<%#vote3%>" ImageUrl="red.bmp"></asp:Image> <asp:Label id="lblVote3" Text="<%#GetVotePercent(vote3)%>" runat="server"></asp:Label> 运行结果

  9. 使用 DataList 控件显示数据 3-1 项模板 交替项 模板 页脚 模板 使用 Datalist 控件可以指定数据流 Datalist 控件 也可以为 DataList控件设置要显示的数据列数和行数 分隔符 模板 输出结果 页眉 模板 W E L C O M E WELCOME 水平方式 垂直方式 选择项 模板 编辑项 模板

  10. 使用 DataList 控件显示数据 3-2 示例:DatalistDemo.aspx private void Page_Load(object sender, System.EventArgs e) { Response.Write(“<center><b><u>带有交替列的数据列表</center>” + “</b></u><br>"); if(!IsPostBack) { DataTable mydt = new DataTable(); DataRow mydr; mydt.Columns.Add(new DataColumn("Numbers“ ,typeof(Int32))); mydt.Columns.Add(new DataColumn("Squares“ ,typeof(Int32))); mydt.Columns.Add(new DataColumn("Cubes“ ,typeof(Int32))); 续… for (int i=0;i<30;i++) { mydr = mydt.NewRow(); mydr[0] = i; mydr[1] = i*i; mydr[2] = i*i*i; mydt.Rows.Add(mydr); } dlMyList.DataSource = mydt; dlMyList.DataBind(); } } 为 DataList控件指定数据源 将数据绑定到 DataList

  11. 使用 DataList 控件显示数据 3-3 示例的 HTML视图 输出结果 <asp:DataList id="dlMyList" RepeatDirection="Horizontal" RepeatColumns="10" runat="server"> <ItemTemplate> <%# DataBinder.Eval(Container.DataItem, "Numbers") %> <br> <%# DataBinder.Eval(Container.DataItem, "Squares") %> <br> <%# DataBinder.Eval(Container.DataItem, "Cubes") %> <br> </ItemTemplate> <AlternatingItemTemplate> <i><b> <%# DataBinder.Eval(Container.DataItem, "Numbers") %> </b><i> <br> <i><b> <%# DataBinder.Eval(Container.DataItem , "Squares") %> <b><i> <br> <i><b> <%# DataBinder.Eval(Container.DataItem, "Cubes") %> <b><i> <br> </AlternatingItemTemplate> </asp:DataList>

  12. DataBinder.Eval 方法 DataBinder.Eval方法用于在运行时计算 数据绑定表达式,并按照要求格式化输出结果 此方法有三个参数 命名容器 对为其表达式求值的对象的引用 数据字段名 数据表中的数据列名称 格式字符串 用于显示指定格式的值

  13. 使用 DataView 控件 3-1 DataView 用于呈现 DataTable中的数据的自定义视图 职员数据 AGE DESIG S No NAME Age > 55 Data View GM MD PL GM MD TL 1. 2. 3. 4. 2. 3. DEF LMN ABC DEF LMN XYG 27 60 57 33 60 57 S No NAME AGE DESIG

  14. 使用 DataView 控件 3-2 employee 表的 DataView控件仅检索年龄大于 55 的职员的记录 mydv = new DataView (ds.Tables ["employee"]); mydv.RowFilter = "age > 55"; Sort 属性用于以递增或递减顺序为行排序 mydv.Sort = "fname ASC"; 递增排列

  15. 使用 DataView 控件 3-3 DataView 控件可以限制 employee 表中显示的行 输出结果 private void Page_Load(object sender, System.EventArgs e) { Response.Write(“<center><b><u>数据视图</center></b></u> <br>"); SqlConnection objSqlConnection = new SqlConnection("server=vijayk;uid=sa;pwd=playware;database=pubs"); SqlDataAdapter objSqlAdapter = new SqlDataAdapter( "select * from employee where job_id = 5", objSqlConnection); DataSet objDataSet = new DataSet(); objSqlAdapter.Fill(objDataSet, "employee"); DataView objDataView = new DataView (objDataSet.Tables ["employee"]); objDataView.RowFilter = "job_lvl >180"; objDataView.Sort = "fname ASC"; dgMyGrid.DataSource=objDataView; dgMyGrid.DataBind(); }

  16. 事务处理 5-1 要启用 ASP.NET 页面中的事务处理,请将事务处理指令添加到 ASPX页面 数据库级 可以在以下级别 创建事务处理 ADO.NET 级 ASP.NET页面级 ASP.NET中可用的事务处理指令

  17. 事务处理 5-2 ASPTransactionsDemo.aspx’ private void btnTransfer_Click(object sender, System.EventArgs e) { lblAccount1.Text=" "; lblAccount2.Text=" "; int CurrBalance; string strSQL = "Select Balance FROM Account where AccNo='"+txtFrom.Text+"'" ; SqlConnection objSqlConnection = new SqlConnection( "server=SQLDB;uid=sa;pwd=password;” + ”database=Account"); objSqlConnection.Open(); SqlDataReader objReader; SqlCommand objSqlCommand = new SqlCommand(strSQL ,objSqlConnection); try { 续.. Transaction = “RequiresNew” 指令 已添加到页面,确保Asp 页面上的操作 将于新的事务处理开始 导入 System.EnterpriseServices命名空间,使 ContextUtil类可用于实现事务处理的提交和放弃方法

  18. 事务处理 5-3 objReader = objSqlCommand.ExecuteReader(); objReader.Read(); CurrBalance = Convert.ToInt32(objReader.GetValue(0)); objReader.Close(); if (CurrBalance < Convert.ToInt32(txtAmount.Text)) { throw(new Exception(“转帐金额不足")); } strSQL = "Update Account set Balance = Balance - " + txtAmount.Text + " where AccNo = '" + Convert.ToInt32(txtFrom.Text) + "'"; objSqlCommand.CommandText=strSQL; objSqlCommand.ExecuteNonQuery();

  19. 事务处理 5-4 lblAccount1.Text=“帐户 " + txtFrom.Text +" 成功记入借方"; strSQL = "Update Account set Balance =Balance + "+ txtAmount.Text + " where AccNo = '" + Convert.ToInt32(txtTo.Text) + "'"; objSqlCommand.CommandText = strSQL;objSqlCommand.ExecuteNonQuery(); lblAccount2.Text=“帐户 ” + txtTo.Text +“ 成功记入贷方"; ContextUtil.SetComplete(); lblException.Text = “成功将金额 " + txtAmount.Text + “ 从帐户 ” + txtFrom.Text + “ 转帐到帐户 ” + txtTo.Text + “。";; } catch(Exception ex) 续.. 在所有事务处理均成功的事件中提交事务处理

  20. 事务处理 5-5 输出结果 { ContextUtil.SetAbort(); lblException.Text = “错误:" + ex.Message; } finally { objSqlConnection.Close(); } } 在任何单项事务处理失败的事件中放弃所有事务处理

  21. 在ADO.NET级实现事务处理 4-1 ‘ADOTransactionDemo.aspx’ private void btnTransfer_Click(object sender, System.EventArgs e) { lblAccount1.Text=" "; lblAccount2.Text=" "; int CurrBalance; string strSQL = "Select Balance FROM Account where AccNo='“ +txtFrom.Text+"'" ; SqlConnection objSqlConnection = new SqlConnection("server=VIJAYK;uid=sa;pwd=playware;database=Account"); objSqlConnection.Open(); SqlDataReader objReader; SqlCommand objSqlCommand = new SqlCommand(strSQL ,objSqlConnection); Cont.. 导入命名空间 System.Data.SqlClient,并且在 在 btnTransfer按钮的 Click事件中添加代码

  22. 在ADO.NET级实现事务处理 4-2 SqlTransaction objSqlTransaction =objSqlConnection. BeginTransaction(); objSqlCommand.Transaction=objSqlTransaction; try { objReader = objSqlCommand.ExecuteReader(); objReader.Read(); CurrBalance = Convert.ToInt32(objReader.GetValue(0)); objReader.Close(); if (CurrBalance < Convert.ToInt32(txtAmount.Text)) { throw(new Exception(“转帐金额不足")); } 续..

  23. 在ADO.NET级实现事务处理 4-3 strSQL = "Update Account set Balance = Balance - " + txtAmount.Text + " where AccNo = '" + Convert.ToInt32(txtFrom.Text) + "'"; objSqlCommand.CommandText=strSQL; objSqlCommand.ExecuteNonQuery(); lblAccount1.Text=“帐户 ” + txtFrom.Text +“成功记入借方"; strSQL = "Update Account set Balance =Balance + "+ txtAmount.Text + " where AccNo = '" + Convert.ToInt32(txtTo.Text) + "'"; objSqlCommand.CommandText = strSQL; objSqlCommand.ExecuteNonQuery();

  24. 在ADO.NET级实现事务处理 4-4 输出结果 lblAccount2.Text=“帐户” + txtTo.Text +“成功记入贷方"; objSqlTransaction.Commit(); lblException.Text = “成功将金额" + txtAmount.Text + “ 从帐户 ” + txtFrom.Text + “转入帐户 ” + txtTo.Text+ “。";; } catch(Exception ex) { objSqlTransaction.Rollback(); lblException.Text = "Error: " + ex.Message; } finally { objSqlConnection.Close(); } } 完成事务处理时提交变更 事务处理不成功时恢复变更

  25. 总结 2-1 • 数据绑定是将从数据库中检索到的数据链接到将显示这些数据的控件的过程 • 可对多种类型的数据执行数据绑定,例如: • 表达式 • 数据库数据 • Databinder.Eval 方法 • DataGrid、DataList 和 Repeater 等控件可用于连接数据库并实现对数据库数据的进行数据绑定 • Databinder.Eval 方法用于计算运行时的数据绑定表达式,并按照浏览器显示的要求来格式化输出结果

  26. 总结 2-2 • DataList 控件可用于显示数据,而且支持指定数据流 • 模板用于将数据显示在浏览器和 DataList 控件中 • DataView 可用于自定义 DataTable 中数据的显示。DataView 中的数据可进行筛选、搜索和排列 • ADO.NET 级的事务处理可以通过 SqlTransaction 类的方法来应用。可通过使用事务处理指令来指示页面上的事务处理支持级

More Related