0

Currently I have a web control and backend code for this control (as well as a designer etc).

To upload it as a macro to Umbraco though I need to condense this all into one file. How would I go about this without breaking something? Currently when I copy paste the code in with tags all the else and if's break.

web form

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="UploadFileControl.ascx.cs" Inherits="FileUpload.UploadFileControl" %>

    <form id="UploadForm" runat="server">
<asp:FileUpload id="FileUploadControl" runat="server" />
    <asp:Button runat="server" id="UploadButton" text="Upload" onclick="UploadButton_Click" />
    <br /><br />
    <asp:Label runat="server" id="StatusLabel" text="Upload status: " />
</form>

And the user control backend

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


namespace FileUpload
{
    //this backend for the web control will be used to upload a file that will have it's XML tags pulled and displayed on a page. 
    //this code checks if the fileupload control has input inside it, then proceeds to the next page with the document saved.
    public partial class UploadFileControl : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        //names the script manager which will be used when the user attempts to upload a form / gives an error if they incorrectly attempt to upload
        protected void UploadButton_Click(object sender, EventArgs e)
        {
            //if file is located
            if (FileUploadControl.HasFile)
            {
                try
                {
                    //allow content type of document / docx
                    if (FileUploadControl.PostedFile.ContentType == "document/docx")
                    {
                        //if the file is is less than 51mb
                        if (FileUploadControl.PostedFile.ContentLength < 2000)
                        {
                            //name the filename, find the path of the name
                            string filename = Path.GetFileName(FileUploadControl.FileName);
                            //path of server upload (we just need to save it as a variable to be found on the next page, as it will be made / deleted
                            FileUploadControl.SaveAs(Server.MapPath("~/") + filename);
                            //update the label with file uploaded
                            StatusLabel.Text = "Upload status: File uploaded!";
                        }
                        else
                            //display the size the file needs to be less than
                            StatusLabel.Text = "Upload status: The file has to be less than 2mb!";
                    }
                    else
                        //tell the user only docx files are accepted
                        StatusLabel.Text = "Upload status: Only DOCX files are accepted!";
                }
                catch (Exception ex)
                {
                    //display the exception message, in which case it would be either size / type / if it's present
                    StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
                }
            }
        }
    }
}
Alex
  • 673
  • 3
  • 9
  • 22

1 Answers1

3

One possibility is to move your codebehind content to the page itself, using <script runat="server"> tags. Like this:

SamplePage.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SamplePage.aspx.cs" Inherits="Test.SamplePage" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

    </div>
    </form>
</body>
</html>

SamplePage.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Test
{
    public partial class SamplePage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //Sample code.
        }
    }
}

Would translate into this:

SamplePage.aspx

<%@ Import Namespace="System.Web"%>
<%@ Import Namespace="System.Web.UI"%>
<%@ Import Namespace="System.Web.UI.WebControls"%>
<%@ Page Language="C#" %>

<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        //Sample code.
    }
</script>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
        </div>
    </form>
</body>
</html>

You may try and check if it works with .ascx content as well.

OnoSendai
  • 3,960
  • 2
  • 22
  • 46
  • That did work, but the only problem was adding the using namespaces to the server. At the moment I'm just calling the two files from each other in the directory so it works, but I'll have to look more into the namespace things for next time. – Alex Dec 09 '13 at 16:06
  • Ah, I see - I added the pertinent keyword, Import, to the sample. Hope it works for you, @Alex. – OnoSendai Dec 09 '13 at 16:13