ASP.NET Flash Remoting

9. May 2008

If you are looking for information on a free ASP.NET Flash Remoting Solution you have come to the right place.

The Midnight Coders have a product called WebORB® that will meet all of your remoting , real-time messaging (RTMP) and Flex Data Services (AMF0 and AMF3, Data Management, Flex Messaging (pub/sub, push)) needs.

I have used this product's Flash Remotings features extensively on one of my clients sites and I highly recommend it to anyone looking for a Flash Remoting Solution even if they are not doing development in ASP.NET.  WebORB® also comes in Java, Ruby On Rails and PHP versions.

 

Installation

Installing WebORB® is extremely easy. Just copy its assemblies to your bin folder, copy a few files and folders to your web applications root directory and give a few of them write permissions. The final step is updating your web.config file with its HTTP Handlers.

If you install WebORB® on your server, you have the option of automatically deploying to to a virtual directory.  There seems to be 1 downfall, it appears that it can only be automatically installed to virtual directorys of your default web site. If you are on a server that is hosting multiple web sites, you should just do the manual install.

Using WebORB

One of WebORB's best abilities is that it will parse your Web Applications Assemblies and generate Action Script code for use in your flash movie or JavaScript for your AJAX functions. It can generate code in a variety of formats and styles: Flex Remoting / AS3, Flash Remoting / AS2, ARP Framework / AS2 or AS3, Cairngorm Framework, FlashComm / FMS2, AJAX Client and PureMVC.  It will generate code for anything that your functions  in your assembly returns. Objects, DataSets, strings  etc.

 

WebOrb

ASP.NET, Flash Remoting ,

ASP.NET Persistent Checkbox State in a GridView With Data Paging.

9. March 2008

One major downsides of using a checkbox in a GridView control to handle mass updates and deletes is that the state of the checkboxs will be lost when going to another page.

To work around this were going to have to write some code to save and load the state of the checkboxes.

You can download the files below.

 

 

The 1st thing we want to do is set up our GridView and a DataSource.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Grid View Check Box Example</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:GridView ID="Gridview1" OnDataBound="Gridview1_OnDataBound" runat="server" Width="100%" OnPageIndexChanging="Gridview1_PageIndexChanging" AllowPaging="True" AutoGenerateColumns="False" DataKeyNames="recordID" DataSourceID="AccessDataSource1">
                <Columns>
                    <asp:BoundField DataField="recordID" HeaderText="recordID" InsertVisible="False" ReadOnly="True" SortExpression="recordID" />
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:CheckBox ID="chkPerson" runat="server" />
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:BoundField DataField="firstName" HeaderText="firstName" SortExpression="firstName" />
                    <asp:BoundField DataField="lastName" HeaderText="lastName" SortExpression="lastName" />
                </Columns>
                <HeaderStyle HorizontalAlign="Left" />
            </asp:GridView>
            <asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/App_Data/gridviewCheckbox.mdb" SelectCommand="SELECT [recordID], [firstName], [lastName] FROM [record]"></asp:AccessDataSource>
            <br />
            <br />
            <asp:Button ID="bttnMassUpdate" runat="server" OnClick="bttnMassUpdate_Click" Text="Mass Update" /><br />
        </div>
    </form>
</body>
</html>

 

For those of you who are unfamiliar with grid views, The DataKeyNames property is set to the PrimaryKey column of your datasource and in this example its the recordID column.

The OnDataBound and OnPageIndexChanging properties are set to the name of the functions I created that I want called when these events are fired.

 

I used a AccessDataSource for this example because It would allow anyone to download and run this example without the need to install a SQL server.

The data in this Access Database was generated with http://www.generatedata.com/.

 

Now since we have our aspx page setup we need to choose a data structure that can hold the primary key for each select row. I chose a List for this task.

protected void Page_Load(object sender, EventArgs e)
   {
       if (!Page.IsPostBack)
       {
           List<int> list = new List<int>();
           Session["gridSave"] = list;
       }
   }

 

Now when we go to another page we need a way to store the primary key of each row that has been selected and to also remove any key that has been unselected.

private void saveValues()
{
    List<int> list = (List<int>)Session["gridSave"];
 
    foreach (GridViewRow row in Gridview1.Rows)
    {
        int id = (int)Gridview1.DataKeys[row.RowIndex].Value;
        bool chk = ((CheckBox)row.FindControl("chkPerson")).Checked;
 
        if (chk)
        {
            //see if we have a id added already
            if (list.Contains(id) == false)
            {
                list.Add(id);
            }
        }
        else
        {
            if (list.Contains(id))
            {
                list.Remove(id);
            }
        }
    }
 
    Session["gridSave"] = list;
}

 

Everytime we go to a new page we will also have to see if any checkboxes need to be checked

private void loadValues()
{
    List<int> list = (List<int>)Session["gridSave"];
 
    foreach (GridViewRow row in Gridview1.Rows)
    {
        int id = (int)Gridview1.DataKeys[row.RowIndex].Value;
        if (list.Contains(id))
        {
            CheckBox chkPerson = (CheckBox)row.FindControl("chkPerson");
            chkPerson.Checked = true;
        }
    }
}

 

The above functions are called from the GridViews PageIndexChanging and OnDataBound Events.

