asp.net 中如何用代码设置HTML容器元素的CLASS属性与内容?
如:<div id="test" runat="server"></div>
现在要设置他的CLASS="testclass"内容为abc要怎么做啊?
我试了Document.getElementById("test").className="testclass",编译时提示:当前上下文中不存在名称 Document是怎么回事啊? 

解决方案 »

  1.   

    <div id="test" runat="server" CLASS="testclass"></div>
      

  2.   

    我这个要用代码设置的,原来在asp里可以
    <div id="test" <%if type="2" then %>class="testclass"<%end if%>>
    <%if type="2" then %>123<%end if%>
    </div>
    现在在asp.net中,如果在代码分离的C#中实现呢?
      

  3.   


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;namespace WebApplication1
    {
        public partial class _Default : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                   HtmlGenericControl d = (HtmlGenericControl)form1.FindControl("test");
                   d.Attributes.Add ("class","t");
            }
        }
    }
    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <style type="text/css">
            .t
            {
                font-size: xx-large;
            }
        </style>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <div id="test" runat="server">
                hahaha</div>
        </div>
        </form>
    </body>
    </html>
      

  4.   

    谢谢楼上两位,但是那个<div>里面的,HAHAHA怎么用代码写进去呢?
      

  5.   


    Document.getElementById("test").innerHTML="aaaaa"
    我这个要用代码设置的,原来在asp里可以 
    <div id="test" <%if type="2" then %>class="testclass" <%end if%>> 
    <%if type="2" then %>123 <%end if%> 
    </div> 
    现在在asp.net中,如果在代码分离的C#中实现呢?这么写
    <div id="test" <%if (type=="2") {%>class="testclass" <%}%>> 
    <%if (type=="2"){ %>123 <%}%> 
    </div> 
      

  6.   

    编译时提示:当前上下文中不存在名称 Document是怎么回事啊? 
      

  7.   

    document.getElementById
    D是小写,js区分大小写
      

  8.   

    晕一个,这个是JS代码,不是C#代码啊?我还以为是C#的代码,把它放到了.CS文件中
      

  9.   

    <div id="test" runat="server"></div>test.InnerHtml="hahaahha";test.InnerText="fsafasf";