Posted on 7/21/2011

In order to use the browser scroll bars, edit the html page that is hosting the SL control. The Silverlight object tag is set to 100% height/width by default, and the div/body tags are set to 100% height as well.

Set the height and width of the Silverlight object to match your explicit sizes in your mainpage.xaml and you should see the browser scrollbars. Of course, only when the Silverlight page’s height and width exceeds the browsers.

 

<body>
    <form id="form1" runat="server" style="height:100%">
    <div id="silverlightControlHost">
        <object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="775" height="650">
          <param name="source" value="ClientBin/MyApplication.xap"/>
          <param name="onError" value="onSilverlightError" />
          <param name="background" value="white" />
          <param name="minRuntimeVersion" value="4.0.50826.0" />
          <param name="autoUpgrade" value="true" />
          <a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=4.0.50826.0" style="text-decoration:none">
               <img src="http://go.microsoft.com/fwlink/?LinkId=161376" alt="Get Microsoft Silverlight" style="border-style:none"/>
          </a>
        </object><iframe id="_sl_historyFrame" style="visibility:hidden;height:0px;width:0px;border:0px"></iframe></div>
    </form>
</body>
Posted on 7/20/2011

Assembly caching provides a mechanism to avoid packaging common DLLs into the application’s .xap file. This will reduce the application’s load time and make subsequent upgrades to the application easier.

Using assembly caching can be activated by selecting “Reduce XAP size by using application library caching” under the Silverlight project’s property pages Silverlight tab.

image

Note: Assembly caching is only available for browser-hosted applications and not for out-of-browser applications. That may change in future editions.

Posted on 7/12/2011

Creating the Membership Database.

Locate and run aspnet_reqsql.exe from a DOS prompt.  It should be located in  c:\Windows\Microsoft.NET\Framework\v4.030319.

The ASP.NET SQL Server Setup Wizard creates the Web site in the instance you specify in the wizard. The key is to modify the Web.config in your project to access the local aspnetdb.mdf.

<configuration>
    <connectionStrings>
      <add name="LocalSqlServer"
         connectionString="data source=(local); Integrated Security=SSPI;Initial Catalog=aspnetdb;"
         providerName="System.Data.SqlClient" />
    </connectionStrings>
</configuration>

 

or , if you are using SQL Server from the Internet you would use a connection string like below.

configuration>
    <connectionStrings>
    <add name="MembershipConnectionString"
         connectionString="Data Source=sql2k510.discountasp.net; InitialCatalog=SQL2005_77; Persist Security Info=True;User ID=SQL2005_77_user; Password=password"
         providerName="System.Data.SqlClient" />
    </connectionStrings>
</configuration>
Posted on 7/12/2011

Storing phone numbers in a table without the dashes and parenthesis sounds like a good idea, until you need to retrieve and format them correctly. As always, reading the documentation - and understanding it - can be time consuming and frustrating. Which it was! But this is what I finally came up with...

This will return (123) 456-7890

public partial class Report1 : Telerik.Reporting.Report
    {
        public Report1()
        {
             InitializeComponent();
        }

        public static string FormatPhoneNumber(string phoneNumber)
        {
            if (phoneNumber.Length == 10)
            {
                return "(" + phoneNumber.Substring(0, 3) + ") " + 
                      phoneNumber.Substring(3, 3) + "-" +
                        phoneNumber.Substring(6);
            }
            else
            {
                return "no phone number";
            }
        }
    }

Then in the "Value" property of the text box use the following: =FormatPhoneNumber(Fields.Phone)

Of course, you can modify this and take into consideration that the phone number may not have an area code. But who doesn't use area codes?

Posted on 7/6/2011

Here is a quick way to load the states into a RadComboBox.

Cut and paste the ComboBox.xml into an separate .xml file. Then place BindComboBox() into the code-behind of the .aspx file and call it from Page_Load().

The values in the list can be modified, but it should save time instead of creating it from scratch

private void BindStateCombo()
        {
            // load State combo box
            string path = "combobox.xml";
            StreamReader sr = method(path);
            StateCombo.LoadXml(sr.ReadToEnd());            
        }

