积极答复者
小问题

问题
-
<%=strJson%>这句代码是什么意思。
- 已移动 孟宪会Moderator 2009年8月14日 8:42 (发件人:.NET Framework 一般性问题讨论区)
答案
-
你好,输入strJson方法返回的内容。
jon.valett@gmail.com- 已标记为答案 邹俊才Moderator 2009年8月15日 3:45
-
你好,请参考:
http://msdn.microsoft.com/zh-cn/library/ms178135(VS.80).aspx
jon.valett@gmail.com- 已标记为答案 邹俊才Moderator 2009年8月15日 3:45
-
您好,
代码的目的如1987raymond版主所述,<% ...... %>的语法表示在这里面的代码会在服务器端运行
稍作解释,请参考:
通常对strJson变量的定义有两种方法:
1、在当前的aspx网页中定义,代码参考如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebFormJS.aspx.cs" Inherits="WebApplication1.WebFormJS" %>
<!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>
</head>
<body>
<form id="form1" runat="server">
<div>
<% string strJson = "This is a Test!"; %>
<%=strJson %>
</div>
</form>
</body>
</html>
2、在CodeBehind中定义,可以把上面的代码分为两部分一个在.aspx文件一个在.cs文件
下面是.aspx的代码
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebFormJS.aspx.cs" Inherits="WebApplication1.WebFormJS" %>
<!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>
</head>
<body>
<form id="form1" runat="server">
<div>
<%=strJson %>
</div>
</form>
</body>
</html>
下面是.cs的代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;namespace WebApplication1
{
public partial class WebFormJS : System.Web.UI.Page
{
protected string strJson = "";
protected void Page_Load(object sender, EventArgs e)
{
strJson = "This is a test!";
}
}
}
注意,在这里需要声明为protected或public,为不破坏封装性建议采用protected。
原因如下:看看上面aspx中的第一句:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebFormJS.aspx.cs" Inherits="WebApplication1.WebFormJS" %>
请看Inherits指明了继承WebApplication1.WebFormJS类,这个类就是上面在.cs文件中定义的类。
所以不难理解aspx继承了相应的类,是子类所以可以访问protected修饰的变量。
希望有帮助!- 已标记为答案 邹俊才Moderator 2009年8月15日 3:48
全部回复
-
你好,输入strJson方法返回的内容。
jon.valett@gmail.com- 已标记为答案 邹俊才Moderator 2009年8月15日 3:45
-
你好,请参考:
http://msdn.microsoft.com/zh-cn/library/ms178135(VS.80).aspx
jon.valett@gmail.com- 已标记为答案 邹俊才Moderator 2009年8月15日 3:45
-
你好 这个是将strJson变量的输出到页面输出流中 并在页面显示 这个应该是集成ASP的那种方式
Wenn ich dich hab’,gibt es nichts, was unerträglich ist.坚持不懈!http://hi.baidu.com/1987raymond -
您好,
代码的目的如1987raymond版主所述,<% ...... %>的语法表示在这里面的代码会在服务器端运行
稍作解释,请参考:
通常对strJson变量的定义有两种方法:
1、在当前的aspx网页中定义,代码参考如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebFormJS.aspx.cs" Inherits="WebApplication1.WebFormJS" %>
<!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>
</head>
<body>
<form id="form1" runat="server">
<div>
<% string strJson = "This is a Test!"; %>
<%=strJson %>
</div>
</form>
</body>
</html>
2、在CodeBehind中定义,可以把上面的代码分为两部分一个在.aspx文件一个在.cs文件
下面是.aspx的代码
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebFormJS.aspx.cs" Inherits="WebApplication1.WebFormJS" %>
<!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>
</head>
<body>
<form id="form1" runat="server">
<div>
<%=strJson %>
</div>
</form>
</body>
</html>
下面是.cs的代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;namespace WebApplication1
{
public partial class WebFormJS : System.Web.UI.Page
{
protected string strJson = "";
protected void Page_Load(object sender, EventArgs e)
{
strJson = "This is a test!";
}
}
}
注意,在这里需要声明为protected或public,为不破坏封装性建议采用protected。
原因如下:看看上面aspx中的第一句:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebFormJS.aspx.cs" Inherits="WebApplication1.WebFormJS" %>
请看Inherits指明了继承WebApplication1.WebFormJS类,这个类就是上面在.cs文件中定义的类。
所以不难理解aspx继承了相应的类,是子类所以可以访问protected修饰的变量。
希望有帮助!- 已标记为答案 邹俊才Moderator 2009年8月15日 3:48
-
asp.net的基础,在页面中直接输出strJson的值。
http://hi.baidu.com/2009ajun