<%@ 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>
选择酒店: <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>
选择房间: <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);
}
}
<!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>
选择酒店: <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>
选择房间: <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);
}
}
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>");
}
}
但运行过输出就会弹出错误框
<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>
<%@ 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>
选择酒店: <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 />
选择房间: <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>");
} }
}
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>