private StreamReader method(string path)
        {            
            StreamReader sr = new StreamReader(Server.MapPath(path));
            return sr;
        }

protected void Page_Load(object sender, EventArgs e)
        {
            if (!Request.IsAuthenticated) { Response.Redirect("Login.aspx"); }

              // load combo boxe
              BindStateCombo();

         }

 

ComboBox.xml

<?xml version="1.0" encoding="utf-8" ?>
<Items>
<Item Text="States" IsSeparator="true" />
<Item  Value="AL" Text="Alabama" />
<Item  Value="AK" Text="Alaska" />
<Item  Value="AZ" Text="Arizona" />
<Item  Value="AR" Text="Arkansas"  />
<Item  Value="CA" Text="California"  />
<Item  Value="CO" Text="Colorado" />
<Item  Value="CT" Text="Connecticut" />
<Item  Value="DC" Text="District of Columbia" />
<Item  Value="DE" Text="Delaware" />
<Item  Value="FL" Text="Florida" />
<Item  Value="GA" Text="Georgia" />
<Item  Value="HI" Text="Hawaii" />
<Item  Value="ID" Text="Idaho" />
<Item  Value="IL" Text="Illinois" />
<Item  Value="IN" Text="Indiana" />
<Item  Value="IA" Text="Iowa" />
<Item  Value="KS" Text="Kansas" />
<Item  Value="KY" Text="Kentucky" />
<Item  Value="LA" Text="Louisiana" />
<Item  Value="ME" Text="Maine"  />
<Item  Value="MD" Text="Maryland" />
<Item  Value="MA" Text="Massachusetts" />
<Item  Value="MI" Text="Michigan" />
<Item  Value="MN" Text="Minnesota" />
<Item  Value="MS" Text="Mississippi" />
<Item  Value="MO" Text="Missouri" />
<Item  Value="MT" Text="Montana" />
<Item  Value="NE" Text="Nebraska" />
<Item  Value="NV" Text="Nevada" />
<Item  Value="NH" Text="New Hampshire" />
<Item  Value="NJ" Text="New Jersey" />
<Item  Value="NM" Text="New Mexico" />
<Item  Value="NY" Text="New York" />
<Item  Value="NC" Text="North Carolina" />
<Item  Value="ND" Text="North Dakota" />
<Item  Value="OH" Text="Ohio" />
<Item  Value="OK" Text="Oklahoma" />
<Item  Value="OR" Text="Oregon" />
<Item  Value="PA" Text="Pennsylvania" />
<Item  Value="RI" Text="Rhode Island"  />
<Item  Value="SC" Text="South Carolina"  />
<Item  Value="SD" Text="South Dakota" />
<Item  Value="TN" Text="Tennessee" />
<Item  Value="TX" Text="Texas" />
<Item  Value="UT" Text="Utah" />
<Item  Value="VT" Text="Vermont" />
<Item  Value="VA" Text="Virginia" Selected="True" />
<Item  Value="WA" Text="Washington" />
<Item  Value="WV" Text="West Virginia" />
<Item  Value="WI" Text="Wisconsin" />
<Item  Value="WY" Text="Wyoming" />
</Items>


 

Download:  combobox.xml (2.09 kb)

Posted on 7/5/2011

Sending an email from a web page using the Silverlight-enabled WCF Service.

CREATE THE PROJECT

First, using Visual Studio 2010, create a new Silverlight Application project and use ContactEmail as the project’s name.

image

When this dialog appears, make sure the checkbox for “Host the Silverlight application in a new Web Site” is checked.

image

 

DESIGN THE EMAIL FORM

Visual Studio will open the MainPage.xaml file in the IDE.

image

Change the MainPage.xaml file to match the one below.

