談談asp.net中的<% %>,<%= %>,<%# %><%$ %>的使用

來源:互聯網
上載者:User

標籤:style   blog   http   color   使用   檔案   

學而不思則罔,思而不學則殆,每天堅持一小步,則成功一大步

asp.net中的<% %>,<%= %>,<%#eval("") %><%$ %>的使用

下面來看一下asp.net中的<% %>,<%= %>,><%# %><%$ %>的使用

首先我們來看一下<% %>的使用

在aspx的頁面中只能使用伺服器控制項和一般的控制項,有些時候你想在該頁面寫入c#代碼,必須使用<% %>,然後在裡面寫入c#的代碼,下面我們來看一例子

<form id="form1" runat="server">    <div>       <%           Response.Write("hello,world");        %>     </div></form>

這樣就可以把該代碼答應顯示到頁面上了。該代碼就如

<html><head></head><body><p>hello ,world</p></body></html>

 我們繼續來看一下<%= %>

這是用來從後台頁面傳值到前台頁面所使用的,就是在前台調用後台變數或參數所使用,前台代碼如下:

 <form id="form1" runat="server">     <div>    <%=name %>     </div> </form>

後台代碼如下:

public partial class index : System.Web.UI.Page{    public String name;    protected void Page_Load(object sender, EventArgs e)    {          name= "haha";    }}

接下來我們來看一下<%# %>

這是一個資料控制項綁定顯示使用的,有多種顯示方式

1.<%# Eval("查詢出的欄位")%>例如

<asp:DataList ID="DataList1" runat="server" DataSourceID="SqlDataSource1">            <ItemTemplate>                _id:                <asp:Label ID="_idLabel" runat="server" Text=‘<%# Eval("_id") %>‘ />                <br />                _name:                <asp:Label ID="_nameLabel" runat="server" Text=‘<%# Eval("_name") %>‘ />                <br />                _sex:                <asp:Label ID="_sexLabel" runat="server" Text=‘<%# Eval("_sex") %>‘ />                <br />                _tel:                <asp:Label ID="_telLabel" runat="server" Text=‘<%# Eval("_tel") %>‘ />                <br /><br />            </ItemTemplate>        </asp:DataList>

2,<%#Bind("")%>資料來源繫結控制項的Formview的資料顯示與綁定,代碼如下

<asp:FormView ID="FormView1" runat="server" DataSourceID="SqlDataSource1">            <EditItemTemplate>                _id:                <asp:Label ID="_idLabel1" runat="server" Text=‘<%# Eval("_id") %>‘ />                <br />                _name:                <asp:TextBox ID="_nameTextBox" runat="server" Text=‘<%# Bind("_name") %>‘ />                <br />                _sex:                <asp:CheckBox ID="_sexCheckBox" runat="server" Checked=‘<%# Bind("_sex") %>‘ />                <br />                _tel:                <asp:TextBox ID="_telTextBox" runat="server" Text=‘<%# Bind("_tel") %>‘ />                <br />                <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True"                     CommandName="Update" Text="更新" />                &nbsp;<asp:LinkButton ID="UpdateCancelButton" runat="server"                     CausesValidation="False" CommandName="Cancel" Text="取消" />            </EditItemTemplate>            <InsertItemTemplate>                _name:                <asp:TextBox ID="_nameTextBox" runat="server" Text=‘<%# Bind("_name") %>‘ />                <br />                _sex:                <asp:CheckBox ID="_sexCheckBox" runat="server" Checked=‘<%# Bind("_sex") %>‘ />                <br />                _tel:                <asp:TextBox ID="_telTextBox" runat="server" Text=‘<%# Bind("_tel") %>‘ />                <br />                <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True"                     CommandName="Insert" Text="插入" />                &nbsp;<asp:LinkButton ID="InsertCancelButton" runat="server"                     CausesValidation="False" CommandName="Cancel" Text="取消" />            </InsertItemTemplate>            <ItemTemplate>                _id:                <asp:Label ID="_idLabel" runat="server" Text=‘<%# Eval("_id") %>‘ />                <br />                _name:                <asp:Label ID="_nameLabel" runat="server" Text=‘<%# Bind("_name") %>‘ />                <br />                _sex:                <asp:CheckBox ID="_sexCheckBox" runat="server" Checked=‘<%# Bind("_sex") %>‘                     Enabled="false" />                <br />                _tel:                <asp:Label ID="_telLabel" runat="server" Text=‘<%# Bind("_tel") %>‘ />                <br />            </ItemTemplate>        </asp:FormView>

最後我們來看一下很少用到的<%$ %>

這個代碼很少見,但是在使用多語言轉換的時候能夠用到,就是必須先建立全域資源和本地資源,還有就是在設定檔中配置如下代碼,然後調用設定檔。代碼如下

設定檔代碼

configuration>  <appSettings>    <add key="connect" value="hello"/>  </appSettings>    <system.web>        <compilation debug="false" targetFramework="4.0" />    </system.web>  </configuration>

 配置調用代碼

<form id="form1" runat="server">    <div>       <%-- <asp:Label ID="Label1" runat="server" Text="<%$ Resources:age %>"></asp:Label>--%>       <asp:Literal ID="Literal1" runat="server" Text="<%$ Resources:Default.aspx,name%>" />       <asp:Literal ID="Literal2" runat="server" Text="<%$ Resources:Default.aspx,age%>" />        <asp:Label ID="heh" runat="server" Text="<%$ appSettings:connect %>"></asp:Label>    </div>

全部代碼下載

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.