![](/img/trans.png)
[英]Display error or success messages into a view after inserting data into a form MVC
[英]How to display success and error messages after postback?
我正在使用ASP.NET和C#。
我有一個表單,用戶可以在其中將文件上傳到服務器。 它使用Javascript進行客戶端驗證檢查,以確保它是正確的文件類型,等等。如果通過驗證,則將控制權移交給aspx.cs
隱藏文件。 上傳發生后,我想在同一頁面上顯示成功通知,或者如果引發任何錯誤,則顯示錯誤通知。
如何創建一個可以傳遞回aspx
文件並顯示的變量?
upload.aspx
<html>
<%@ Page Language="C#" CodeFile="upload.aspx.cs" Inherits="Upload" %>
...
<head>
<script language="Javascript">
function validate()
{
var filter = /<stuff>/;
var file1 = document.getElementById("uploadfile1").value;
var file2 = document.getElementById("uploadfile2").value;
var file3 = document.getElementById("uploadfile3").value;
//validation code to make sure uploaded file is legit
if (success)
{
return true;
}
else
{
alert("File not legit. Please correct.");
return false;
}
}
</script>
</head>
<body>
//I want to put something here like:
//<% if success is given, display "Successful Upload" in green %>
//<% if failure is given, display "Error Uploading" in red %>
<form method="post" runat="server" action="upload.aspx" name="upload" enctype="multipart/form-data">
<asp:FileUpload ID="uploadfile1" runat="server" />
<asp:FileUpload ID="uploadfile2" runat="server" />
<asp:FileUpload ID="uploadfile3" runat="server" />
<asp:Button ID="btnUpload" runat="server" Text="Upload" onClientClick="return validate()" onClick="btnUpload_Click" />
</form>
</body>
</html>
upload.aspx.cs
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Upload_File : System.Web.UI.Page
{
protected void btnUpload_Click(object sender, EventArgs e)
{
FileUpload[] files = new FileUpload[3];
files[0] = uploadfile1;
files[1] = uploadfile2;
files[2] = uploadfile3;
string rootpath = "C:\\path\\to\\directory\\";
for(int i = 0; i < 3; i++)
{
if(files[i].HasFile)
{
files[i].SaveAs(rootpath + files[i].FileName);
//I want to put something here like:
//var uploadTime = getTimeStamp();
//var VariableToPass = "Files uploaded at " + uploadTime;
//also, a way to catch error thrown and set success/error boolean
}
}
}
}
該鏈接包含上傳成功消息。
<asp:Label id="UploadStatusLabel" runat="server"> </asp:Label>
如果成功,則使用UploadStatusLabel.Forecolor = green
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.