<UserControl x:Class="ContactEmail.MainPage"
     xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="400" d:DesignWidth="600">
    <Grid x:Name="LayoutRoot" Margin="10">
        <StackPanel x:Name="EmailPanel" Width="293" Canvas.Left="529" Canvas.Top="99" Margin="134,0,135,0">
            <TextBlock x:Name="name" HorizontalAlignment="Left" Text="Name:" />
            <TextBox Name="txtName"   />
            <TextBlock x:Name="phone" HorizontalAlignment="Left" Text="Phone:" />
            <TextBox Name="txtPhone"   />
            <TextBlock x:Name="txtemail" HorizontalAlignment="Left" Text="Email:" />
            <TextBox Name=”txtEmail” TextWrapping="Wrap"/>
            <TextBlock x:Name="txtComment" HorizontalAlignment="Left" Text="Comments:" TextWrapping="Wrap" />
            <RichTextBox  Name="txtComments"  Height="95"/>                      
            <Button Height="33" x:Name="btnSumit"   Content="Submit" Width="80" />
        </StackPanel>
    </Grid>
</UserControl>

Click the Design Tab  - bottom of page – to view the MainPage.xaml. The page should look like the one below. Save the MainPage.xaml.

image 

 

CREATE THE WCF SERVICE

Add a new item to the ContactEmail.Web project.

image

In the Installed Templates – left panel – select Silverlight. From the center panel, select Silverlight-enabled WCF Service. Use EmailService.svc for the name of the service.

image

Visual Studio will add the necessary files to the References folder in the ContactEmail.Web project. Visual Studio will also make changes to the Web.config file in the same project.

Open the Web.Config file and notice the following items that have been added.

  <system.serviceModel>
        <behaviors>
            <serviceBehaviors>
                <behavior name="">
                    <serviceMetadata httpGetEnabled="true" />
                    <serviceDebug includeExceptionDetailInFaults="false" />
                </behavior>
            </serviceBehaviors>
        </behaviors>
        <bindings>
            <customBinding>
                <binding name="ContactEmail.Web.EmailService.customBinding0">
                    <binaryMessageEncoding />
                    <httpTransport />
                </binding>
            </customBinding>
        </bindings>
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
            multipleSiteBindingsEnabled="true" />
        <services>
            <service name="ContactEmail.Web.EmailService">
                <endpoint address="" binding="customBinding" bindingConfiguration="ContactEmail.Web.EmailService.customBinding0"
                    contract="ContactEmail.Web.EmailService" />
                <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
            </service>
        </services>
    </system.serviceModel>

Build the Solution first. Then right click on the EmailService.svc file in the EmailContact.web project. Select "View in Browser" and the following screen should be displayed.

image

 

CREATE A CLIENT FOR THE EMAIL SERVICE

Open the EmailService.svc.cs file and add the following using statement.

Using System.Net.Mail;

Then add the following code:

 [OperationContract]
        public bool SendEmail(String ClientName, String ClientPhone, String ClientEmail, String Comment)
        {
            try
            {
                //////////////////////////////////
                // BUILD EMAIL MESSAGE STRING //
                ///////////////////////////////// 
                string Filler = "";                
                string MsgBody = "";
 
                MsgBody += "Name:" + Filler.PadLeft(5) + ClientName.Trim() + Environment.NewLine + Environment.NewLine;
                MsgBody += "Email:" + Filler.PadLeft(4) + ClientEmail.ToString() + Environment.NewLine + Environment.NewLine;
                MsgBody += "Phone:" + Filler.PadLeft(4) + ClientPhone + Environment.NewLine + Environment.NewLine;
                MsgBody += "Comments:" + Environment.NewLine + Environment.NewLine;
                MsgBody += Comment + Environment.NewLine;

                string txtEmailTo = "webmaster@yourewbsite.com";
                string userName = "UserName";   // WebMaster's user name
                string passWord = "passWord";   // WebMaster's password

                MailMessage msg = new MailMessage();
                msg.From = new MailAddress(ClientEmail);
                msg.To.Add(new MailAddress(txtEmailTo));
                msg.Subject = "*** Contact from Website ***";
                msg.Body = MsgBody;

                SmtpClient smtp = new SmtpClient("smtp.yourwebsite.com");
                smtp.UseDefaultCredentials = false;
                System.Net.NetworkCredential theCredential = new System.Net.NetworkCredential(userName, passWord);
                smtp.Credentials = theCredential;
                smtp.Send(msg);
            }

            catch (Exception ex)
            {
                return false;
            }

            return true;
        }