protected void Gridview1_PageIndexChanging(object sender, GridViewPageEventArgs e)
   {
       saveValues();
       Gridview1.PageIndex = e.NewPageIndex;
   }
 
   protected void Gridview1_OnDataBound(object sender, EventArgs e)
   {
       loadValues();
   }

ASP.NET, Generics, GridView , ,

Dictionary VS Hash Table

8. January 2008

We all know how useful a hash table can be, but if you are using the .NET Framework 2.0 you should be using a Dictionary instead. A dictionary and a hash table in .NET 2.0 are very similar but with 1 main difference, a dictionary is faster because it does not need to box and unbox the data as a hash table would.

 

I made a simple program to see just how much faster a dictionary is than a hash table. I ran the test 20 times for each data set and the results were close each time. A GUID as the key and a boolean as the data.

This is the results of the last test for each dataset.

Time in Milliseconds for 100,000 keys Dictionary Hash Table
Inserting 34.41264 76.0970
Reading 18.9005 31.1648
Deleting 21.2474 30.4655

 

Time in Seconds for 10,000,000 keys Dictionary Hash Table
Inserting 6.5704143 26.9096540
Reading 3.1362891 5.6670613
Deleting 3.4623316 4.8830676

 

My comp specs

Vista Business 32bit, AMD 64 x2 5000+, 2GB ram

 

and now for the code I used. You can also download the project.  

 

 

 

 

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
 
namespace hashVsDictionary
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            #region init
 
            int num = 10000000;
            Guid[] guids = new Guid[num];
 
            Stopwatch s = new Stopwatch();
            s.Start();
 
            for (int i = 0; i < num; i++)
            {
                guids[i] = Guid.NewGuid();
            }
 
            s.Stop();
            Console.WriteLine("Data Creation = " + s.Elapsed);
            s.Reset();
 
            #endregion
 
            #region hash
 
            Hashtable hash = new Hashtable();
            s.Start();
            for (int i = 0; i < num; i++)
            {
                hash.Add(guids[i], true);
            }
            s.Stop();
            Console.WriteLine("Insert Hash = " + s.Elapsed);
            s.Reset();
 
            s.Start();
            for (int i = 0; i < num; i++)
            {
                bool b = Convert.ToBoolean(hash[guids[i]]);
            }
            Console.WriteLine("Extracting Hash = " + s.Elapsed);
            s.Reset();
 
            s.Start();
            for (int i = 0; i < num; i++)
            {
                hash.Remove(guids[i]);
            }
 
            Console.WriteLine("Removing Hash = " + s.Elapsed);
            s.Reset();
 
            #endregion
 
            #region dict
 
            Dictionary<Guid, bool> dict = new Dictionary<Guid, bool>();
            s.Start();
            for (int i = 0; i < num; i++)
            {
                dict.Add(guids[i], true);
            }
            s.Stop();
            Console.WriteLine("Insert Dictionary = " + s.Elapsed);
            s.Reset();
 
            s.Start();
            for (int i = 0; i < num; i++)
            {
                bool b = dict[guids[i]];
            }
            Console.WriteLine("Extracting Dict = " + s.Elapsed);
            s.Reset();
 
            s.Start();
            for (int i = 0; i < num; i++)
            {
                dict.Remove(guids[i]);
            }
            Console.WriteLine("Removing Dict = " + s.Elapsed);
            s.Reset();
 
            #endregion
 
            Console.ReadLine();
        }
    }
}

ASP.NET, Generics ,

ASP.NET Google Map Control

4. January 2008

If you are looking for a ASP.NET Google Map Control, your search is now over.

Jacob Reimers from http://www.reimers.dk/ offers a great asp.net control in 2 flavors.

The free version which lets you easily display a map with markers and/or lines and a licensed version which gives you the full power of Google Maps.

 

I have used this control on a few of Web Solutions clients sites. Two of the better implementations are Low Index and Seville Homes.

 

*The deals listed below are entirely fictional. They were created for presentation purposes only.

 

lowIndex

seville

 

 

 

To use this great control, head over to http://www.reimers.dk/, download the free control, unzip it to your Bin folder in your web app and add a reference to it.

 

This is a sample asp.net page

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<%@ Register Assembly="GoogleMap" Namespace="Reimers.Map" TagPrefix="Reimers" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title>Google Map test</title>

</head>

<body>

    <form id="form1" runat="server">

        <div>

            <reimers:googlemap id="GMap" runat="server" width="349" height="354" onmarkerclick="GMap_MarkerClick"  />

        </div>

    </form>

</body>

</html>

 

and the code behind

 

using System;

using System.Web.UI;

using Reimers.Map;

 

public partial class _Default : Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

        GMap.GoogleKey = "Your API Code here";

        GMap.MapType = MapType.Map;

        GMap.TypeControl = MapTypeControl.None;

        GMap.MapControl = ControlType.Small;

 

        GoogleMarker testMarker = new GoogleMarker("newMarker", new GoogleLatLng(43.611611, -88.952931));

        testMarker.MarkerText = "Test Marker";

        GMap.Markers.Add(testMarker);

 

        GMap.Latitude = testMarker.Latitude;

        GMap.Longitude = testMarker.Longitude;

        GMap.Zoom = 10;

    }

 

    protected void GMap_MarkerClick(GoogleMap GMap, GoogleMarker Marker, ref String MapCommand)

    {

        MapCommand = Marker.OpenInfoWindowHTML(GMap, Marker.MarkerText);

    }

}

ASP.NET, Google Maps ,