我在UpdatePanel中使用输入文件,它运行良好。但使用UpdatePanel PostBackTrigger,ScriptManager.RegisterStartupScript无法正常工作。
如果我不使用PostBackTrigger,则ScriptManager.RegisterStartupScript可以正常工作,但输入文件不会。
我使用带有C#的ASP Web Forms
这是源代码。
的.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="UploadFile.aspx.cs" Inherits="WebApplication1.UploadFile" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="up_upload" runat="server" UpdateMode="Conditional">
<Triggers>
<asp:PostBackTrigger ControlID="btn_upload" />
</Triggers>
<ContentTemplate>
<input type="file" id="file_test" runat="server" />
<asp:Button ID="btn_upload" runat="server" Text="Subir" CausesValidation="False" OnClick="btn_upload_Click" />
</ContentTemplate>
</asp:UpdatePanel>
</form>
</body>
</html>
的.cs
protected void btn_upload_Click(object sender, EventArgs e)
{
try
{
string message = "file not uploaded!";
if (file_test.PostedFile != null)
{
string fn = file_test.PostedFile.FileName;
string only_path = Server.MapPath(".");
file_test.PostedFile.SaveAs(only_path + "//Images//" + fn);
ScriptManager.RegisterStartupScript(up_upload, up_upload.GetType(), Guid.NewGuid().ToString(), "alert('server path: ' + '" + only_path.Replace("\\", "\\\\") + "');", true);
message = "file uploaded!";
}
ScriptManager.RegisterStartupScript(up_upload, up_upload.GetType(), Guid.NewGuid().ToString(), "alert('" + message + "');", true);
up_upload.Update();
}
catch (Exception ex)
{
ScriptManager.RegisterStartupScript(up_upload, up_upload.GetType(), Guid.NewGuid().ToString(), "alert('" + ex.Message + "');", true);
}
}
答案 0 :(得分:1)
解决!当我回发整页时,我使用了ClientScript.RegisterStartupScript而不是ScriptManager.RegisterstartupScript。
ClientScript.RegisterStartupScript(up_upload.GetType(),
Guid.NewGuid().ToString(), "<script>alert('" + message + "');</script>");
感谢您的帮助!
答案 1 :(得分:0)