Remember to change the values in the txtEmailTo, userName, passWord variables. Also, change the line SmtpClient smtp = new SmtpClient(“smtp.yourwebsite.com”) to your web site.

Save the EmailService.svc.cs file and then Build the project. Now right click in References in the ContactEmail Silverlight project and choose Add Service Reference.

image

Click the Discover button and select the EmailService.svc. Use the Namespace Services and click the OK button.

image

Visual Studio will add the necessary files to the References folder in the ContactEmail Silverlight project.

 

CALL THE EMAIL SERVICE FROM MainPage.Xaml

Open the MainPage.xaml file and click the Design tab at the bottom of the page.  Right click on the Submit button and select properties from the context menu.

Click on Events – at the top of the properties window – and double click on the Click event.

image

Add the following using statement in the MainPage.xaml.cs file

using ContactEmail.EmailServiceReference;

Then add the following code:

private void btnSumit_Click(object sender, RoutedEventArgs e)
        {
            txtComments.SelectAll();

            Services.EmailServiceClient client = new Services.EmailServiceClient();
            client.SendEmailAsync(txtName.Text, txtPhone.Text, txtEmail.Text, txtComments.Selection.Text);
            client.SendEmailCompleted += new EventHandler<Services.SendEmailCompletedEventArgs>(client_SendEmailCompleted);                                
        }

        
        void client_SendEmailCompleted(object sender, Services.SendEmailCompletedEventArgs e)
        {
            if (e.Result == true)
            {
                new messageWindow("Title Information", "Email Successfully Sent.").Show();
            }
            else
            {
                new messageWindow("Title Information", "Email Not Sent.").Show();
            }
        }

 

CREATE THE CHILD WINDOW

From the ComtactEmail Silverlight project select Add, New Item.

image

In the Installed Templates – left panel – select Silverlight. From the center panel, select Silverlight Child Window. Use messageWindow.xaml for the name. Click the Add button to continue.

image

Add the following code to the messageWindow.xaml file.

<controls:ChildWindow x:Class="ContactEmail.messageWindow"
           xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
           xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
           xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
           Width="400" Height="300" 
           Title="messageWindow">
    <Grid x:Name="LayoutRoot" Margin="2">
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>      
        <TextBox Height="21" HorizontalAlignment="Center" Margin="12,108,20,0" Name="Caption" VerticalAlignment="Top" Width="325" BorderThickness="0" Background="Transparent" />
        <Button x:Name="CancelButton" Content="Cancel" Click="CancelButton_Click" Width="75" Height="23" HorizontalAlignment="Right" Margin="0,12,0,0" Grid.Row="1" />
        <Button x:Name="OKButton" Content="OK" Click="OKButton_Click" Width="75" Height="23" HorizontalAlignment="Right" Margin="0,12,79,0" Grid.Row="1" />
    </Grid>
</controls:ChildWindow>

Open the MessageWindow.xaml.cs file and add the following code:

 public MessageWindow(string title, string caption)
        {
            InitializeComponent();
            this.Title = title;
            this.Caption.Text = caption;

            // OkButton properties
            this.OKButton.Width = 75;
            this.OKButton.Height = 30;
            this.OKButton.HorizontalAlignment = HorizontalAlignment.Center;

            // Use OK button only
            this.CancelButton.Visibility = Visibility.Collapsed;
        }

Build the solution and hit F5 to run.

 

PUBLISHING

Before publishing to a web site, open the ServiceReferences.ClientConfig file – in the ContactEmail Silverlight project – and change the endpoint address to your web site’s address.

Change From:

<client>
      <endpoint address="http://localhost:49472/EmailService.svc" binding="customBinding"
       bindingConfiguration="CustomBinding_EmailService" contract="Services.EmailService"
       name="CustomBinding_EmailService" />
</client>

Change To:

<client>
      <endpoint address="http://www.YourWebSite.com/EmailService.svc" binding="customBinding"
       bindingConfiguration="CustomBinding_EmailService" contract="Services.EmailService"
       name="CustomBinding_EmailService" />
</client>

 

Build, Publish, and Run!!!