技术开发 频道

ASP.NET 4.0新特性之Web.config的改进

  3、ClientIDMode 属性的用法

  ClientID.aspx

<%@ Page Title="ClientID" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true"
    CodeBehind
="ClientID.aspx.cs" Inherits="AspDotNet.ClientID" ClientIDMode="Static" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    
    
<!--
        ClientIDMode
- 生成客户端 ID 的方式
            ClientIDMode.AutoID
- 生成方式和以前一样,为保证唯一,会把其以上各层级的控件ID拿过来拼成一个页面中的唯一ID
            ClientIDMode.Inherit
- 继承父控件的客户端ID生成方式
            ClientIDMode.Static
- 静态方式。在服务端设置的ID是什么,客户端所呈现的ID就是什么
            ClientIDMode.Predictable
- 生成ID的方式为:[Prefix]_[ID]_[Suffix]
        注意:
            在某控件层级中如果没有设置 ClientIDMode,则其默认值为 AutoID
            如果在控件层级中的父级控件设置了 ClientIDMode,则其子控件的默认值为 Inherit
    
-->

    
<!-- ClientIDMode.AutoID 的 Demo -->
    
<fieldset>
        
<legend>Legacy</legend>
        
<asp:TextBox ID="txtLegacy" ClientIDMode="AutoID" runat="server" Text="ID: txtLegacy" />
    
</fieldset>

    
<!-- ClientIDMode.Static 的 Demo -->
    
<fieldset>
        
<legend>Static</legend>
        
<asp:TextBox ID="txtStatic" ClientIDMode="Static" runat="server" Text="ID: txtStatic" />
    
</fieldset>

    
<!-- ClientIDMode.Inherit 的 Demo (注意:Page 上的 ClientIDMode 的值为 Static,所以此控件的客户端ID生成方式也是 Static)-->
    
<fieldset>
        
<legend>Inherit</legend>
        
<asp:TextBox ID="txtInherit" ClientIDMode="Inherit" runat="server" Text="ID: txtInherit" />
    
</fieldset>

    
<!-- Predictable 模式中自动分配 Suffix 的方式 -->
    
<fieldset>
        
<legend>Predictable Repeater </legend>
        
<div id="repeaterContainer">
            
<asp:Repeater ID="repeater" runat="server" ClientIDMode="Static">
                
<ItemTemplate>
                    
<div>
                        
<asp:Label ID="productPrice" ClientIDMode="Predictable" runat="server">
                        
<%# string.Format(System.Globalization.CultureInfo.CurrentUICulture, "{0:c}", Eval("ProductPrice"))%>
                        
</asp:Label>
                    
</div>
                
</ItemTemplate>
            
</asp:Repeater>
        
</div>
        
<asp:TextBox ID="txtPredictableRepeater" runat="server" TextMode="MultiLine" Rows="10"
            ClientIDMode
="Static" Style="width: 99%;" />
    
</fieldset>

    
<!-- Predictable 模式中分配指定 Suffix 的方式(ClientIDRowSuffix 指定 Suffix 的数据来源) -->
    
<fieldset>
        
<legend>Predictable ListView </legend>
        
<asp:ListView ID="listView" runat="server" ClientIDMode="Static" ClientIDRowSuffix="ProductId">
            
<ItemTemplate>
                
<div>
                    
<asp:Label ID="productPrice" ClientIDMode="Predictable" runat="server">
                        
<%# string.Format(System.Globalization.CultureInfo.CurrentUICulture, "{0:c}", Eval("ProductPrice"))%>
                    
</asp:Label>
                
</div>
            
</ItemTemplate>
            
<LayoutTemplate>
                
<div id="listViewContainer">
                    
<div id="itemPlaceholder" runat="server" />
                
</div>
            
</LayoutTemplate>
        
</asp:ListView>
        
<asp:TextBox ID="txtPredictableListView" runat="server" TextMode="MultiLine" Rows="10"
            ClientIDMode
="Static" Style="width: 99%;" />
    
</fieldset>

    
<script type="text/javascript">

        window.onload
= function () {
            document.getElementById(
'<%= txtLegacy.ClientID %>').value += "   ClientID: " + '<%= txtLegacy.ClientID %>';

            document.getElementById(
'<%= txtStatic.ClientID %>').value += "   ClientID: " + '<%= txtStatic.ClientID %>';

            document.getElementById(
'<%= txtInherit.ClientID %>').value += "   ClientID: " + '<%= txtInherit.ClientID %>';

            document.getElementById(
'txtPredictableRepeater').value = document.getElementById('repeaterContainer').innerHTML;

            document.getElementById(
'txtPredictableListView').value = document.getElementById('listViewContainer').innerHTML;
        }

    
</script>
</asp:Content>

 

  ClientID.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace AspDotNet
{
    
public partial class ClientID : System.Web.UI.Page
    {
        protected void Page_Load(
object sender, EventArgs e)
        {
            BindData();
        }

        
// 绑定数据到 ListView
        
private void BindData()
        {
            Random random
= new Random();

            List
<Product> products = new List<Product>();
            
for (int i = 0; i < 5; i++)
            {
                products.Add(
new Product { ProductId = i + 100, ProductName = "名称", ProductPrice = random.NextDouble() });
            }

            listView.DataSource
= products;
            listView.DataBind();

            repeater.DataSource
= products;
            repeater.DataBind();
        }

        class Product
        {
            
public int ProductId { get; set; }
            
public string ProductName { get; set; }
            
public double ProductPrice { get; set; }
        }
    }
}

  

0
相关文章