Home » ASP.net | charity | DevExpress

Saving control values when using a callback to upload a file

21. January 2009 by Dave Hawes 0 Comments

This is my ASP.net webform:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="AddPortfolioEvidenceControl.ascx.cs" Inherits="MyApplication.Web.RegisteredUsers.Portfolios.AddPortfolioEvidenceControl" %>
<%@ Register Assembly="DevExpress.Web.v8.2, Version=8.2.3.0, Culture=neutral, PublicKeyToken=9b171c9fd64da1d1"
    Namespace="DevExpress.Web.ASPxCallback" TagPrefix="dxcb" %>
<%@ Register Assembly="DevExpress.Web.ASPxEditors.v8.2, Version=8.2.3.0, Culture=neutral, PublicKeyToken=9b171c9fd64da1d1"
    Namespace="DevExpress.Web.ASPxEditors" TagPrefix="dxe" %>
<%@ Register Assembly="DevExpress.Web.v8.2, Version=8.2.3.0, Culture=neutral, PublicKeyToken=9b171c9fd64da1d1"
    Namespace="DevExpress.Web.ASPxUploadControl" TagPrefix="dxuc" %>
<%@ Reference VirtualPath="~/Default.master" %>
    
<dxcb:ASPxCallback ID="CallbackComplete" ClientInstanceName="Callback1" runat="server" OnCallback="CallbackComplete_Callback">
    <ClientSideEvents CallbackComplete="function(s, e) {uplImage.UploadFile();}" />
</dxcb:ASPxCallback>

<table>
<tr>
<td>Description:</td>
<td>
    <dxe:ASPxTextBox ID="txtEvidenceName2" runat="server" Width="400px">
    </dxe:ASPxTextBox>
</td>
</tr>
<tr>
<td>Valid From:</td>
<td>
<dxe:ASPxDateEdit ID="dtpValidFrom" runat="server" ClientInstanceName="dtpValidFrom" />
    
</td>
</tr>
<tr>
<td>Valid To:</td>
    
<td>
<dxe:ASPxDateEdit ID="dtpValidTo" runat="server" ClientInstanceName="dtpValidTo" />
</td>
</tr>
<tr>
<td>File:</td>
<td>
<dxuc:ASPxUploadControl ID="uplImage" runat="server" ClientInstanceName="uplImage"
    Size="35" OnFileUploadComplete="uplImage_FileUploadComplete"> 
    
    <ClientSideEvents FileUploadComplete="function(s, e) {refreshTabControl(); }"  />
    
</dxuc:ASPxUploadControl>


</td>
</tr>
<tr>
<td colspan="2">
<dxe:ASPxButton ID="btnUpload" runat="server" AutoPostBack="False" Text="Upload" 
        ClientInstanceName="btnUpload" Width="100px">
    <ClientSideEvents Click="function(s, e) { Callback1.PerformCallback(); }" />
</dxe:ASPxButton>
</td>
</tr>
</table>

 

This is my code behind:

 
using System;
using System.Drawing;
using DevExpress.Web.ASPxUploadControl;
using System.Web.UI;
using MyApplication.DAL.EntityClasses;

namespace MyApplication.Web.RegisteredUsers.Portfolios
{
    public partial class AddPortfolioEvidenceControl : MyAppUserControlBase
    {

        protected void CallbackComplete_Callback(object source, DevExpress.Web.ASPxCallback.CallbackEventArgs e)
        {
            PortfolioEvidenceBaseEntity evidence = new PortfolioEvidenceBaseEntity();
            evidence.Name = txtEvidenceName2.Text;
            evidence.ValidFrom = dtpValidFrom.Date;
            evidence.ValidTo = dtpValidTo.Date;
            
            Session["newUploadedEvidence"] = evidence;
            
        }

        protected void uplImage_FileUploadComplete(object sender, FileUploadCompleteEventArgs e)
        {
            e.CallbackData = SavePostedFile();
        }

        protected string SavePostedFile()
        {
            if (uplImage.IsValid && uplImage.HasFile)
            {

                PortfolioEvidenceBaseEntity evidence = (PortfolioEvidenceBaseEntity)Session["newUploadedEvidence"];
                

                try
                {
                   string resFileName = MapPath(UploadDirectory) + uplImage.fileName;
                   evidence.FileName =  resFileName;
                   evidence.FileType = uplImage.PostedFile.ContentType;
                   uplImage.SaveAs(evidence.FileType);
                }
                catch
                {
                    Session.Remove("newUploadedEvidence");
                    return "There was an error uploading file";
                }

                Session.Remove("newUploadedEvidence");
                evidence.Save();
                SelectedPortfolioItem.PorfolioEvedenceId = evidence.Id;
                SelectedPortfolioItem.Save();

                return "Completed";
                
            }
            return "No valid file found";
        }
    }
}
 
 
Comments are closed