[英]how can I access my public method from aspx code behind to a public class?
我在aspx代码中有一个称为PhotoDatabinding的公共方法,它的作用是将数据库绑定到List视图控件。
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
}
}
然后在页面后面的代码中
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
var dataAccess = new PhotoAccess();
var items = dataAccess.GetPhotos();
lvSubAlbumDB.DataSource = items;
lvSubAlbumDB.DataBind();
}
}
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);
var items = dataAccess.GetPhotos();
lvSubAlbumDB.DataSource = items;
lvSubAlbumDB.DataBind();
}
您也可以将绑定代码重构为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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.