动态设置HTML服务器控件的样式和属性

protected void Page_Load(object sender, System.EventArgs e)
{
    // Perform the initialization only the first time the page is requested.
    // After that, this information is tracked in view state.
    if (!Page.IsPostBack)
    {
        // Set the style attributes to configure appearance.
        Text1.Style["font-size"] = "20px";
        Text1.Style["color"] = "red";
        // Use a slightly different but equivalent syntax
        // for setting a style attribute.
        Text1.Style.Add("background-color", "lightyellow");
        // Set the default text.
        Text1.Value = "<Enter e-mail address here>";
        // Set other nonstandard attributes.
        Text1.Attributes["onfocus"] = "alert(Text1.value)";
    }

}

If you request the page, the following HTML code will be returned for the text box:
<input id="Text1" type="text"
 style="font-size:20px;color:red;background-color:lightyellow;"
 value="<Enter e-mail address here>"
 onfocus="alert(Text.value)" /> 

posted @ 2009-10-30 10:19  wispzone  阅读(681)  评论(0编辑  收藏  举报