ASP.NET的简单数据绑定
2006-10-08 19:59
323 查看
ASP.NET也可以将代码前置,不借助VS.NET的代码提示功能,也许能体会到更多的细节吧。简单数据绑定的关键一点就是利用<%# ... %>来调用函数,下图是要实现的功能:
代码如下:
<%...@ Page Language="C#" ContentType="text/html" ResponseEncoding="gb2312" Debug="true"%>
<%...@ Import Namespace="System.Data" %>
<%...@ Import Namespace="System.Data.SqlClient" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>简单数据绑定</title>
<style type="text/css">...
h1,div {...}{text-align:center; font-family:宋体; font-size:x-large; color:#CC0000}
table {...}{font-size:14px; color:black}
.stdTextBox {...}{text-align:center; background-color:#CCCCCC; color:#990000}
</style>
</head>
<body bgcolor="ivory">
<form runat="server">
<h1>当前路径:<asp:Label runat="server" ID="lblPath"/></h1>
<div>
<table align="center">
<tr>
<th>编号</th><th>姓名</th><th>城市</th></tr>
<td><asp:TextBox ID="txtID" CssClass="stdTextBox" runat="server" Text='<%#getData("CustomerID") %>'/></td>
<td><asp:TextBox ID="txtName" CssClass="stdTextBox" runat="server" Text='<%#getData("CompanyName") %>'/></td>
<td><asp:TextBox ID="txtTeam" CssClass="stdTextBox" runat="server" Text='<%#getData("City") %>'/></td>
</table>
<asp:LinkButton ID="btnPrev" Font-Size="17px" runat="server" Text="<<" onClick="movePrev"/>
<asp:LinkButton ID="btnNext" Font-Size="17px" runat="server" Text=">>" onClick="moveNext"/>
</div>
</form>
<script language="c#" runat="server">...
public void Page_Load(Object sender,EventArgs e)
...{
this.lblPath.Text = Request.Url.ToString();
if(!IsPostBack)
...{
string strConn = "server=.;database=Northwind;uid=sa;pwd=millfox";
DataSet ds = new DataSet();
string strSelect = "SELECT CustomerID,CompanyName,City FROM Customers";
SqlDataAdapter cmd = new SqlDataAdapter(strSelect,strConn);
cmd.Fill(ds,"tempTable");
this.txtID.Text = ds.Tables["tempTable"].Rows[0]["CustomerID"].ToString();
this.txtName.Text = ds.Tables["tempTable"].Rows[0]["CompanyName"].ToString();
this.txtTeam.Text = ds.Tables["tempTable"].Rows[0]["City"].ToString();
Session["currentRecord"] = 0;
Session["recordCount"] = ds.Tables["tempTable"].Rows.Count-1;
Session["myDs"] = ds;
ReFresh();
}
}
public void ReFresh()
...{
this.txtID.DataBind();
this.txtName.DataBind();
this.txtTeam.DataBind();
}
public void moveNext(Object sender,EventArgs e)
...{
int i = (int)Session["currentRecord"];
int total = (int)Session["recordCount"];
if(i<total)
...{
Session["currentRecord"] = i+1;
ReFresh();
}
}
public void movePrev(Object sender,EventArgs e)
...{
int i = (int)Session["currentRecord"];
if(i>0)
...{
Session["currentRecord"] = i-1;
ReFresh();
}
}
public string getData(string colName)
...{
DataSet ds = (DataSet)Session["myDs"];
int i = (int)Session["currentRecord"];
string temp = ds.Tables["tempTable"].Rows[i][colName].ToString();
return temp;
}
</script>
</body>
</html>
代码如下:
<%...@ Page Language="C#" ContentType="text/html" ResponseEncoding="gb2312" Debug="true"%>
<%...@ Import Namespace="System.Data" %>
<%...@ Import Namespace="System.Data.SqlClient" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>简单数据绑定</title>
<style type="text/css">...
h1,div {...}{text-align:center; font-family:宋体; font-size:x-large; color:#CC0000}
table {...}{font-size:14px; color:black}
.stdTextBox {...}{text-align:center; background-color:#CCCCCC; color:#990000}
</style>
</head>
<body bgcolor="ivory">
<form runat="server">
<h1>当前路径:<asp:Label runat="server" ID="lblPath"/></h1>
<div>
<table align="center">
<tr>
<th>编号</th><th>姓名</th><th>城市</th></tr>
<td><asp:TextBox ID="txtID" CssClass="stdTextBox" runat="server" Text='<%#getData("CustomerID") %>'/></td>
<td><asp:TextBox ID="txtName" CssClass="stdTextBox" runat="server" Text='<%#getData("CompanyName") %>'/></td>
<td><asp:TextBox ID="txtTeam" CssClass="stdTextBox" runat="server" Text='<%#getData("City") %>'/></td>
</table>
<asp:LinkButton ID="btnPrev" Font-Size="17px" runat="server" Text="<<" onClick="movePrev"/>
<asp:LinkButton ID="btnNext" Font-Size="17px" runat="server" Text=">>" onClick="moveNext"/>
</div>
</form>
<script language="c#" runat="server">...
public void Page_Load(Object sender,EventArgs e)
...{
this.lblPath.Text = Request.Url.ToString();
if(!IsPostBack)
...{
string strConn = "server=.;database=Northwind;uid=sa;pwd=millfox";
DataSet ds = new DataSet();
string strSelect = "SELECT CustomerID,CompanyName,City FROM Customers";
SqlDataAdapter cmd = new SqlDataAdapter(strSelect,strConn);
cmd.Fill(ds,"tempTable");
this.txtID.Text = ds.Tables["tempTable"].Rows[0]["CustomerID"].ToString();
this.txtName.Text = ds.Tables["tempTable"].Rows[0]["CompanyName"].ToString();
this.txtTeam.Text = ds.Tables["tempTable"].Rows[0]["City"].ToString();
Session["currentRecord"] = 0;
Session["recordCount"] = ds.Tables["tempTable"].Rows.Count-1;
Session["myDs"] = ds;
ReFresh();
}
}
public void ReFresh()
...{
this.txtID.DataBind();
this.txtName.DataBind();
this.txtTeam.DataBind();
}
public void moveNext(Object sender,EventArgs e)
...{
int i = (int)Session["currentRecord"];
int total = (int)Session["recordCount"];
if(i<total)
...{
Session["currentRecord"] = i+1;
ReFresh();
}
}
public void movePrev(Object sender,EventArgs e)
...{
int i = (int)Session["currentRecord"];
if(i>0)
...{
Session["currentRecord"] = i-1;
ReFresh();
}
}
public string getData(string colName)
...{
DataSet ds = (DataSet)Session["myDs"];
int i = (int)Session["currentRecord"];
string temp = ds.Tables["tempTable"].Rows[i][colName].ToString();
return temp;
}
</script>
</body>
</html>
相关文章推荐
- ASP.NET中DropDownList控件的简单使用--DDL绑定数据库表中值,选择DDL中值直接检索数据
- 【翻译】使用Knockout, Web API 和 ASP.Net Web Forms 进行简单数据绑定
- ASP.NET基础04_简单数据绑定与App_Offline.htm文件
- asp.net Xml绑定到数据控件的两种简单方法
- ASP.NET的简单数据绑定
- Knockout, Web API 和 ASP.Net Web Forms 进行简单数据绑定
- ASP.NET中DropDownList控件的简单使用--DDL绑定数据库表中值,选择DDL中值直接检索数据
- 一步一步学会在ASP.NET 4数据绑定控件中启用动态数据(翻译)
- Asp.Net中几种相似数据绑定标记符号的解释及用法
- ASP.NET页面的数据绑定
- asp.net高级开发之数据绑定
- ASP.NET 数据绑定常用代码
- ASP.NET中的数据绑定:哪个更快
- 数据验证ASP.NET 简单使用
- asp.net Repeater 数据绑定代码
- 如何应用Asp.Net Mvc内建功能(DefaultModelBinder)实现简单类型、复杂类型、集合类型,以及字典类型的自动绑定
- asp.net 数据绑定 -- 时间格式
- asp.net 数据绑定到属性、集合、表达式、方法
- ASP.NET 3.5核心编程学习笔记(18):数据绑定表达式
- ASP.NET 数据绑定 Web 服务器控件概述