Call code behind method on Ajax FileUpload Complete in ASP.Net

Last Reply 10 months ago By dharmendr

Posted 10 months ago

hello,

i have ajax file upload control to file upload it works fine. i want to call another c# method on file upload control

here is the my code

<asp:AjaxFileUpload ID="AjaxFileUpload11" runat="server" MaximumNumberOfFiles="5"
    Width="400px" OnUploadComplete="OnUploadComplete" />

 

protected void OnUploadComplete(object sender, AjaxFileUploadEventArgs e)
{
    string fileName = Path.GetFileName(e.FileName);
    AjaxFileUpload11.SaveAs(Server.MapPath("~/Upload/" + fileName));

    // i wan to call my below method after file upload but it does not called after file upload
}

 

protected void callthismethod()
{
    my code here//
}

 

Posted 10 months ago

Hi nauna,

You have to use OnClientUploadComplete event to define a javascript function to call the Code Behind function.

For calling code behind method refer below article.

Call Code Behind function from Client Side (JavaScript) in ASP.Net

Refer the below example.

HTML

<asp:ToolkitScriptManager runat="server" EnablePageMethods="true">
</asp:ToolkitScriptManager>
<asp:AjaxFileUpload ID="AjaxFileUpload11" runat="server" MaximumNumberOfFiles="5"
    Width="400px" OnUploadComplete="OnUploadComplete" OnClientUploadComplete="uploadComplete" />
<br />
<asp:Label ID="lblMesg" runat="server" />
<script type="text/javascript">
    function uploadComplete(sender) {
        PageMethods.GetCurrentTime(OnSuccess);
    }
    function OnSuccess(response, userContext, methodName) {
        $get("<%=lblMesg.ClientID%>").innerHTML = response;
    }
</script>

C#

protected void OnUploadComplete(object sender, AjaxFileUploadEventArgs e)
{
    string fileName = Path.GetFileName(e.FileName);
    string filePath = "~/Uploads/" + fileName;
    AjaxFileUpload11.SaveAs(Server.MapPath("~/Uploads/" + fileName));
}

[System.Web.Services.WebMethod]
public static string GetCurrentTime()
{
    return "The Current Time is: " + DateTime.Now.ToString();
}

Screenshot