你基本上是正確的關於<%=...%>
語法。 這裏是引擎蓋下發生的事情爲例:
<script runat="server">
public string SomeProperty { get { return "Hello World!"; } }
</script>
<form id="form1" runat="server">
<%= SomeProperty %>
<div>
<asp:Label ID="Label1" runat="server" Text="<%= SomeProperty %>"></asp:Label>
</div>
</form>
這是解析,下面的C#代碼創建(我已經簡化了一點):
private Label @__BuildControlLabel1()
{
Label @__ctrl = new Label();
this.Label1 = @__ctrl;
@__ctrl.ApplyStyleSheetSkin(this);
@__ctrl.ID = "Label1";
@__ctrl.Text = "<%= SomeProperty %>";
return @__ctrl;
}
private void @__Renderform1(HtmlTextWriter @__w, Control parameterContainer)
{
@__w.Write(SomeProperty);
@__w.Write("\r\n <div>\r\n ");
parameterContainer.Controls[0].RenderControl(@__w);
@__w.Write("\r\n </div>\r\n ");
}
下面是一個例子引擎蓋下會發生什麼了<%#...%>
語法:
<script runat="server">
public string SomeProperty { get { return "Hello World!"; } }
protected void Page_Load(object sender, EventArgs e) { Label1.DataBind(); }
</script>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="<%# SomeProperty %>"></asp:Label>
</div>
</form>
生成此代碼:
private Label @__BuildControlLabel1()
{
Label @__ctrl = new Label();
this.Label1 = @__ctrl;
@__ctrl.ApplyStyleSheetSkin(this);
@__ctrl.ID = "Label1";
@__ctrl.DataBinding += new System.EventHandler([email protected]__DataBindingLabel1);
return @__ctrl;
}
public void @__DataBindingLabel1(object sender, EventArgs e)
{
Label dataBindingExpressionBuilderTarget = ((Label)(sender));
Page Container = ((Page)(dataBindingExpressionBuilderTarget.BindingContainer));
dataBindingExpressionBuilderTarget.Text = System.Convert.ToString(SomeProperty , System.Globalization.CultureInfo.CurrentCulture);
}
正如您所看到的,<%=...%>
語法可用於服務器控件的屬性之外,以直接呈現返回的值。另一方面,<%#...%>
語法爲標籤的DataBinding事件生成一個事件處理程序。此事件將標籤屬性的值設置爲SomeProperty的值。無論何時調用DataBind方法,都會觸發DataBinding事件,這就是爲什麼我將該調用添加到Page_Load事件中的原因。
希望這可以幫助你理解它們之間的區別。
(http://stackoverflow.com/questions/9595851/set-visible-property-with-server-tag-in-framework-3-5 [設置可見與服務器標籤<%= %>在Framework 3.5屬性] ) – 2013-03-02 08:15:00