C# 如何将我的公共方法从aspx code behind访问到公共类?
在我的aspx代码中有一个公共方法叫做PhotoDatabinding,它的作用是将数据库绑定到列表视图控件C# 如何将我的公共方法从aspx code behind访问到公共类?,c#,asp.net,C#,Asp.net,在我的aspx代码中有一个公共方法叫做PhotoDatabinding,它的作用是将数据库绑定到列表视图控件 public void PhotoDatabinding() { lnqPhotoDataContext dbCon = new lnqPhotoDataContext(); var res = from p in dbCon.Photos orderby p.PhotoID descending select new { p.PhotoID, p.FileName
public void PhotoDatabinding()
{
lnqPhotoDataContext dbCon = new lnqPhotoDataContext();
var res = from p in dbCon.Photos orderby p.PhotoID descending select new { p.PhotoID, p.FileName };
lvSubAlbumDB.DataSource = res;
lvSubAlbumDB.DataBind();
}
现在,在名为Process的公共类中,有一个名为UpdateSave的方法。我的问题是如何访问PhotoBinding方法,使其看起来像这样
public class Process
{
public UpdateSave()
{
....some code
PhotoDatabinding();
}
}
感谢并感谢您的帮助和建议。要清除流程,请执行以下操作:
您需要创建一个仅用于从数据库获取数据或更新数据的类
public class PhotoAccess
{
public class PhotoInfo
{
public int PhotoID {get; set;}
public string FileName {get; set;}
}
public IEnumerable<PhotoInfo> GetPhotos()
{
using ( var dbCon = new lnqPhotoDataContext())
{
var res = from p in dbCon.Photos
orderby p.PhotoID descending
select new PhotoInfo
{
p.PhotoID,
p.FileName
};
return res.AsEnumerable();
}
}
public bool UpdateSave(...)
{
... code to do update or save, use here only classes for working with the DB
}
}
您还可以将绑定代码重构为Page类的另一个方法
private void BindAlbum()
{
var dataAccess = new PhotoAccess();
var items = dataAccess.GetPhotos();
lvSubAlbumDB.DataSource = items;
lvSubAlbumDB.DataBind();
}
页面加载将是:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindAlbum();
}
}
和更新处理程序
protected void btSave_OnClick(object sender, EventArgs e)
{
var dataAccess = new PhotoAccess();
dataAccess.UpdateSave(...pass here the parameters or an object which is going to be inserted);
BindAlbum();
}
Process
与PhotoDatabinding
所在的类之间的关系是什么?该类是创建流程的实例还是创建创建该流程的类?通常,您的页面会使用该流程类,而不是相反。你在层与层之间打错了方向的电话。我同意David的观点,这是错误的方向的电话。David是正确的。我会重新考虑你的设计。Silverlight FileUpload控件有一个名为FileUploadProcess的类,可以将照片上载到服务器。我在aspx.cs中的PhotoBinding方法基本上是更新listview,用当前上传的照片更新页面。谢谢你们的评论和建议。嗨,阿德里安,首先我感谢你们提供的所有例子。为了让您更了解我试图实现的目标,在我的添加照片页面上,我有一个带有数据绑定的listview控件和一个silverlight[link](文件上载)控件,我无法调用PhotoDatabinding方法来更新“添加照片”页面中的Listview控件。必须有一种方法使控件引发一个事件,该事件表示图像已上载。确切地说。。。但我无法在silverlight FileUpload.xap控件上载按钮中引发任何事件。
protected void btSave_OnClick(object sender, EventArgs e)
{
var dataAccess = new PhotoAccess();
dataAccess.UpdateSave(...pass here the parameters or an object which is going to be inserted);
BindAlbum();
}