Click here to Skip to main content
15,891,951 members
Articles / Web Development / ASP.NET

Integrating Visual Studio LightSwitch Application Into An Existing Website using IFrames

Rate me:
Please Sign up or sign in to vote.
4.77/5 (22 votes)
25 Oct 2011Ms-PL13 min read 96.4K   992   40  
An advanced LightSwitch tutorial covering PreProcess Queries, Email, PopUps, WCF RIA Services, and HTML pages
  • DNNThingsForSaleFinal.zip
    • DNNData
    • DNNThingsForSaleFinal.sln
    • DNNThingsForSaleFinal
      • _Pvt_Extensions
        • Microsoft.LightSwitch.Extensions
          • Client
            • DebugOnlyRef
              • Microsoft.LightSwitch.Extensions.Design.Client.dll
          • ClientGen
            • Reference
              • ar
                • Microsoft.LightSwitch.Extensions.Client.Resources.dll
              • bg
                • Microsoft.LightSwitch.Extensions.Client.Resources.dll
              • ca
                • Microsoft.LightSwitch.Extensions.Client.Resources.dll
              • cs
                • Microsoft.LightSwitch.Extensions.Client.Resources.dll
              • da
                • Microsoft.LightSwitch.Extensions.Client.Resources.dll
              • de
                • Microsoft.LightSwitch.Extensions.Client.Resources.dll
              • el
                • Microsoft.LightSwitch.Extensions.Client.Resources.dll
              • es
                • Microsoft.LightSwitch.Extensions.Client.Resources.dll
              • et
                • Microsoft.LightSwitch.Extensions.Client.Resources.dll
              • eu
                • Microsoft.LightSwitch.Extensions.Client.Resources.dll
              • fi
                • Microsoft.LightSwitch.Extensions.Client.Resources.dll
              • fr
                • Microsoft.LightSwitch.Extensions.Client.Resources.dll
              • he
                • Microsoft.LightSwitch.Extensions.Client.Resources.dll
              • hr
                • Microsoft.LightSwitch.Extensions.Client.Resources.dll
              • hu
                • Microsoft.LightSwitch.Extensions.Client.Resources.dll
              • id
                • Microsoft.LightSwitch.Extensions.Client.Resources.dll
              • it
                • Microsoft.LightSwitch.Extensions.Client.Resources.dll
              • ja
                • Microsoft.LightSwitch.Extensions.Client.Resources.dll
              • ko
                • Microsoft.LightSwitch.Extensions.Client.Resources.dll
              • lt
                • Microsoft.LightSwitch.Extensions.Client.Resources.dll
              • lv
                • Microsoft.LightSwitch.Extensions.Client.Resources.dll
              • Microsoft.LightSwitch.Extensions.Client.dll
              • ms
                • Microsoft.LightSwitch.Extensions.Client.Resources.dll
              • nl
                • Microsoft.LightSwitch.Extensions.Client.Resources.dll
              • no
                • Microsoft.LightSwitch.Extensions.Client.Resources.dll
              • pl
                • Microsoft.LightSwitch.Extensions.Client.Resources.dll
              • pt
                • Microsoft.LightSwitch.Extensions.Client.Resources.dll
              • pt-BR
                • Microsoft.LightSwitch.Extensions.Client.Resources.dll
              • ro
                • Microsoft.LightSwitch.Extensions.Client.Resources.dll
              • ru
                • Microsoft.LightSwitch.Extensions.Client.Resources.dll
              • sk
                • Microsoft.LightSwitch.Extensions.Client.Resources.dll
              • sl
                • Microsoft.LightSwitch.Extensions.Client.Resources.dll
              • sr-Cyrl-CS
                • Microsoft.LightSwitch.Extensions.Client.Resources.dll
              • sr-Latn-CS
                • Microsoft.LightSwitch.Extensions.Client.Resources.dll
              • sv
                • Microsoft.LightSwitch.Extensions.Client.Resources.dll
              • th
                • Microsoft.LightSwitch.Extensions.Client.Resources.dll
              • tr
                • Microsoft.LightSwitch.Extensions.Client.Resources.dll
              • uk
                • Microsoft.LightSwitch.Extensions.Client.Resources.dll
              • vi
                • Microsoft.LightSwitch.Extensions.Client.Resources.dll
              • zh-Hans
                • Microsoft.LightSwitch.Extensions.Client.Resources.dll
              • zh-Hant
                • Microsoft.LightSwitch.Extensions.Client.Resources.dll
          • IDE
            • Reference
              • de
                • Microsoft.LightSwitch.Extensions.Design.Resources.dll
              • es
                • Microsoft.LightSwitch.Extensions.Design.Resources.dll
              • fr
                • Microsoft.LightSwitch.Extensions.Design.Resources.dll
              • it
                • Microsoft.LightSwitch.Extensions.Design.Resources.dll
              • ja
                • Microsoft.LightSwitch.Extensions.Design.Resources.dll
              • ko
                • Microsoft.LightSwitch.Extensions.Design.Resources.dll
              • Microsoft.LightSwitch.Extensions.Design.dll
              • ru
                • Microsoft.LightSwitch.Extensions.Design.Resources.dll
              • zh-Hans
                • Microsoft.LightSwitch.Extensions.Design.Resources.dll
              • zh-Hant
                • Microsoft.LightSwitch.Extensions.Design.Resources.dll
          • LsPkgLastWriteTime.txt
          • ServerGen
            • Reference
              • ar
                • Microsoft.LightSwitch.Extensions.Server.Resources.dll
              • bg
                • Microsoft.LightSwitch.Extensions.Server.Resources.dll
              • ca
                • Microsoft.LightSwitch.Extensions.Server.Resources.dll
              • cs
                • Microsoft.LightSwitch.Extensions.Server.Resources.dll
              • da
                • Microsoft.LightSwitch.Extensions.Server.Resources.dll
              • de
                • Microsoft.LightSwitch.Extensions.Server.Resources.dll
              • el
                • Microsoft.LightSwitch.Extensions.Server.Resources.dll
              • es
                • Microsoft.LightSwitch.Extensions.Server.Resources.dll
              • et
                • Microsoft.LightSwitch.Extensions.Server.Resources.dll
              • eu
                • Microsoft.LightSwitch.Extensions.Server.Resources.dll
              • fi
                • Microsoft.LightSwitch.Extensions.Server.Resources.dll
              • fr
                • Microsoft.LightSwitch.Extensions.Server.Resources.dll
              • he
                • Microsoft.LightSwitch.Extensions.Server.Resources.dll
              • hr
                • Microsoft.LightSwitch.Extensions.Server.Resources.dll
              • hu
                • Microsoft.LightSwitch.Extensions.Server.Resources.dll
              • id
                • Microsoft.LightSwitch.Extensions.Server.Resources.dll
              • it
                • Microsoft.LightSwitch.Extensions.Server.Resources.dll
              • ja
                • Microsoft.LightSwitch.Extensions.Server.Resources.dll
              • ko
                • Microsoft.LightSwitch.Extensions.Server.Resources.dll
              • lt
                • Microsoft.LightSwitch.Extensions.Server.Resources.dll
              • lv
                • Microsoft.LightSwitch.Extensions.Server.Resources.dll
              • Microsoft.LightSwitch.Extensions.Server.dll
              • ms
                • Microsoft.LightSwitch.Extensions.Server.Resources.dll
              • nl
                • Microsoft.LightSwitch.Extensions.Server.Resources.dll
              • no
                • Microsoft.LightSwitch.Extensions.Server.Resources.dll
              • pl
                • Microsoft.LightSwitch.Extensions.Server.Resources.dll
              • pt
                • Microsoft.LightSwitch.Extensions.Server.Resources.dll
              • pt-BR
                • Microsoft.LightSwitch.Extensions.Server.Resources.dll
              • ro
                • Microsoft.LightSwitch.Extensions.Server.Resources.dll
              • ru
                • Microsoft.LightSwitch.Extensions.Server.Resources.dll
              • sk
                • Microsoft.LightSwitch.Extensions.Server.Resources.dll
              • sl
                • Microsoft.LightSwitch.Extensions.Server.Resources.dll
              • sr-Cyrl-CS
                • Microsoft.LightSwitch.Extensions.Server.Resources.dll
              • sr-Latn-CS
                • Microsoft.LightSwitch.Extensions.Server.Resources.dll
              • sv
                • Microsoft.LightSwitch.Extensions.Server.Resources.dll
              • th
                • Microsoft.LightSwitch.Extensions.Server.Resources.dll
              • tr
                • Microsoft.LightSwitch.Extensions.Server.Resources.dll
              • uk
                • Microsoft.LightSwitch.Extensions.Server.Resources.dll
              • vi
                • Microsoft.LightSwitch.Extensions.Server.Resources.dll
              • zh-Hans
                • Microsoft.LightSwitch.Extensions.Server.Resources.dll
              • zh-Hant
                • Microsoft.LightSwitch.Extensions.Server.Resources.dll
      • app.config
      • Bin
        • Data
          • ApplicationDatabase.mdf
          • ApplicationDatabase_log.ldf
          • Temp
      • Client
      • ClientGenerated
      • Common
        • Bin
          • Application.Common.dll
          • Application.Common.pdb
          • de
            • Microsoft.CSharp.resources.dll
            • System.ComponentModel.Composition.resources.dll
          • es
            • Microsoft.CSharp.resources.dll
            • System.ComponentModel.Composition.resources.dll
          • fr
            • Microsoft.CSharp.resources.dll
            • System.ComponentModel.Composition.resources.dll
          • it
            • Microsoft.CSharp.resources.dll
            • System.ComponentModel.Composition.resources.dll
          • ja
            • Microsoft.CSharp.resources.dll
            • System.ComponentModel.Composition.resources.dll
          • ko
            • Microsoft.CSharp.resources.dll
            • System.ComponentModel.Composition.resources.dll
          • LSCodeGeneratorsTask.mcs
          • Microsoft.CSharp.dll
          • Microsoft.CSharp.xml
          • ru
            • Microsoft.CSharp.resources.dll
            • System.ComponentModel.Composition.resources.dll
          • System.ComponentModel.Composition.dll
          • System.ComponentModel.Composition.xml
          • temp
            • de
            • es
            • fr
            • it
            • ja
            • ko
            • ru
            • zh-Hans
            • zh-Hant
          • zh-Hans
            • Microsoft.CSharp.resources.dll
            • System.ComponentModel.Composition.resources.dll
          • zh-Hant
            • Microsoft.CSharp.resources.dll
            • System.ComponentModel.Composition.resources.dll
        • Common.csproj
        • Common.csproj.user
        • Properties
        • UserCode
      • Data
        • ApplicationDefinition.lsml
      • default.htm
      • DNNThingsForSaleFinal.lsproj
      • DNNThingsForSaleFinal.lsproj.user
      • Server
      • ServerGenerated
      • ServiceConfiguration.cscfg
      • ServiceDefinition.csdef
      • Silverlight.js
