<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"  Inherits="_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>Untitled Page</title>
<script   language="javascript">   
  function   selectroom(hostelid)
  {                 
    var un=encodeURI(hostelid);   
    var scr=document.createElement('script');   
    scr.src='Default.aspx?hostedlid=' + un;   
    document.getElementById('room').appendChild(scr); 
    //alert(document.getElementById('room').innerHTML);   
  }    
</script>   </head>
<body>
<form>
    <div>
        选择酒店:&nbsp;&nbsp;&nbsp;&nbsp;<select   name="hostelid"   id="hostelid"   onChange="selectroom(this.value);">   
    <option   value="1">adfasdfadf</option>   
    <option   value="1">adfasdfadf</option>   
    <option   value="1">adfasdfadf</option>   
    <option   value="1">adfasdfadf</option> 
    </select>
    <br><br>   
  选择房间:&nbsp;&nbsp;&nbsp;&nbsp;<div id="room"></div>
    </div>
    </form>
</body>
</html>
代码
string str = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        int id = Convert.ToInt32(Request["hostelid"]);
        if (id != 0)
        {
            str = "document.getElementById('room').innerHTML = ";
            for (int i = 1; i < id; i++)
            {
                str = str + "★";
                str = str + "";
            }            Response.Write(str);
        }
    }

解决方案 »

  1.   

    你的代码肯定是有问题的,改成下面的string str = string.Empty; 
        protected void Page_Load(object sender, EventArgs e) 
        { 
            int id = Convert.ToInt32(Request["hostelid"]); 
            if (id != 0) 
            { 
                str = "document.getElementById('room').innerHTML =' "; 
                
                for (int i = 1; i  < id; i++) 
                { 
                    str += "★"; 
                } 
                str+="';";
                Response.Write("<script language=javascript>"+str+"</script>");
            } 
        }
      

  2.   

    调用JS脚本后,会弹出语法错误的弹出框,照my22xo 修改了,还是不行
      

  3.   

    2楼代码存在js执行先后的问题,如果是文档加载完后清楚div的内容,则要加上window.onload改输出如下Response.Write("<script language=javascript>window.onload=function(){"+str+";}</"+"script>");
      

  4.   

    帖出生成的html代码看看
      

  5.   

    后台运行后生成的<script>document.getElementById('room').innerHTML =' ★★★';</script>
    但运行过输出就会弹出错误框
      

  6.   

    <!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>
      <title> new document </title>
      <meta name="generator" content="editplus" />
      <meta name="author" content="www.77techan.com" />
      <meta name="keywords" content="" />
      <meta name="description" content="" />
     </head> <body>
      <script type="text/javascript">
      <!--
    function GetData(webFile, fnWhenDone)
    {
    var myConn = new XHConn();
    if (!myConn) alert("XMLHTTP not available. Try a newer/better browser.");
    myConn.connect(webFile, "GET", "", fnWhenDone);
    }function PostData(webFile, data, fnWhenDone)
    {
    var myConn = new XHConn();
    if (!myConn) alert("XMLHTTP not available. Try a newer/better browser.");
    myConn.connect(webFile, "POST", data, fnWhenDone);
    }function XHConn()
    {
      var xmlhttp, bComplete = false;
      try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
      catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
      catch (e) { try { xmlhttp = new XMLHttpRequest(); }
      catch (e) { xmlhttp = false; }}}
      if (!xmlhttp) return null;
      this.connect = function(sURL, sMethod, sVars, fnDone)
      {
        if (!xmlhttp) return false;
        bComplete = false;
        sMethod = sMethod.toUpperCase();    try {
          if (sMethod == "GET")
          {
            xmlhttp.open(sMethod, sURL+ ((sURL.indexOf("?")==-1)? "?":"&")+sVars, true);
            sVars = "";
          }
          else
          {
            xmlhttp.open(sMethod, sURL, true);
            xmlhttp.setRequestHeader("Method", "POST "+sURL+" HTTP/1.1");
            xmlhttp.setRequestHeader("Content-Type",
              "application/x-www-form-urlencoded");
          }
          xmlhttp.onreadystatechange = function(){
            if (xmlhttp.readyState == 4 && !bComplete)
            {
              bComplete = true;
              fnDone(xmlhttp);
            }};
          xmlhttp.send(sVars);
        }
        catch(z) { firstPost = true; return false; }
        return true;
      };
      return this;
    }
    function Show(o)
    {
    document.getElementById('room').innerHTML = o.responseText;
    }
      function   selectroom(hostelid) 
      {
    GetData('/处理的文件?hostelid='+hostelid, Show);    
      } 
      //-->
      </script>
      <select   name="hostelid"   id="hostelid"   onChange="selectroom(this.value);">    
         <option   value="1">adfasdfadf </option>    
         <option   value="1">adfasdfadf </option>    
         <option   value="1">adfasdfadf </option>    
         <option   value="1">adfasdfadf </option>  
         </select>
    <div id="room"> </div> </body>
    </html>
      

  7.   


    <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_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>
        <script type="text/javascript">    
      function   selectroom(hostelid) 
      {
        /*                  
        var un=encodeURI(hostelid);    
        var scr=document.createElement('script');    
        scr.src='Default.aspx?hostedlid=' + un;    
        document.getElementById('room').appendChild(scr);  
        //alert(document.getElementById('room').innerHTML);
        以为代码错误,因为Default.aspx,返回的有html,而当成js来处理,一定有问题
        */
        var un=encodeURI(hostelid);
        location.href='Default.aspx?hostedlid=' + un; 
      }     
    </script>    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <div> 
            选择酒店:&nbsp;&nbsp;&nbsp;&nbsp; <select name="hostelid" id="hostelid" onchange="selectroom(this.value);">    
         <option   value="1">adfasdfadf </option>    
         <option   value="1">adfasdfadf </option>    
         <option   value="1">adfasdfadf </option>    
         <option   value="1">adfasdfadf </option>  
         </select> 
         <br /> <br />    
      选择房间:&nbsp;&nbsp;&nbsp;&nbsp; <span id="room"></span> 
         </div> 
        </div>
        </form>
    </body>
    </html>public partial class _Default : System.Web.UI.Page 
    {
        string str = string.Empty; 
        protected void Page_Load(object sender, EventArgs e)
        {
            JsInit();
        }
        public void JsInit() {
            int id = Convert.ToInt32(Request["hostedlid"]);
            if (id != 0)
            {
                str = "document.getElementById('room').innerHTML ='";            for (int i = 0; i < id; i++)
                {
                    str += "★";
                }
                str += "';";
                Response.Write("<script type=\"text/javascript\">window.onload=function(){" + str + "}</"+"script>");
            }     }
    }
      

  8.   

    <script   language="javascript">    
      function   selectroom(hostelid) 
      {                  
        var un=encodeURI(hostelid);    
        var scr=document.createElement('script');    
        scr.src='Default.aspx?hostedlid=' + un;    
        document.getElementById('room').appendChild(scr);  
        //alert(document.getElementById('room').innerHTML);    
      }     
    </script>    方法有错误
    <script   language="javascript">    
      function   selectroom(hostelid) 
      {                  
        var un=encodeURI(hostelid);       
        un='Default.aspx?hostedlid=' + un;    
        document.getElementById('room').innerHTML=un
      }     
    </script> 
      

  9.   

    忘了一件事,因为你的Response.Write代码是放在page_load里面的,所以JS脚本会抢在你的room对应的html标签之前出现,也就是说<script这代码块在页面最先出现,所以js找不到room的对应ID,报错,需要解决的就先将room这个标签在页面上显示出来,再输出response.write