C#ASPX页面可以直接访问类方法吗?

这很好用,问题是我希望在许多页面上都这样做,并且不想一直重复与类通信的CS代码。

ASPX页面中的变量引用:

<%:countVal %>

后面的代码:

public string countVal = "";
protected void Page_Load(object sender,EventArgs e)
{
  reviewCount count = new reviewCount();
  countVal = count.reviewCounts().ToString();
}

和方法:

public int reviewCounts()
{
    int fbCount = 10;
    int googleCount = 10;

    int total = fbCount + googleCount;

    return total;
}

是否可以跳过中间的跳过并操纵此<%:countVal %>以直接从类中获取数据?

是C#的新词,很抱歉,答案是盲目的。

qq090630 回答:C#ASPX页面可以直接访问类方法吗?

您的reviewCounts()方法不是类,而是方法。您无法实例化一个方法,然后在该方法上访问一个方法。

相反,您将不得不将此reviewCounts方法放入实际的类中。由于您仅提供了样机代码,并且我不知道您是否真正需要实例来计算您的reviewCounts,因此我将为您提供此示例:

static class ReviewCount
{
  public static int ReviewCounts
  {
    get
    {
      int fbCount = 10;
      int googleCount = 10;

      int total = fbCount + googleCount;

      return total;
    }
  }
}

然后使用<%: ReviewCounts.ReviewCounts %>从任何aspx页面的此属性中获取值。

本文链接:https://www.f2er.com/3109794.html

大家都在问