如何在Kentico Custom BizForm中上传多个文件?

时间:2014-10-25 20:04:36

标签: asp.net file-upload kentico

我有一个创建为ascx控件的Form.Data以biz形式成功保存。我的问题是如何上传文件?我不知道我使用哪种控件正确使用上传文件?以及如何添加文件上传提交到这里rec.SetValue(" UploadCV",duUploadCV.InnerAttachmentGUID);你可以在下面看到点击按钮代码。我在下面有代码ascx和.cs。我使用的ascx上传控件是对还是不对?设计代码如下。代码.cs是

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CMS.Base;
using CMS.DataEngine;
using CMS.DocumentEngine;
using CMS.EmailEngine;
using CMS.EventLog;
using CMS.FormControls;
using CMS.FormEngine;
using CMS.Helpers;
using CMS.Localization;
using CMS.MacroEngine;
using CMS.Membership;
using CMS.PortalControls;
using CMS.PortalEngine;
using CMS.Protection;
using CMS.SiteProvider;
using CMS.WebAnalytics;
using System.Data;
using CMS.GlobalHelper;
using CMS.TreeEngine;
using CMS.CMSHelper;
using CMS.SiteProvider;
using CMS.EmailEngine;
using CMS.EventLog;
using CMS.DataEngine;
using CMS.WebAnalytics;
using CMS.LicenseProvider;
using CMS.PortalEngine;
using CMS.SettingsProvider;
using CMS.IDataConnectionLibrary;

using CMS.OnlineForms;
using CMS.DataEngine;
using CMS.SiteProvider;
using CMS.Helpers;


using CMS.FormEngine;
using CMS.SettingsProvider;
using CMS.DataEngine;
using CMS.GlobalHelper;

