我在DataList控件中放置了一个Label9和一个ImageButton2,前者用于显示下载的次数,后者用来点击下载指定的文件。下面的ImageButton2_Click()函数是点击下载的事件代码,而downlist1_ItemCommand()是DataList1的事件代码,使数据库中的'点击次数'字段加1,其中的Page_Load(source, e);是想刷新页面以显示最新的下载次数的。然而运行起来后,点击DataList1中的ImageButton2可以正常实现下载指定文件,但并不能更新数据库最新的'下载次数'信息,请问问题出在哪里?谢谢! protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
{
ImageButton imageButton = sender as ImageButton;
string filepath = Server.MapPath("~/downloads/") + imageButton.AlternateText;
FileInfo fi = new FileInfo(filepath);
if (fi.Exists)
{
Page.Response.Clear();
bool success = ResponseFile(Page.Request, Page.Response, fi.Name, filepath, 1024000); //ResponseFile()函数是下载文件函数
if (!success)
{
Response.Write("<script>alert('下载文件出错!');</script>");
Page.Response.End();
}
}
}
protected void downlist1_ItemCommand(object source, DataListCommandEventArgs e)
{
int recid = Convert.ToInt32(this.downlist1.DataKeys[e.Item.ItemIndex]);
sqldata.ExecSQL("Update tb_downloads Set downcount = downcount + 1 where id = '" + recid + "'");
}
{
ImageButton imageButton = sender as ImageButton;
string filepath = Server.MapPath("~/downloads/") + imageButton.AlternateText;
FileInfo fi = new FileInfo(filepath);
if (fi.Exists)
{
Page.Response.Clear();
bool success = ResponseFile(Page.Request, Page.Response, fi.Name, filepath, 1024000); //ResponseFile()函数是下载文件函数
if (!success)
{
Response.Write("<script>alert('下载文件出错!');</script>");
Page.Response.End();
}
}
}
protected void downlist1_ItemCommand(object source, DataListCommandEventArgs e)
{
int recid = Convert.ToInt32(this.downlist1.DataKeys[e.Item.ItemIndex]);
sqldata.ExecSQL("Update tb_downloads Set downcount = downcount + 1 where id = '" + recid + "'");
}
/////////
去掉Page_Load(source, e);
在执行下面的代码后
int recid = Convert.ToInt32(this.downlist1.DataKeys[e.Item.ItemIndex]);
sqldata.ExecSQL("Update tb_downloads Set downcount = downcount + 1 where id = '" + recid + "'");
重新绑定一下数据源
你说的问题出现的原因应该是在更新了数据之后没有重新绑定数据源