頁面資料繫結全攻略

來源:互聯網
上載者:User

標籤:使用   資料   art   代碼   for   io   

在做Web開發時,都會遇到將背景資料顯示在前台頁面的情況。幾種比較典型的應用執行個體:

方法1:<%#%>

後台代碼:

namespace WebApplication3
{
    public partial class Index : System.Web.UI.Page
    {
        public string name = "ltp";
        public string sex = "man";
        public string old = "25";
        protected void Page_Load(object sender, EventArgs e)
        {
            Page.DataBind();//千萬不能忘記!<%#%>只有Databind()後才有效
        }
    }
}

前台代碼:

<body>
    <form id="form1" runat="server">
    <div>
      <p>姓名:<%# name%></p>
      <p>性別:<%# sex%></p>
      <p>年齡:<%# old%></p>
    </div>
    </form>
</body>

 

方法2:<%=%>

後台代碼:

namespace WebApplication3
{
    public partial class Index2 : System.Web.UI.Page
    {
        public string name = "ltp";
        public string sex = "name";
        public string old = "25";

        protected void Page_Load(object sender, EventArgs e)
        {

        }
    }
}

前台代碼:

<body>
    <form id="form1" runat="server">
    <div>
      <p>姓名:<%=name %></p>
      <p>性別:<%=sex %></p>
      <p>年齡:<%=old %></p>
    </div>
    </form>
</body>

<%=%>與<%#%>區別

<%=%>相當於Response.write().是放變數但是取出變數的值!

<%#%>專門用於資料繫結,可以綁定一些變數或者資料來源中的東西,中間綁定石資料來源的條目。

 

使用DataBinder類進行綁定

DataBinder類最主要的好處是會自動執行類型轉換。

綁定格式:DataBinder.Eval(資料項目的命名容器,資料欄位名稱,格式字串)。

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.