using CMS.ExtendedControls;
using CMS.Helpers;
using CMS.UIControls;
using System.Text;
using System.Web.UI.WebControls;
public partial class CMSWebParts_ePortalWebPart_ePortalControl_GraduateProgramApplication : CMSAbstractWebPart
{
    //public static int SaveSignupForm(SignupFormModel model)

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            GetGender();
            GetUniversity();
            GetStudyArea();
            GetSpecializationEng();

        }

    }

    #region "Layout properties"
    public override string SkinID
    {
        get
        {
            return base.SkinID;
        }
        set
        {
            base.SkinID = value;
            SetSkinID(value);
        }
    }
    public string NameText
    {
        get
        {
            return DataHelper.GetNotEmpty(GetValue("NameText"), ResHelper.LocalizeString("{$GPAName$}"));


        }
        set
        {
            SetValue("NameText", value);
            lblName.Text = value;

        }
    }

    #endregion
    public string DOBText
    {
        get
        {
            return DataHelper.GetNotEmpty(GetValue("DOBText"), ResHelper.LocalizeString("{$GPADateOfBirth$}"));

        }
        set
        {
            this.SetValue("LastNameText", value);
            lblDOB.Text = value;
        }
    }
    public string EmailText
    {
        get
        {
            return DataHelper.GetNotEmpty(this.GetValue("EmailText"), ResHelper.LocalizeString("{$Webparts_Membership_RegistrationForm.Email$}"));
        }
        set
        {
            this.SetValue("EmailText", value);
            lblEmail.Text = value;
        }
    }
    public string Submit
    {
        get
        {
            return DataHelper.GetNotEmpty(GetValue("Submit"), ResHelper.LocalizeString("{$custom.custom.Submit$}"));

        }
        set
        {
            this.SetValue("Submit", value);
            btnOk.Text = value;
        }
    }
    public string Gender
    {
        get
        {
            return DataHelper.GetNotEmpty(GetValue("Gender"), ResHelper.LocalizeString("{$GPAGender$}"));

        }
        set
        {
            this.SetValue("Gender", value);
            lblGender.Text = value;
        }
    }
    public string Email
    {
        get
        {
            return DataHelper.GetNotEmpty(GetValue("Email"), ResHelper.LocalizeString("{$GPAEmail$}"));

        }
        set
        {
            this.SetValue("Email", value);
            lblEmail.Text = value;
        }
    }
    public string GSMNumber
    {
        get
        {
            return DataHelper.GetNotEmpty(GetValue("GSMNumber"), ResHelper.LocalizeString("{$GPAGSMNumber$}"));

        }
        set
        {
            this.SetValue("GSMNumber", value);
            lblGSMNumber.Text = value;
        }
    }
    public string Grade
    {
        get
        {
            return DataHelper.GetNotEmpty(GetValue("Grade"), ResHelper.LocalizeString("{$GPAGrade$}"));

        }
        set
        {
            this.SetValue("Grade", value);
            lblGrade.Text = value;
        }
    }
    public string ExpectGraduateDate
    {
        get
        {
            return DataHelper.GetNotEmpty(GetValue("ExpectGraduateDate"), ResHelper.LocalizeString("{$GPAExpectGraduateDate$}"));

        }
        set
        {
            this.SetValue("ExpectGraduateDate", value);
            lblExpectGraduateDate.Text = value;
        }
    }
    public string University
    {
        get
        {
            return DataHelper.GetNotEmpty(GetValue("University"), ResHelper.LocalizeString("{$GPAUniversity$}"));

        }
        set
        {
            this.SetValue("University", value);
            lblUniversity.Text = value;
        }
    }
    public string StudyArea
    {
        get
        {
            return DataHelper.GetNotEmpty(GetValue("StudyArea"), ResHelper.LocalizeString("{$GPAStudyArea$}"));

        }
        set
        {
            this.SetValue("StudyArea", value);
            lblStudyArea.Text = value;
        }
    }
    public string Specialization
    {
        get
        {
            return DataHelper.GetNotEmpty(GetValue("Specialization"), ResHelper.LocalizeString("{$GPASpecialization$}"));

        }
        set
        {
            this.SetValue("Specialization", value);
            lblSpecialization.Text = value;
        }
    }
    public string WhyJoinPAEW
    {
        get
        {
            return DataHelper.GetNotEmpty(GetValue("WhyJoinPAEW"), ResHelper.LocalizeString("{$GPAWhyJoinPAEW$}"));

        }
        set
        {
            this.SetValue("WhyJoinPAEW", value);
            lblWhyJoinPAEW.Text = value;
        }
    }
    public string UploadCV
    {
        get
        {
            return DataHelper.GetNotEmpty(GetValue("UploadCV"), ResHelper.LocalizeString("{$GPAUploadCV$}"));

        }
        set
        {
            this.SetValue("UploadCV", value);
            lblUploadCV.Text = value;
        }
    }
    public string Certificates
    {
        get
        {
            return DataHelper.GetNotEmpty(GetValue("Certificates"), ResHelper.LocalizeString("{$GPACertificates$}"));

        }
        set
        {
            this.SetValue("Certificates", value);
            lblCertificates.Text = value;
        }
    }

    protected void SetupControl()
    {
        if (this.StopProcessing)
        {
            // Do not process
            rfvName.Enabled = false;
            rfvDOB.Enabled = false;
            rfvEmail.Enabled = false;
            revEmail.Enabled = false;
            rfvGSMNumber.Enabled = false;
            revMobile.Enabled = false;
            rvGrade.Enabled = false;
            rfvGrade.Enabled = false;
            rvExpectGraduateDate.Enabled = false;
            rfvExpectGraduateDate.Enabled = false;
            rfvUniversity.Enabled = false;
            rfvWhyJoinPAEW.Enabled = false;
           // rfvUploadCV.Enabled = false;
           // rfvCertificates.Enabled = false;
        }
        else
        {
            // Set texts
            lblName.Text = this.NameText;
            lblDOB.Text = this.DOBText;
            btnOk.Text = this.Submit;
            lblGender.Text = this.Gender;
            lblEmail.Text = this.Email;
            lblGSMNumber.Text = this.GSMNumber;
            lblGrade.Text = this.Grade;
            lblExpectGraduateDate.Text = this.ExpectGraduateDate;
            lblUniversity.Text = this.University;
            lblStudyArea.Text = this.StudyArea;
            lblSpecialization.Text = this.Specialization;
            lblWhyJoinPAEW.Text = this.WhyJoinPAEW;
            lblUploadCV.Text = this.UploadCV;
            lblCertificates.Text = this.Certificates;
            //lddSpecialization.Items.Insert(0, new ListItem(ResHelper.GetString(ResHelper.LocalizeString("{$GPASpecialization$}"),CultureHelper.GetPreferredCulture())));
           // BizForm1.OnAfterSave += new BizForm.OnAfterSaveEventHandler(GetGender());
           // ObjectEvents.Insert.After += new EventHandler<ObjectEventArgs>(Insert_After);


            // Set required field validators texts
            rfvName.ErrorMessage = ResHelper.GetString("GPArfv");
            rfvDOB.ErrorMessage = ResHelper.GetString("GPArfv");
            rfvEmail.ErrorMessage = ResHelper.GetString("GPArfv");
            revEmail.ErrorMessage = ResHelper.GetString("GPArfv");
            rfvGSMNumber.ErrorMessage = ResHelper.GetString("GPArfv");
            revMobile.ErrorMessage = ResHelper.GetString("GPArevMobile");
            rvGrade.ErrorMessage = ResHelper.GetString("GPAGradeLess");
            rfvGrade.ErrorMessage = ResHelper.GetString("GPArfv");
            rvExpectGraduateDate.ErrorMessage = ResHelper.GetString("GPArvExpectGraduateDate");
            rfvExpectGraduateDate.ErrorMessage = ResHelper.GetString("GPArfv");
            rfvUniversity.ErrorMessage = ResHelper.GetString("GPArfv");
            rfvWhyJoinPAEW.ErrorMessage = ResHelper.GetString("GPArfv");
           // rfvUploadCV.ErrorMessage = ResHelper.GetString("Webparts_Membership_RegistrationForm.rfvLastName");
          //  rfvCertificates.ErrorMessage = ResHelper.GetString("Webparts_Membership_RegistrationForm.rfvLastName");
            // Set SkinID
            if (!this.StandAlone && (this.PageCycle < PageCycleEnum.Initialized))
            {
                SetSkinID(this.SkinID);
            }


        }
    }

    void SetSkinID(string skinId)
    {
        if (skinId != "")
        {
            lblName.SkinID = skinId;
            lblDOB.SkinID = skinId;

        }
    }

    public override void OnContentLoaded()
    {
        base.OnContentLoaded();
        SetupControl();

    }


    /// <summary>
    /// Reloads data
    /// </summary>
    public override void ReloadData()
    {
        base.ReloadData();
        SetupControl();
    }

         /////////////////// Append Gender Radio List ///////////////////
    private void GetGender()
    {
            Dictionary<string, string> Genderdic = new Dictionary<string, string>();
            Genderdic.Add("1", "Male"); Genderdic.Add("2", "Female");
            rvGender.DataSource = Genderdic;
            rvGender.DataValueField = "Key";
            rvGender.DataTextField = "Value";
            rvGender.DataBind();
            rvGender.SelectedValue = "1";
      }
           /////////////////// Append Gender Radio List ///////////////////
            private void GetUniversity()
            {
            Dictionary<string, string> Universitydic = new Dictionary<string, string>();
            Universitydic.Add("1", "Sultan Qaboos University"); Universitydic.Add("2", "Higher College of Technology"); Universitydic.Add("3", "Other");
            ddlUniversity.DataSource = Universitydic;
            ddlUniversity.DataValueField = "Key";
            ddlUniversity.DataTextField = "Value";
            ddlUniversity.DataBind();
            }

            private void GetStudyArea()
            {
                Dictionary<string, string> StudyAreadic = new Dictionary<string, string>();
                StudyAreadic.Add("1", "Engineering"); StudyAreadic.Add("2", "Business");
                rblStudyArea.DataSource = StudyAreadic;
                rblStudyArea.DataValueField = "Key";
                rblStudyArea.DataTextField = "Value";
                rblStudyArea.DataBind();
                rblStudyArea.SelectedValue = "1";
            }
            private void GetSpecializationEng()
            {
                Dictionary<string, string> Specializationdic = new Dictionary<string, string>();
                if (rblStudyArea.SelectedValue == "1")
                {
                Specializationdic.Add("1", "Mechanical"); Specializationdic.Add("2", "Instrumentation"); Specializationdic.Add("3", "Civil"); Specializationdic.Add("4", "Chemistry"); Specializationdic.Add("5", "Water Resources"); Specializationdic.Add("6", "Other");
                }
                else
                { 
                    Specializationdic.Add("1", "Finance"); Specializationdic.Add("2", "HR"); Specializationdic.Add("3", "Communication"); Specializationdic.Add("4", "BA"); Specializationdic.Add("5", "Other");
                }
                lddSpecialization.DataSource = Specializationdic;
                lddSpecialization.DataValueField = "Key";
                lddSpecialization.DataTextField = "Value";
                lddSpecialization.DataBind();
                lddSpecialization.SelectedValue = "1";
            }
            protected void ddlUniversity_SelectedIndexChanged(object sender, EventArgs e)
            {
                if (ddlUniversity.SelectedValue == "3")
                { txtUniversity.Visible = true;}
                else
                { txtUniversity.Visible = false;}

            }
            protected void rblStudyArea_SelectedIndexChanged(object sender, EventArgs e)
            {
                GetSpecializationEng();
            }
            protected void lddSpecialization_SelectedIndexChanged(object sender, EventArgs e)
            {
                if (rblStudyArea.SelectedValue == "1")
                {
                    if (lddSpecialization.SelectedValue == "6")
                    { txtSpecialization.Visible = true; }
                    else
                    { txtSpecialization.Visible = false; }
                }
                else
                {
                    if (lddSpecialization.SelectedValue == "5")
                    { txtSpecialization.Visible = true; }
                    else
                    { txtSpecialization.Visible = false; }

                }
            }
           protected void btnOk_Click(object sender, EventArgs e)
            {

                    if ((this.PageManager.ViewMode == ViewModeEnum.Design) || (this.HideOnCurrentPage) || (!this.IsVisible))
                    {
                        // Do not process
                    }
                    else
                    {
                        rfvName.Validate();
                        rfvDOB.Validate();
                        rfvEmail.Validate();
                        revEmail.Validate();
                        rfvGSMNumber.Validate();
                        revMobile.Validate();
                        rvGrade.Validate();
                        rfvGrade.Validate();
                        rvExpectGraduateDate.Validate();
                        rfvExpectGraduateDate.Validate();
                        rfvUniversity.Validate();
                        rfvWhyJoinPAEW.Validate();
                        Page.Validate();
                        //if (Page.IsValid)
                        //{
                        String siteName = SiteContext.CurrentSiteName;


                        #region "Banned IPs"

                        // Ban IP addresses which are blocked for registration
                        if (!BannedIPInfoProvider.IsAllowed(siteName, BanControlEnum.Registration))
                        {
                            lblError.Visible = true;
                            lblError.Text = GetString("banip.ipisbannedregistration");
                            return;
                        }

                        #endregion

                        BizFormInfo formObject = BizFormInfoProvider.GetBizFormInfo("GraduateProgramApplication", SiteContext.CurrentSiteID);
                        if (formObject != null)
                        {
                            DataClassInfo formClass = DataClassInfoProvider.GetDataClassInfo(formObject.FormClassID);
                            if (formClass != null)
                            {
                                BizFormItemProvider bProvider = new BizFormItemProvider();
                                BizFormItem rec = BizFormItem.New(formClass.ClassName, null);
                                DateTime Applicant_DOB = DateTime.ParseExact(txtDOB.Text.Trim(), "dd/MM/yyyy", null);
                                DateTime Applicant_ExpectGraduateDate = DateTime.ParseExact(txtExpectGraduateDate.Text.Trim(), "MM/dd/yyyy", null);
                                rec.SetValue("FullName", txtName.Text.Trim());
                                rec.SetValue("DOB", Applicant_DOB);
                                rec.SetValue("Gender", rvGender.SelectedItem.Text.Trim());
                                rec.SetValue("Email", txtEmail.Text.Trim());
                                rec.SetValue("GSMNumber", txtGSMNumber.Text.Trim());
                                rec.SetValue("Grade", txtGrade.Text.Trim());
                                rec.SetValue("ExpectGraduateDate", Applicant_ExpectGraduateDate);
                                rec.SetValue("University", ddlUniversity.SelectedItem.Text.Trim());
                                rec.SetValue("StudyArea", rblStudyArea.SelectedItem.Text.Trim());
                                rec.SetValue("Specialization", lddSpecialization.SelectedItem.Text.Trim());
                                rec.SetValue("WhyPAEW", txtWhyJoinPAEW.Text.Trim());
                                // rec.SetValue("UploadCV", duUploadCV.InnerAttachmentGUID);

                                // BasicForm.Data.GetValue("answerText"), "");
                                rec.SetValue("FormInserted", DateTime.Now);
                                rec.SetValue("FormUpdated", DateTime.Now);
                                rec.Insert();
                                BizFormInfoProvider.RefreshDataCount(formObject.FormName, formObject.FormSiteID);

                            }

                        }
                        string test = "Hello";
                        Response.Write("<script>alert('" + test + "');</script>");
                    }

            }

}

1 个答案:

答案 0 :(得分:0)

我认为你需要触发直接上传器以某种方式保存文件,然后它会给你一个GUID - 我不完全熟悉它是如何工作的,因为我从来没有在它的上下文之外使用它Kentico表格引擎。

话虽这么说,我认为Kentico中的Forms模块可以完成我在这里尝试做的所有事情,而无需编写自定义代码。

每当我在Kentico中创建表单时,我都会尝试尽可能地利用表单模块。

我建议您阅读表单模块的文档,了解您正在使用的任何版本的Kentico,以尝试通过用户界面创建表单,然后询问问题,如果您在此过程中遇到问题 - 除非有什么习惯,我误解了。