一百分求解了:为什么我在JS中调用session,就会报错呢??

malin2670372 2008-09-08 03:14:31

我用C#在load中:Session["theVal"] = "asdf";
string theValue = Session["theVal"].ToString();
在前台JS方法中:
function addRow()
{
var controlValue = ' <% = theValue%>';
//var contrlValue = '<% = Session["theVal"] %>'; 也不行
}

在方法中用了newrow = table.insertRow(),和newrow.insertCell用于动态创建行
我感觉是一用到了 <% %>页面就会报错. 请高手指点下.谢谢了
我不是为了去获得session值,这只是一测试.
我想的是调用后台代码一有返回值的方法.

...全文
130 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
malin2670372 2008-09-08
  • 打赏
  • 举报
回复
谢谢各位,public string theValue = "";全局的.
kkai189 2008-09-08
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 wxg22526451 的回复:]
var controlValue = ' <% = theValue%>';
theValue得是protected或public才行
[/Quote]

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default4.aspx.cs" Inherits="Default4" %>

<!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 language="javascript">
var controlValue;
function addRow(){
controlValue='<%=this.theValue %>';
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<div id="s"></div>
<input id="Button1" type="button" value="button" onclick="addRow();document.getElementById('s').innerHTML=controlValue;" />
</div>
</form>
</body>
</html>


using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Default4 : System.Web.UI.Page
{
protected string theValue;
protected void Page_Load(object sender, EventArgs e)
{
Session["theVal"] = "asdf";
theValue = Session["theVal"].ToString();
}
}


zpcoder 2008-09-08
  • 打赏
  • 举报
回复
经过测试: 通过


<script type="text/javascript">
function f()
{
var i='<%= Session["mysin"] %>';
alert(i);
}
</script>
<link href="Style/globe.css" rel="stylesheet" type="text/css" />
</head>
<body onload="f()">
malin2670372 2008-09-08
  • 打赏
  • 举报
回复
我也很头疼啊.不知道为什么!在我的addRow()中有实现动态添加row 和cell.每次都是出现在while后边说:Microsoft JScript 运行时错误: 缺少对象!
var   i=2;
while(i--)
{
var newcell = newrow.insertCell();

switch(i){
case 1: newcell.innerHTML= '<input type="button" onClick="javascript:removeRow(this.parentElement.parentElement)" value="删除">';break;
default: newcell.innerHTML= andOrValue+" "+ fieldItem + " " + operatorItem+ " "+controlValue;
break;

我把var controlValue = ' <% = theValue%>';删掉就正确了...我用<%=ReturnValue() %>同样的错误.ReturnValue()返回string.
ppp7p 2008-09-08
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 wxg22526451 的回复:]
var controlValue = ' <% = theValue%>';
theValue得是protected或public才行
[/Quote]
可以试一下,不知道是不是这个原因。
hm7921936 2008-09-08
  • 打赏
  • 举报
回复
string theValue 的是全局. PUBLIC
hm7921936 2008-09-08
  • 打赏
  • 举报
回复
运行没发现报错
wxg22526451 2008-09-08
  • 打赏
  • 举报
回复
var controlValue = ' <% = theValue%>';
theValue得是protected或public才行
drummery 2008-09-08
  • 打赏
  • 举报
回复
没有明白你的意思,我试过了,如果代码没有其他问题,

var contrlValue = ' <% = Session["theVal"] %>';

这一句是可以正常解析的。

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