我正在使用Ajax文件上传将一些图像插入到我的数据库中,我也使用数据列表来显示这些图像。
<ajaxToolkit:AjaxFileUpload ID="AjaxFileUpload1" runat="server" Height="141px" Width="640px" MaximumNumberOfFiles="100" OnUploadStart="AjaxFileUpload1_UploadStart" OnUploadComplete="AjaxFileUpload1_UploadComplete" OnUploadCompleteAll="AjaxFileUpload1_UploadCompleteAll" />
和数据列表
<asp:DataList ID="dlImages" runat="server" RepeatColumns="3" CellPadding="5"> <ItemTemplate> <a id="imageLink" href='<%# Eval("ImageName","~/SlideImages/{0}") %>' title='<%#Eval("Description") %>' rel="prettyPhoto[pp_gal]" runat="server" > <asp:Image ID="Image1" ImageUrl='<%# Bind("ImageName", "~/SlideImages/{0}") %>' runat="server" Width="112" Height="84" /> </a> </ItemTemplate> <ItemStyle BorderColor="Brown" BorderStyle="dotted" BorderWidth="3px" HorizontalAlign="Center" VerticalAlign="Bottom" /> </asp:DataList>
以及后面的代码如下: -
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindDataList();
}
}
protected void BindDataList()
{
con.Open();
SqlCommand command = new SqlCommand("SELECT ImageName,Description from SlideShowTable", con);
SqlDataAdapter da = new SqlDataAdapter(command);
DataTable dt = new DataTable();
da.Fill(dt);
dlImages.DataSource = dt;
dlImages.DataBind();
con.Close();
}
protected void AjaxFileUpload1_UploadComplete(object sender, AjaxControlToolkit.AjaxFileUploadEventArgs e)
{
string fileName = Path.GetFileName(e.FileName);
string filePath = "~/SlideImages/";
AjaxFileUpload1.SaveAs(MapPath(filePath + System.IO.Path.GetFileName(e.FileName)));
con.Open();
SqlCommand cmd = new SqlCommand("Insert into SlideShowTable(ImageName,Description) values(@ImageName,@Description)", con);
cmd.Parameters.AddWithValue("@ImageName", fileName);
cmd.Parameters.AddWithValue("@Description", "Some Description");
cmd.ExecuteNonQuery();
con.Close();
BindDataList();
}
protected void AjaxFileUpload1_UploadCompleteAll(object sender, AjaxControlToolkit.AjaxFileUploadCompleteAllEventArgs e)
{
Response.Redirect("~/Demo.aspx");
Server.TransferRequest(Request.Url.AbsolutePath, false);
Response.Redirect(Request.Url.AbsoluteUri);
}
现在的问题是,在使用传统的ASP文件上传后,控制回发并自动刷新页面,我的数据列表绑定,上传后数据列表会更新新图像。但是在使用Ajax文件上传时,尽管我尝试了不同的事情,但没有回发并且没有刷新发生
Response.Redirect("~/Demo.aspx");
Server.TransferRequest(Request.Url.AbsolutePath, false);
Response.Redirect(Request.Url.AbsoluteUri);
但仍然无法刷新页面,我必须手动刷新页面才能在我的页面上获取更新的数据列表。任何帮助或解决方案表示赞赏。谢谢。