我有多个网页,里面有相同的函数,我想把相同的函数集中写到一个类里面。但是其中的几个函数出现错误,分别在网页里是没有错误的: public string getip() //获取客户端ip地址函数
{
string ip = "";
if (Context.Request.ServerVariables["HTTP_VIA"] != null)
{
ip = Context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString(); }
else
{
ip = Context.Request.ServerVariables["REMOTE_ADDR"].ToString();
}
return ip;
}这是第一个 编译错误:错误 1 当前上下文中不存在名称“Context”
我的命名空间是:
using System;
using System.Data;
using System.Configuration;
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;
using System.Data.SqlClient;
第二个问题:tableworks1.Rows[row].Cells[cell].InnerText = "已获得" + getvote(vid) + "票";
不认识这个tableworks1,这是我在其中一个网页定义的表的id,如何让类能认识它?
{
string ip = "";
if (Context.Request.ServerVariables["HTTP_VIA"] != null)
{
ip = Context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString(); }
else
{
ip = Context.Request.ServerVariables["REMOTE_ADDR"].ToString();
}
return ip;
}这是第一个 编译错误:错误 1 当前上下文中不存在名称“Context”
我的命名空间是:
using System;
using System.Data;
using System.Configuration;
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;
using System.Data.SqlClient;
第二个问题:tableworks1.Rows[row].Cells[cell].InnerText = "已获得" + getvote(vid) + "票";
不认识这个tableworks1,这是我在其中一个网页定义的表的id,如何让类能认识它?
1、命名空间有没有引用?
2、Context在不在类文件里面?
3、tableworks1是不是这个文件里的?
除非你把这个table 放在集合中比如session
在其他的地方用的时候取出来
DataTable dt = (DataTable)Session["key"];
tableworks1是在网页Default.aspx的html语言里定义的table的id,为什么在Default.aspx.cs里就能调用,在我自己建的一个基类xy.cs里就调用不了呢?
这是当然了!tableworks1在Default.aspx.cs里能调用,是因为它已经注册过了!你自己建的类没有这个东西,当然用不了。
不太明白你这样做是为了什么?
HttpContext context
HttpContext.Current