有人可以帮忙吗...请!
我不断收到以下错误?
非静态字段,方法或属性'MyClass.SetImageUrl()'需要对象引用
这是代码...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace ThreeTierWebApp
{
class MyClass
{
public partial class Holidays : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SetImageUrl();
}
}
}
protected void Timer1_Tick(object sender, EventArgs e)
{
SetImageUrl();
}
private void SetImageUrl()
{
Random _rand = new Random();
int i = _rand.Next(9, 16);
Image5.ImageUrl = "~/Images/" + i.ToString() + ".jpg";
}
protected void MultiView1_ActiveViewChanged(object sender, EventArgs e)
{
}
}
}
最佳答案
更改
private void SetImageUrl()
{
Random _rand = new Random();
int i = _rand.Next(9, 16);
Image5.ImageUrl = "~/Images/" + i.ToString() + ".jpg";
}
至
public void SetImageUrl()
{
Random _rand = new Random();
int i = _rand.Next(9, 16);
Image5.ImageUrl = "~/Images/" + i.ToString() + ".jpg";
}
我同意您的代码很奇怪。您可能必须将方法设为静态并按如下方式调用它:
MyClass.SetImageUrl()