编译错误 CS0120: 非静态字段、方法或属性“System.Web.UI.Page.Session.get”要求对象引用

2025-04-19 09:11:05
推荐回答(2个)
回答1:

取Session不能在方法外面取,不然会报上述错误。
楼主需要把取Session写在某个方法里,如果这个UserID需要整个页面都能用,可以把UserID定义到外面,如:
public partial class OrderQuery : System.Web.UI.Page
{
protected SqlConnection con = new SqlConnection(Common.Conn);
private String UserID="";
protected void Page_Load(object sender, EventArgs e)
{
String UserID = Session["UserName"].ToString();
}

回答2:

把UserID定义到外面