using DNNData;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text.RegularExpressions;
using System.Transactions;
using System.Web.UI;
using System.Web.UI.WebControls;
using LightSwitchApplication;

namespace ApplicationData.Implementation
{
    public partial class PublicPage : System.Web.UI.Page
    {
        #region CurrentPage
        public int CurrentPage
        {
            get
            {
                if (ViewState["CurrentPage"] == null)
                {
                    return 1;
                }
                else
                {
                    return (int)ViewState["CurrentPage"];
                }
            }
            set
            {
                ViewState["CurrentPage"] = value;
            }
        }
        #endregion

        #region CurrentPostID
        public int CurrentPostID
        {
            get
            {
                if (ViewState["CurrentPostID"] == null)
                {
                    return -1;
                }
                else
                {
                    return (int)ViewState["CurrentPostID"];
                }
            }
            set
            {
                ViewState["CurrentPostID"] = value;
            }
        }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                GetPosts(true, CurrentPage);
            }
        }

        #region GetPosts
        private void GetPosts(bool FillCategories, int intPage)
        {
            // Collections to hold the final results
            List<DNNData.Category> lstCategories = new List<DNNData.Category>();

            // Connect to DotNetNuke Data
            string _IntrinsicData = Convert.ToString(ConfigurationManager.ConnectionStrings["DotNetNuke5Entities"]);
            DotNetNuke5Entities db = new DotNetNuke5Entities(_IntrinsicData);

            int intCategoryID = -1;

            // Get selected category
            if (!FillCategories)
            {
                intCategoryID = Convert.ToInt32(ddlCategories.SelectedValue);
            }

            if (FillCategories)
            {
                // execute query
                // Note this method bypasses all LightSwitch security
                var colCatagories = (from Cat in db.Categories
                                     select Cat).ToList();

                // Add an All option
                DNNData.Category objCategory = new DNNData.Category();
                objCategory.Id = -1;
                objCategory.CategoryName = "All";
                lstCategories.Add(objCategory);

                // Add Categories
                foreach (var item in colCatagories)
                {
                    lstCategories.Add(item);
                }

                // We now have the results - bind then to the dropdown
                ddlCategories.DataSource = lstCategories;
                ddlCategories.DataBind();
            }

            // Get Posts

            // create query
            // Note this method bypasses all LightSwitch security
            var colPosts = from PST in db.Posts
                           select PST;

            // Get active posts
            colPosts = colPosts.Where(x => x.Active == true);

            // Get Category
            if (intCategoryID != -1)
            {
                colPosts = colPosts.Where(x => x.Category.Id == intCategoryID);
            }

            // Do paging
            colPosts =
                colPosts.OrderByDescending(x => x.PostUpdated).
                Skip(((intPage - 1) * 10)).Take(10);

            // We now have the results - bind then to the dropdown
            gvPosts.DataSource = colPosts;
            gvPosts.DataBind();

        }
        #endregion

        #region ddlCategories_SelectedIndexChanged
        protected void ddlCategories_SelectedIndexChanged(object sender, EventArgs e)
        {
            pnlMessage.Visible = false;
            GetPosts(false, CurrentPage);
        }
        #endregion

        #region btnLeft_Click
        protected void btnLeft_Click(object sender, EventArgs e)
        {
            pnlMessage.Visible = false;

            if (CurrentPage > 1)
            {
                CurrentPage--;
            }

            GetPosts(false, CurrentPage);
        }
        #endregion

        #region btnRight_Click
        protected void btnRight_Click(object sender, EventArgs e)
        {
            pnlMessage.Visible = false;

            if (gvPosts.Rows.Count > 0)
            {
                CurrentPage++;
            }

            GetPosts(false, CurrentPage);
        }
        #endregion

        #region gvPosts_RowCommand
        protected void gvPosts_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            LinkButton lnkButtton = (LinkButton)e.CommandSource;
            CurrentPostID = Convert.ToInt32(lnkButtton.CommandArgument);

            pnlMessage.Visible = true;
        }
        #endregion

        #region btnSubmit_Click
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                Transaction currentTrx = Transaction.Current;
                Transaction.Current = null;

                // Validate
                // We cannot use the normal LightSwitch validation
                // because the insert has to bypass nomal security
                if (!IsValidEmail(txtEmail.Text))
                {
                    lblError.Text = "Must use a valid Email";
                    return;
                }

                if (txtEmail.Text.Count() < 1)
                {
                    lblError.Text = "Must enter a message";
                    return;
                }

                if (txtEmail.Text.Count() > 255)
                {
                    lblError.Text = "Message must be les than 255 characters";
                    return;
                }

                // Connect to DotNetNuke Data
                string _IntrinsicData = Convert.ToString(ConfigurationManager.ConnectionStrings["DotNetNuke5Entities"]);
                DotNetNuke5Entities db = new DotNetNuke5Entities(_IntrinsicData);

                // Get the information for the user who made the Post
                var DNNUser = (from PST in db.Posts
                               from vw_Users in db.vw_UserRoles
                               where PST.PostedBy == vw_Users.Username
                               where PST.Id == CurrentPostID
                               select vw_Users).FirstOrDefault();

                // Create New Message
                DNNData.Message NewMessage = new DNNData.Message();

                NewMessage.NameFrom = txtName.Text;
                NewMessage.EmailFrom = txtEmail.Text;
                NewMessage.NameTo = DNNUser.DisplayName;
                NewMessage.EmailTo = DNNUser.Email;
                NewMessage.MessageText = txtMessage.Text;
                NewMessage.MessageCreated = DateTime.UtcNow;
                NewMessage.Message_Post = CurrentPostID;

                db.AddToMessages(NewMessage);
                db.SaveChanges();

                // Get the information for the Post
                var PostInfo = (from PST in db.Posts
                                where PST.Id == CurrentPostID
                                select PST).FirstOrDefault();

                // Send Email

                string strSubject = "Message From ThingsForSale";

                string strMessage = String.Format("{0}",
                    "This is a message From the Things For Sale program.")
                    + Environment.NewLine + Environment.NewLine;

                strMessage = strMessage + String.Format("Regarding the post '{0}'",
                    PostInfo.Description) + Environment.NewLine;

                strMessage = strMessage + String.Format("This message was sent by {0}",
                    txtName.Text) + Environment.NewLine + Environment.NewLine;

                strMessage = strMessage + String.Format("Message: {0}",
                    txtMessage.Text) + Environment.NewLine;

                // Create the MailHelper class created in the Server project.
                MailHelper mailHelper =
                    new MailHelper(
                        txtName.Text,
                        DNNUser.Email,
                        DNNUser.DisplayName,
                        strSubject,
                        strMessage);

                // Send Email
                mailHelper.SendMail();


                lblError.Text = "Success";

                // Clear fields
                txtMessage.Text = "";
            }
            catch (Exception ex)
            {
                lblError.Text = ex.Message;
            }
        }
        #endregion

        #region IsValidEmail
        public static bool IsValidEmail(string strIn)
        {
            // Return true if strIn is in valid e-mail format.
            // From: http://msdn.microsoft.com/en-us/library/01escwtf.aspx
            return Regex.IsMatch(strIn,
                   @"^(?("")("".+?""@)|(([0-9a-zA-Z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-zA-Z])@))" +
                   @"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,6}))$");
        }
        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


Written By
Software Developer (Senior) http://ADefWebserver.com
United States United States
Michael Washington is a Microsoft MVP. He is a ASP.NET and
C# programmer.
He is the founder of
AiHelpWebsite.com,
LightSwitchHelpWebsite.com, and
HoloLensHelpWebsite.com.

He has a son, Zachary and resides in Los Angeles with his wife Valerie.

He is the Author of:

Comments and Discussions