ASP.NET 的 WebControl.Attributes 属性

本文介绍WebControl.Attributes属性,该属性允许开发者以编程方式控制Web服务器控件的特性,包括添加和删除属性。通过示例展示了如何使用该属性为TextBox控件添加JavaScript命令。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

WebControl.Attributes 属性 


获取与控件的属性不对应的任意特性(只用于呈现)的集合。
[Visual Basic]
Public ReadOnly Property Attributes As AttributeCollection
[C#]
public AttributeCollection Attributes {get;}
[C++]
public: __property AttributeCollection* get_Attributes();
[JScript]
public function get Attributes() : AttributeCollection;


属性值
名称和值对的 System.Web.UI.AttributeCollection。


备注
Attributes 集合包含在 Web 服务器控件的开始标记中声明的所有属性的集合。这使您得以以编程方式控制与 Web 服务器控件关联的属性。您可以将属性添加到此集合或从此集合中移除属性。
注意   此属性用控件开始标记中集合中的所有属性来呈现,与浏览器设置无关。并非所有的浏览器都支持呈现的每个属性。不受支持的属性通常被浏览器忽略。


示例
[Visual Basic, C#, JScript] 下面的示例阐释当 TextBox 控件失去焦点时,可以如何使用 WebControl 的 Attributes 属性运行 Javascript 命令。
[Visual Basic]

<%@ Page Language="VB" AutoEventWireup="True" %>

 <html>
 <head>
    <script language="VB" runat="server">
 
        Sub Page_Load(sender As Object, e As EventArgs)
            TextBox1.Attributes("onblur") = "javascript:alert('Hello! Focus lost from text box!!');"
        End Sub
    </script>
 
 </head>
 <body>
    <h3>Attributes Property of a Web Control</h3>
 <form runat="server">
 
    <asp:TextBox id="TextBox1" columns=54
     Text="Click here and then tap out of this text box"
     runat="server"/> 
 
 </form>
 </body>
 </html>
[C#]

<%@ Page Language="C#" AutoEventWireup="True" %>

 <html>
 <head>
    <script language="C#" runat="server">
 
        void Page_Load(Object sender, EventArgs e) {
           TextBox1.Attributes["onblur"]="javascript:alert('Hello! Focus lost from text box!!');";   
        }
    </script>
 
 </head>
 <body>
    <h3>Attributes Property of a Web Control</h3>
 <form runat="server">
 
    <asp:TextBox id="TextBox1" columns=54
     Text="Click here and then tap out of this text box"
     runat="server"/> 
 
 </form>
 </body>
 </html>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值