Click here to Skip to main content
13,199,669 members (40,553 online)
Click here to Skip to main content
Add your own
alternative version


14 bookmarked
Posted 25 May 2009

Confirm Box for File Replace using Client Callback

, 25 May 2009
Rate this:
Please Sign up or sign in to vote.
Ask user to replace a file or not


You are providing a facility for uploading files to your webserver. Client can upload any file. Client doesn't know whether the file he is uploading is already present on the webserver or not. So you need to check and ask to replace an existing file.

Once a file is chosen by the user, we need to check whether the file exists on the webserver or not.

A full postback for this will make the file lost from FileUpload control. As the page and its controls are re-created, the page code runs on the server, and a new version of the page is rendered to the browser.

A full postback will be done only to save the file.


To avoid losing client state and not incur the processing overhead of a server roundtrip, you can code an ASP.NET Web page so that it can perform client callbacks. In a client callback, a client-script function sends a request to an ASP.NET Web page. The Web page runs a modified version of its normal life cycle. The page is initiated and its controls and other members are created, and then a specially marked method is invoked. The method performs the processing that you have coded and then returns a value to the browser that can be read by another client script function. Throughout this process, the page is live in the browser.

Using the Code

<asp:FileUpload ID="FileUpload1" runat="server" />       
<input type="button" id="hBtn" value="Save" onclick="CheckFile()" />
<asp:Button ID="btnSave" runat="server" Text="Save" onclick="btnSave_Click" />
<asp:HiddenField ID="hdnIsOverwrite" runat="server" />
public partial class _Default : System.Web.UI.Page, 
    protected bool returnValue;
    protected void Page_Load(object sender, EventArgs e)
        // hide server side button
        btnSave.Style.Add(HtmlTextWriterStyle.Display, "none");
        String cbReference =
            "arg", "ReceiveServerData", "context","processError",false);
        String callbackScript;
        callbackScript = "function CallServer(arg, context)" +
            "{ " + cbReference + ";}";
            "CallServer", callbackScript, true);
    protected void btnSave_Click(object sender, EventArgs e)
    public void RaiseCallbackEvent(String eventArgument)
        //dummy processing
        //set return value
        returnValue = File.Exists(Server.MapPath(Path.GetFileName(eventArgument)));
    public String GetCallbackResult()
        return returnValue.ToString().ToLower();
 <script language="javascript" type="text/javascript">
function CheckFile()
    var fu= document.getElementById("FileUpload1");
    var btn = document.getElementById("hBtn");
        alert('Please select a file.');
        //fire request to server

function ReceiveServerData(rValue)
    var fu= document.getElementById("FileUpload1");
    var btnServer = document.getElementById("btnSave");
    var btn = document.getElementById("hBtn");

    //disable the button for further clicking

    if(rValue=='true') // file exists
        if(confirm('Are you sure you want to replace the file ?'))
            //save file , you can also use __doPostback()
        //save file , you can also use __doPostback();
function processError(rValue)

Points of Interest

  • A good use of ClientCallback


  • 25th May, 2009: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer
India India
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralFile Uploaded functionality With confirmation Pin
Amarpreet kamboj10-Dec-12 23:46
memberAmarpreet kamboj10-Dec-12 23:46 
GeneralAnother solution Pin
Florian DREVET3-Jun-09 11:01
memberFlorian DREVET3-Jun-09 11:01 
QuestionWill it work if client and server are on different machine? Pin
Sohel_Rana25-May-09 21:02
memberSohel_Rana25-May-09 21:02 
AnswerRe: Will it work if client and server are on different machine? Pin
rcravens2-Jun-09 7:47
memberrcravens2-Jun-09 7:47 
GeneralI prefer... Pin
Seishin#25-May-09 9:01
memberSeishin#25-May-09 9:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171020.1 | Last Updated 25 May 2009
Article Copyright 2009 by thinkmmk
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid