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


原文:https://www.cnblogs.com/Hackerman/p/3857630.html

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

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

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

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

?
1
2
3
4
5
6
7
< 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>
復制代碼

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

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

配置文件代碼

?
1
2
3
4
5
6
7
8
9
10
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>
復制代碼


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM