数据绑定控件 (一)_第1页
数据绑定控件 (一)_第2页
数据绑定控件 (一)_第3页
数据绑定控件 (一)_第4页
数据绑定控件 (一)_第5页
已阅读5页,还剩21页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

数据绑定控件(一)

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简单属性绑定<asp:Imageid="imgVote1"runat="server"Height="12px"Width="<%#vote1%>"ImageUrl="red.bmp"></asp:Image><asp:Labelid="lblVote1"Text="<%#vote1%>"runat="server"></asp:Label><asp:Imageid="imgVote2"runat="server"Height="12px"Width="<%#vote2%>"ImageUrl="red.bmp"></asp:Image><asp:Labelid="lblVote2"Text="<%#vote2%>"runat="server"></asp:Label><asp:Imageid="imgVote3"runat="server"Height="12px"Width="<%#vote3%>"ImageUrl="red.bmp"></asp:Image><asp:Labelid="lblVote3"Text="<%#vote3%>"runat="server"></asp:Label>代码视图//定义成员变量protectedstaticintvote1=0;protectedstaticintvote2=0;protectedstaticintvote3=0;privatevoidbtnVote1_Click(objectsender,System.EventArgse){ vote1+=1; this.DataBind();}privatevoidbtnVote2_Click(objectsender,System.EventArgse){ vote2+=1; this.DataBind();}privatevoidbtnVote3_Click(objectsender,System.EventArgse){ vote3+=1; this.DataBind();}HTML视图运行结果7表达式绑定<asp:Imageid="imgVote1"runat="server"Height="12px"Width="<%#4*vote1%>"ImageUrl="red.bmp"></asp:Image><asp:Labelid="lblVote1"Text="<%#vote1%>"runat="server"></asp:Label><asp:Imageid="imgVote2"runat="server"Height="12px"Width="<%#4*vote2%>"ImageUrl="red.bmp"></asp:Image><asp:Labelid="lblVote2"Text="<%#vote2%>"runat="server"></asp:Label><asp:Imageid="imgVote3"runat="server"Height="12px"Width="<%#4*vote3%>"ImageUrl="red.bmp"></asp:Image><asp:Labelid="lblVote3"Text="<%#vote3%>"runat="server"></asp:Label>代码视图HTML视图运行结果//定义成员变量protectedstaticintvote1=0;protectedstaticintvote2=0;protectedstaticintvote3=0;privatevoidbtnVote1_Click(objectsender,System.EventArgse){ vote1+=1; this.DataBind();}privatevoidbtnVote2_Click(objectsender,System.EventArgse){ vote2+=1; this.DataBind();}privatevoidbtnVote3_Click(objectsender,System.EventArgse){ vote3+=1; this.DataBind();}8方法的结果绑定protectedstringGetVotePercent(intvote){ intsumVote=vote1+vote2+vote3; if(sumVote==0) { return"0%"; } else { decimalpercent=100*(Convert.ToDecimal(vote) /Convert.ToDecimal(sumVote)); returnpercent.ToString("n2")+"%"; }

}建立一个新方法<asp:Imageid="imgVote1"runat="server"Height="12px"Width="<%#vote1%>"ImageUrl="red.bmp"></asp:Image><asp:Labelid="lblVote1"Text="<%#GetVotePercent(vote1)%>"runat="server"></asp:Label><asp:Imageid="imgVote2"runat="server"Height="12px"Width="<%#vote2%>"ImageUrl="red.bmp"></asp:Image><asp:Labelid="lblVote2"Text="<%#GetVotePercent(vote2)%>"runat="server"></asp:Label><asp:Imageid="imgVote3"runat="server"Height="12px"Width="<%#vote3%>"ImageUrl="red.bmp"></asp:Image><asp:Labelid="lblVote3"Text="<%#GetVotePercent(vote3)%>"runat="server"></asp:Label>调用方法的结果绑定运行结果9使用DataList控件显示数据3-1使用Datalist控件可以指定数据流输出结果WELCOMEWELCOME水平方式垂直方式Datalist控件项模板交替项模板页脚模板页眉模板编辑项模板选择项模板分隔符模板也可以为DataList控件设置要显示的数据列数和行数10使用DataList控件显示数据3-2示例:DatalistDemo.aspxprivatevoidPage_Load(objectsender,System.EventArgse){

Response.Write(“<center><b><u>带有交替列的数据列表</center>” +“</b></u><br>");

if(!IsPostBack) {

DataTablemydt=newDataTable();

DataRowmydr;

mydt.Columns.Add(newDataColumn("Numbers“ ,typeof(Int32)));

mydt.Columns.Add(newDataColumn("Squares“ ,typeof(Int32)));

mydt.Columns.Add(newDataColumn("Cubes“ ,typeof(Int32)));

续… for(inti=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控件指定数据源将数据绑定到DataList11使用DataList控件显示数据3-3示例的HTML视图<asp:DataListid="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>输出结果12DataBinder.Eval方法DataBinder.Eval方法用于在运行时计算数据绑定表达式,并按照要求格式化输出结果此方法有三个参数对为其表达式求值的对象的引用格式字符串数据表中的数据列名称用于显示指定格式的值命名容器数据字段名13使用DataView控件3-1DataView用于呈现DataTable中的数据的自定义视图NAMESNoDESIGAGE1.2.3.4.ABCDEFLMNXYG27605733PLGMMDTL职员数据NAMESNoDESIGAGE2.3.DEFLMN6057GMMDDataViewAge>5514使用DataView控件3-2employee表的DataView控件仅检索年龄大于55的职员的记录mydv=newDataView(ds.Tables["employee"]);mydv.RowFilter="age>55";Sort属性用于以递增或递减顺序为行排序mydv.Sort="fnameASC";递增排列15使用DataView控件3-3DataView控件可以限制employee表中显示的行privatevoidPage_Load(objectsender,System.EventArgse){

Response.Write(“<center><b><u>数据视图</center></b></u><br>");

SqlConnectionobjSqlConnection=new SqlConnection("server=vijayk;uid=sa;pwd=playware;database=pubs");

SqlDataAdapterobjSqlAdapter=newSqlDataAdapter( "select*fromemployeewherejob_id=5",objSqlConnection);

DataSetobjDataSet=newDataSet();

objSqlAdapter.Fill(objDataSet,"employee");

DataViewobjDataView=newDataView(objDataSet.Tables["employee"]);

objDataView.RowFilter="job_lvl>180";

objDataView.Sort="fnameASC";

dgMyGrid.DataSource=objDataView;

dgMyGrid.DataBind();}输出结果16事务处理5-1ASP.NET中可用的事务处理指令指令描述Disabled这是默认值,表示禁止在事务处理中执行页面NotSupported表示不管事务处理是活动还是闲置,页面都不会在事务处理中执行

Supported这表示页面将在已存在的事务处理上下文中执行。但是,如果不存在事务处理,则将不会新建一个事务处理Required设定此指令后,页面将在已存在的事务处理上下文中执行。如果不存在事务处理,则会新建一个事务处理RequriesNew当页面请求一项事务处理时,此指令为每个请求新建一个事务处理数据库级ADO.NET级可以在以下级别创建事务处理ASP.NET页面级要启用ASP.NET页面中的事务处理,请将事务处理指令添加到ASPX页面17privatevoidbtnTransfer_Click(objectsender,System.EventArgse){

lblAccount1.Text="";

lblAccount2.Text="";

intCurrBalance;

stringstrSQL="SelectBalanceFROMAccountwhereAccNo='"+txtFrom.Text+"'";

SqlConnectionobjSqlConnection=newSqlConnection( "server=SQLDB;uid=sa;pwd=password;” +”database=Account");

objSqlConnection.Open();

SqlDataReaderobjReader;

SqlCommandobjSqlCommand=newSqlCommand(strSQL ,objSqlConnection);

try {

续..导入System.EnterpriseServices命名空间,使ContextUtil类可用于实现事务处理的提交和放弃方法ASPTransactionsDemo.aspx’Transaction=“RequiresNew”指令已添加到页面,确保Asp页面上的操作将于新的事务处理开始事务处理5-218 objReader=objSqlCommand.ExecuteReader();

objReader.Read();

CurrBalance=Convert.ToInt32(objReader.GetValue(0));

objReader.Close();

if(CurrBalance<Convert.ToInt32(txtAmount.Text)) {

throw(newException(“转帐金额不足")); }

strSQL="UpdateAccountsetBalance=Balance-" +txtAmount.Text+"whereAccNo='" +Convert.ToInt32(txtFrom.Text)+"'";

objSqlCommand.CommandText=strSQL;

objSqlCommand.ExecuteNonQuery();

事务处理5-319

lblAccount1.Text=“帐户"+txtFrom.Text+"

成功记入借方";

strSQL="UpdateAccountsetBalance=

Balance+"+txtAmount.Text+"whereAccNo='"+Convert.ToInt32(txtTo.Text)+"'";

objSqlCommand.CommandText=strSQL;

objSqlCommand.ExecuteNonQuery();

lblAccount2.Text=“帐户”+txtTo.Text+“成功记入贷方";

ContextUtil.SetComplete();

lblException.Text=“成功将金额"+txtAmount.Text+“从帐户”+txtFrom.Text +“转帐到帐户”+txtTo.Text+“。";; }

catch(Exceptionex)

续..在所有事务处理均成功的事件中提交事务处理事务处理5-420

{

ContextUtil.SetAbort();

lblException.Text=“错误:"+ex.Message; }

finally {

objSqlConnection.Close(); }} 输出结果在任何单项事务处理失败的事件中放弃所有事务处理事务处理5-521在ADO.NET级实现事务处理4-1privatevoidbtnTransfer_Click(objectsender,System.EventArgse){

lblAccount1.Text="";

lblAccount2.Text="";

intCurrBalance;

stringstrSQL="SelectBalanceFROMAccountwhereAccNo='“ +txtFrom.Text+"'";

SqlConnectionobjSqlConnection=new SqlConnection("server=VIJAYK;uid=sa;pwd=playware;database=Account");

objSqlConnection.Open();

SqlDataReaderobjReader;

SqlCommandobjSqlCommand=newSqlCommand(strSQL ,objSqlConnection);

Cont..‘ADOTransactionDemo.aspx’导入命名空间System.Data.SqlClient,并且在在btnTransfer按钮的Click事件中添加代码22SqlTransactionobjSqlTransaction=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(newException(“转帐金额不足")); }

续..在ADO.NET级实现事务处理4-223strSQL="UpdateAccountsetBalance=Balance-"+txtAmount.Text +"whereAccNo='"+Convert.ToInt32(txtFrom.Text)+"'";objSqlCommand.CommandText=strSQL;objSqlCommand.ExecuteNonQuery();

lblAccount1.Text=

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论