noblerare
noblerare

Reputation: 11923

How to display success and error messages after postback?

I am using ASP.NET and C#.

I have a form in which the user can upload a file to the server. It does a client-side validation check with Javascript to make sure it is the correct file type, etc. If it passes that, control is handed off to the aspx.cs codebehind file. After the upload happens, I want to display a success notification on the same page or an error notification if any error is thrown.

How do I create a variable that can be passed back to the aspx file and displayed?

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
            }
        }


    }
}

Upvotes: 0

Views: 2898

Answers (1)

prospector
prospector

Reputation: 3469

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.fileupload.saveas%28v=vs.110%29.aspx

This link has the upload success message.

<asp:Label id="UploadStatusLabel" runat="server"> </asp:Label>

if successful use UploadStatusLabel.Forecolor = green

Upvotes: 2

Related Questions