asp.net <%%>用法

发布时间:2018-10-10编辑:佚名阅读(1383)

一、<%%>

这种格式实际上就是和asp的用法一样的,只是asp中里面是vbscript或者javascript代码,而在asp.net中是.net平台下支持的语言。特别注意:服务器控件中不能有<%%>语法。
(这里用C#代码)

<%
    int a = 2;
    int b = 3;
    int c = a + b;
    Response.Write(c);
%>

结果:

二、<%#%>和<%=%>

aspx代码:

Server Control:<asp:TextBox ID="TextBox1" runat="server" Text="<%#text%>"></asp:TextBox><br />
<!--Server Control-->
Client Control:<input type="text" id="textbox2" value="<%=text%>" /><!--Client Control-->
<br />
<label id="label1"><%=DisplayStr()%></label>
<br />
<label id="label2" runat="server"><%=DisplayStr()%></label>

aspx.cs代码:

protected string text;//注意这里必须申明为public或protected,否则aspx页面(子类)无法访问
protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        this.text = "aaaaaaaaaaaaa";
        this.TextBox1.DataBind();//或this.DataBind();              
    }
}
public string DisplayStr()//注意这里必须要有返回值,否则将会发生运行时错误
{
    return "bbbb";
}

结果:

三、<%$%>
这种形式主要用于对web.config文件的键值对进行绑定:通常用于连接数据库的字符串。特别注意:

  1. 绑定的只能是服务器控件。

  2. 只能绑定到服务器控件的某个属性上。

<asp:TextBox runat="server" ID="cc" Text="<%$ConnectionStrings:pubs%>"></asp:TextBox>

web.config文件如下:

<connectionStrings>
    <add name="pubs" connectionString="Server=.;database=pubs;uid=sa;pwd=123" providerName="System.Data.SqlClient"/>
</connectionStrings>

结果:

  关键字:asp.net <%%>用法


鼓掌

0

正能量

0

0

呵呵

0


评论区