aspx页代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DataBind.aspx.cs" Inherits="DataBind" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<html xmlns="http://www.w3.org/1999/xhtml " >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
当前的系统时间是:<%# GetSystemTime()%><br />
用户名:<%# UserName%><br />
博客地址:<%# BlogUrl%>
</div>
</form>
</body>
</html>
对应的代码隐藏页代码:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class DataBind : System.Web.UI.Page
{
public string UserName = "四毛";
protected string BlogUrl = "http://hi.baidu.com ";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Page.DataBind();
}
}
public string GetSystemTime()
{
string time = System.DateTime.Now.ToString();
return time;
}
}
技术说明:
用<%# %>在html里面直接绑定显示后台中的变量,方法。注意这里要绑定的变量,方法的访问修饰符最能是public 或protected。如果没有红色标识的代码,数据是无法绑定到页面的。