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 >
<% ... @ 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 >