数据绑定语法

 

数据绑定语法

1.   ASP.NET 声明性数据绑定语法使用 <%# %> 表示法。

2.   可以绑定到数据源、页或其他控件的属性、集合、表达式以及从方法调用返回的结果。

3.   语法示例

简单属性

Customer: <%# custID %>

集合

Orders: <asp:ListBox id="List1" datasource='<%# myArray %>' runat="server">

表达式

Contact: <%# ( customer.First Name + " " + customer.LastName ) %>

方法结果

Outstanding Balance: <%# GetBalance(custID) %>

4.   ASP Response.Write DataBind 方法区别

ASP Response.Write 快捷方式语法在处理页时计算,而 ASP.NET 数据绑定语法仅在调用 DataBind 方法时计算。

5.   DataBind 的级联

当在父控件上调用 DataBind 时,它级联到该控件的所有子控件。

例如,DataList1.DataBind() 将因此对 DataList 模板中的每一控件调用 DataBind 方法。在上调用 DataBindPage.DataBind() 或只是 DataBind()会导致计算页上的所有数据绑定表达式

6.  DataBinder.Eval

A.计算后期绑定的数据绑定表达式并且可选择将结果格式化为字符串。

B.<%# DataBinder.Eval(Container.DataItem, "IntegerValue", "{0:c}") %>

CDataBinder.Eval 会对标准数据绑定语法带来很明显的性能损失,因为它使用后期绑定反射,注意这一点很重要。使用 DataBinder.Eval 时需谨慎,尤其是在不需要字符串格式化时。

7数据绑定表达式必须计算为 String 类型的值

自定义控件类的代码

using System;

using System.Web;

using System.Web.UI;

 

 

namespace SimpleControlSamples {

 

 

    public class Simple : Control {

 

 

       protected override void Render(HtmlTextWriter output) {

           output.Write("<H2>欢迎使用控件开发!</H2>");

       }

    }   

}

aspx页引用该自定义控件的代码

<%@ Register TagPrefix="SimpleControlSamples" Namespace="SimpleControlSamples" Assembly="SimpleControlSamples" %>

<SimpleControlSamples:Simple id="MyControl" runat=server/>

 

 

属性类似具有访问器方法的聪明字段。应该从控件公开属性而不是公开公共字段,因为属性允许数据隐藏、可以版本化并受可视化设计器的支持。属性具有设置和检索属性的 get/set 访问器方法,并允许在需要时执行附加的程序逻辑。

 

 

如果 A 类具有一个类型为 B 类的属性,则 B 的属性(如果有)称为 A 的子属性。下面的示例定义自定义控件 SimpleSubProperty,该控件具有 Format 类型的属性。Format 是具有两个基元属性的类——Color Size,这两个属性反过来成为 SimpleSubProperty 的子属性。

 

 

注意 ASP.NET 具有设置子属性的特殊语法。下面的代码示例显示如何以声明方式设置 SimpleSubProperty 上的 Format.Color Format.Size 子属性。“-”语法指示子属性。

<SimpleControlSamples:SimpleSubProperty Message="Hello There" Format-Color="red" Format-Size="3" runat=server/>

每个控件都具有从 System.Web.UI.Control 继承的 Controls 属性. 这是表示控件的子控件(如果有)的集合属性

如果控件未用 ParseChildrenAttribute 标记,或是标记为 ParseChildrenAttribute(ChildrenAsProperties = false),则当控件在页上以声明方式使用时,ASP.NET 页框架将应用以下分析逻辑。如果分析器在控件的标记内遇到嵌套控件,它将创建嵌套控件的实例并将它们添加到控件的 Controls 属性。标记之间的文本添加为 LiteralControl。任何其他嵌套元素都生成分析器错误。
:
 <SimpleControlSamples:SimpleInnerContent id="MyControl" runat=server>

             我的消息在控件标记内!!!!

          </SimpleControlSamples:SimpleInnerContent>

以上正确

<SimpleControlSamples:SimpleInnerContent id="MyControl" runat=server>

            <input type=”text” runat = server>          </SimpleControlSamples:SimpleInnerContent>

以上错误,因为<input type=”text” runat = server>是一个控件,标记之间只能添加文本,要添加控件,必须设置属性ParseChildrenAttribute=true

 

 

 

 

在分析 ASP.NET 页时,样式信息被填充到 System.Web.UI.HtmlControls.HtmlControl 类上的 Style 属性(CssStyleCollection 类型)中。该属性本质上是一个词典,它将控件的样式公开为每个样式属性键值的字符串索引集合。例如,可以使用下面的代码在 HtmlInputText 服务器控件上设置并随后检索 width 样式属性。

<script language="JavaScript" type="text/javascript"> function doClick(index, numTabs, id) { document.all("tab" + id, index).className = "tab"; for (var i=1; i &lt; numTabs; i++) { document.all("tab" + id, (index + i) % numTabs).className = "backtab"; } document.all("code" + id, index).style.display = ""; for (var j=1; j &lt; numTabs; j++) { document.all("code" + id, (index + j) % numTabs).style.display = "none"; } } </script>

 

 

<script language="C#" runat="server" >

 

 

    void Page_Load(Object sender, EventArgs E) {

        MyText.Style["width"] = "90px";

        Response.Write(MyText.Style["width"]);

    }

 

 

</script>

 

 

<input type="text" id="MyText" runat="server"/>

 

 

<script language="VB" runat="server" >

 

 

    Sub Page_Load(Sender As Object, E As EventArgs)

        MyText.Style("width") = "90px"

        Response.Write(MyText.Style("width"))

    End Sub

 

 

</script>

 

 

<input type="text" id="MyText" runat="server"/>

 

 

<script language="JScript" runat="server" >

 

 

    function Page_Load(sender : Object, E : EventArgs) : void {

        MyText.Style("width") = "90px";

        Response.Write(MyText.Style("width"));

    }

 

 

</script>

 

 

<input type="text" id="MyText" runat="server"/>

<script language="C#" runat="server">

    void Page_Load(Object Src, EventArgs E ) {

        Style style = new Style();

        style.BorderColor = Color.Black;

        style.BorderStyle = BorderStyle.Dashed;

        style.BorderWidth = 1;

 

        MyLogin.ApplyStyle (style);

        MyPassword.ApplyStyle (style);

        MySubmit.ApplyStyle (style);

    }

</script>

 

Login: <ASP:TextBox id="MyLogin" runat="server" />/<p/>

Password: <ASP:TextBox id="MyPassword" TextMode="Password" runat="server" />

View:  <ASP:DropDownList id="MySelect" runat="server">  ...  </ASP:DropDownList>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值