2012-10-16 67 views
-1

只是想知道,如果有可能不拆以下步骤...提前创建时为控件分配属性?

Button b = new Button() 
{ 
    // You can access Attributes here, but only get, not set. 
    ID = "btnExample", 
    Text = "Click Me" 
}; 

b.Attributes.Add("onclick", "alert('hello, world')"); 

感谢。

+0

你为什么不试试呢? – codingbiz

回答

1

你可以从按键驱动,并将其添加到构造函数:

class MyButton: Button 
{ 
    MyButton(MyAttributes atr) 
    { 
     for each (elm in atr) 
     { 
      base.Attributes.Add(elm.Key, elm.Value); 
     } 
    } 
} 
相关问题