今天,有网友问我这个问题,asp中,服务端JS如何遍历Request.ServerVariables?
说实话,以前的asp都使用vbs写的,从来没想过js怎么写,随即给出答案! 如下:
<%@ Language="Javascript" %>
<%
for(var i in Request.ServerVariables)
{
Response.Write(Request.ServerVariables(i));
}
%>
运行后发现,并没有任何输出,奇怪! 呵呵!
随即看看vbs的写法,如下:
<TABLE>
<TR><TD>Server Variable</TD><TD>Value</TD></TR>
<% For Each name In Request.ServerVariables %>
<TR><TD> <%= name %> </TD><TD> <%= Request.ServerVariables(name) %> </TD></TR>
<% Next %>
</TABLE>
看来没有什么问题,只得暂时作罢!
今天,查阅MSDN无意间发现了这个东西,Enumerator !
随即给出如下代码,
<%@ Language="JavaScript" %>
<%
var s = new Enumerator(Request.ServerVariables);
for(;!s.atEnd();s.moveNext())
{
var x = s.item();
Response.Write(x + " = " + Request.ServerVariables(x) + "<br>");
}
%>
问题解决!看来服务端的js语言规则与客户端不是完全一样! 呵呵!
很早期的东西

/**//*
* Class Session
* 在客户端多页面之间共享信息
*/
function Session()

{
var SessionObj = null;
this.init = function()
{
SessionObj = document.createElement('input');
SessionObj.type = "hidden";
SessionObj.id = "Sessionid";
SessionObj.style.behavior = "url('#default#userData')"
document.body.appendChild(SessionObj);
}
this.load = function(sessionName)
{
if (sessionName != null && sessionName != "")
{
SessionObj.load("s");
return SessionObj.getAttribute(sessionName);
}
}
this.save = function(objId,attribute,sessionName)
{
var obj = null;
if (document.getElementById(objId) != null) obj = document.getElementById(objId)
else return;
var value = obj[attribute];
if (sessionName != null && sessionName != "")
{
SessionObj.setAttribute(sessionName,value)
SessionObj.save("s")
}
}
this.init();
}
var Session = new Session();

//例子:
在文本框中输入任意内容!!点击 [save] !!<br>
然后刷新页面!!也可以删除文本框内容!!! 点击[load] <br><br>
<input type="text" id="txt">
<input type="button" id="btn" value="save" onclick="Session.save('txt','value','op');">
<input type="button" id="btn1" onclick="alert(Session.load('op'));" value="load">
<script>
function Session()

{
var SessionObj = null;
this.init = function()
{
SessionObj = document.createElement('input');
SessionObj.type = "hidden";
SessionObj.id = "Sessionid";
SessionObj.style.behavior = "url('#default#userData')"
document.body.appendChild(SessionObj);
}
this.load = function(sessionName)
{
if (sessionName != null && sessionName != "")
{
SessionObj.load("s");
return SessionObj.getAttribute(sessionName);
}
}
this.save = function(objId,attribute,sessionName)
{
var obj = null;
if (document.getElementById(objId) != null) obj = document.getElementById(objId)
else return;
var value = obj[attribute];
if (sessionName != null && sessionName != "")
{
SessionObj.setAttribute(sessionName,value)
SessionObj.save("s")
}
}
this.init();
}
var Session = new Session();
</script>
namespace THOA.Web.Utility

{
class HandlerTaskbar : IHttpHandler
{
IHttpHandler 成员#region IHttpHandler 成员
public bool IsReusable
{
get
{ return false; }
}
public void ProcessRequest(HttpContext context)
{
Task task = new Task();
ICtrl ctrTask = ICtrl.FactoryCreate(CtrType.CtrTask);
task.TaskID = Convert.ToInt32(context.Request.QueryString["task_id"]);
Bitmap map = new Bitmap(m_Width, m_Height);
Graphics g = Graphics.FromImage(map);
//--此处进度条绘图代码略
context.Response.Clear();
map.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif); //写回客户端输出流
g.Dispose();
map.Dispose();
context.Response.End();
}
}
}
<httpHandlers>
<add verb="GET" path="Taskbar.aspx" type="THOA.Web.Utility.HandlerTaskbar,THOA.Web.Utility" />
</httpHandlers>
<asp:GridView ID="gvTask" runat="server">
<Columns>
<asp:TemplateField HeaderText="进度">
<HeaderStyle HorizontalAlign="center" Width="45px"/>
<ItemTemplate>
<asp:Image runat="server" ID="imgBar" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
void gridview_RowDataBound(object sender, GridViewRowEventArgs e)

{
Image imgBar = e.Row.FindControl("imgBar") as Image;
if (imgBar != null)
imgBar.ImageUrl = "Taskbar.aspx?task_id=" + e.Row.Cells[0].Text;
}