string strScript; 
 string strhtml = "<table>";
            List<Main> listmain = new List<Main>();
            listmain = mbll.getTop10Main();
            for (int i = 0; i < listmain.Count;i++ )
            {
                strhtml += " <tr>";
                strhtml += " <td>";
                strhtml += " <a href='MainInfo.aspx?mid=" + listmain[i].Mid + "'>" + listmain[i].Workinfo.ToString() +" </a>"; 
                strhtml += " </td>";
                strhtml += " </tr>"; 
            }
            strhtml += " </table>";            strScript = " <script type='text/javascript'>";
            strScript += "window.document.getElementById('sdiv').innerHTML = " + strhtml + ";";
            strScript += " </script>";
            Response.Write(strScript);
我在页面有这样一个层:
<div id="sdiv">
            
            </div>
但是他始终抱 说window.document.getElementById('sdiv')对象为空,或不是对象。??????
请高手指点哈!!!

解决方案 »

  1.   

    IE浏览器,页面还没加载完成的时候DOM还没生成,getElementById当然为空了。你可以把这段脚本写成添加到window.onload事件里。。
      

  2.   

    Page.registCLientscript注册 页面脚本
      

  3.   

    或者用 RegisterStartup 来输出脚本
      

  4.   

    不好意思,看错了!<div id="sdiv"> 
                
                </div> 
    window.document.getElementById('sdiv')对象为空,或不是对象。?????? 
    window.document.getElementById('sdiv') 这句话,必须写在 <div id="sdiv"></div>
    后面
      

  5.   

    Response.Write的东西在页面的最顶部,执行JS的时候document还未加载,故报错简单的办法:
    <div id="sdiv"> 
                <%= strhtml %>
                </div> 
      

  6.   

    如果在后台进行组装的话,最好用StringBulider的,不要用string的
    public string SetDivHtml()
    {
    StringBulider html = new StringBulider();
    html.Append("<table>");
    ...
    return html.ToString();
    }<div id="sdiv"> 
                <%= SetDivHtml()%> 
                </div> 
      

  7.   

    <div id="sdiv"> 
                <%= strhtml %> 
                </div>