Using a Rich Text Editor with Dynamic Data

In the following post I will outline how to use the FCKeditor in a Dynamic Data project.  Using the FCKeditor is straight forward especially if you use the .NET FCKeditor control.

Steps:

  1. Read the liscensing and terms of use for the FCKeditor
  2. Download the FCKeditor .Net control, dll, and add a reference
  3. Download the FCKeditor fckeditor dir and all sub dirs/files...place these in the root dir of your Dyanamic Data project
  4. In your dynamic data project, in the Field Templates directory, Add new item -> Dynamic Data Field
  5. Delete the FCkEditor.ascx as we will only be using the control for editing
  6. In FCKeditor_Edit.ascx, remove all of the controls, then add

    <%@ Register Assembly="FredCK.FCKeditorV2" Namespace="FredCK.FCKeditorV2" TagPrefix="FCKeditorV2" %>
    <FCKeditorV2:FCKeditor ID="TextBox1" Value='<%# FieldValueEditString %>' runat="server"></FCKeditorV2:FCKeditor>
  7. In the code behind, you want to end up with the below (you will want to modify to add your own trickery...)

    protected void Page_Load(object sender, EventArgs e) {

    }

    protected override void ExtractValues(IOrderedDictionary dictionary) {

         dictionary[Column.Name] = ConvertEditedValue(TextBox1.Value);

    }

    public override Control DataControl {

         get { 

              return TextBox1;

         }

    }

  8. Now, create a partial class in the same namespace as your dbml (LINQ) classes...I will be using the FCKeditor for text areas for my LINQ class "AttempPending"

    *Important thing to note is that the UIHint needs to be the name of your user control (minus the _Edit)...for this example, my user control is named "FCKeditor_Edit.ascx"

    [MetadataType(typeof(AttemptPending_Meta))]

    partial class AttemptPending {

         public class AttemptPending_Meta {           

              [UIHint(
    "FCKeditor")]
             
    public object DescriptionLong { get; set; }
         }
    }

    The net result of the above is that when you use a DynamicData field that binds to the Property DescriptionLong, it will render with the FCKeditor

  9. In the page that you would like the FCKeditor, you would add the following (In my case, this is within a DeatilsView)

    <asp:DynamicField DataField="DescriptionLong" HeaderText="Description Long" />  

    And that should result in the FCKeditor appear on your page...

ASP.NET error a control with id could not be located or a different control is assigned to the same ID after postback

Recently I came across an error in an asp.net page that took a fair amount of troubleshooting to resolve.  I had an asp.net page that was using a master page and within the aspx page, I had a grid view.  On the aspx page, I also had a drop down list that posted back to the server when the selected index changed.  On post back, I received the below error: 

An error has occurred because a control with id 'ctl00$ContentPlaceHolder1$gvFiles$ctl05$ctl00' could not be located or a different control is assigned to the same ID after postback. If the ID is not assigned, explicitly set the ID property of controls that raise postback events to avoid this error.

System.Web.UI.Page.ProcessPostData

After some digging, I determined the error was due to multiple asp:ButtonField controls being placed in the GridView.  Since you can not assign an ID to these controls the above error was being raised.  I did not find out exactly why, but at a higher level, the issue was due to my master page having view state disabled.  Once I changed this so my master page had ViewStateEnabled="true" (I had previously set it to false) the error went away.  If you have viewstate turned off at the page or gridview level you may also encounter this issue.

Styling AJAX Control Toolkit Tab Control

If you have a need to apply custom styles to the AJAX Control Toolkit tab control you should take a look at Matt Berseth's post that includes examples.  You can also create your own styles....to do this, you can take a look at Matt's style sheets and then create your own from his. 

 In order to override the default styles, you will need to define the styles and then when you add the tab control, use the CssClass property to set the css style to use:

<ajaxToolkit:TabContainer runat="server" ID="Tabs" ActiveTabIndex="0" CssClass="ajax__tab_mycustom-theme">

Integrate Disqus into your site using JavaScript

Recently I added Disqus, a hosted comment system, to a site and wanted to point out a couple of things that were not too clear from the documentation on the Disqus site.  It may have been updated, but as of this post, I was not able to find too much info on the JavaScript implementaion of Disqus. 

Basically the JavaScript implementation will work with any web site technology, for example php, ASP.NET, etc.  In order to get it to work, you first need to sign up for an account.  Once you do this, they will provide you with the "code" that you need to add to your site.  Once you add this code, all of the necessary HTML and comment data will appear on your page. 

When you create an account, you provide your site URL. It is important to understand that if you are developing or testing your site on a server other than the one registered with your domain name, the Disqus comment section will not appear.  So if you are setting things up on your local machine and testing, you will not see the full comment section.  Once you push your code to production, you will then see the full Disqus comment section

That is it...it is very easy to set up and use...

Server.Map path returns error System.InvalidOperationException: Failed to map the path in Cassini

I encountered an issue today while trying to use the Server.MapPath method in an asp.net web page.  When trying to use Server.MapPath with a virtual directory in IIS (outside the web site) I was receiving an System.InvalidOperationException: Failed to map the path error.  Turns out, since I was running the web site as a file system project, and using cassini, this error was being generated.  By moving the project to IIS and running the site under IIS and not cassini, the error went away.

Using Dependency Injection with an ObjectDataSource

In this post I will outline how to use dependency injection with an ObjectDataSource.  There are many benefits to using dependency injection and programming to interfaces including maintainability, extensibility, and testability.  For the example that follows the use case is to provide a pagable, sortable list of data, using a gridview, and performing the paging and sorting within the database for performance reasons.  Based on these requirements, an object data source is needed as it provides the mechanism to support custom paging and sorting for a grid view.

Dependency Injection For this example I have choosen StructureMap.  One of the nice features of StructureMap is that is allows you to map your Interfaces to concrete types using code.  The below code creates a Registry and leverages some new features in StrucutreMap version 2.5

public class ServiceRegistry : Registry
{
  protected override void configure()
 {
    ForRequestedType<IPersonService>().TheDefaultIsConcreteType<PersonRepository>();
  }
}

This code instructs StructureMap to return PersonRepository when someone requests an IPersonService. There is a lot more you can do with StructureMap, for a complete overview of StructureMap, please check out their homepage.

Next, it is time to set up the ObjectDataSource.  An ObjectDataSource does not let you specify an Interface directly, so you need to override the default behavior.  First, define the types:

<asp:ObjectDataSource ID="ObjectDataSourcePerson" runat="server" SelectMethod="GetRecordList"        DataObjectTypeName="Services" TypeName=Services.IPersonService"        OnObjectCreating="ObjectDataSourcePerson_ObjectCreating" >   
</asp:ObjectDataSource> 

The important things to note are the types (DataObjectTypeName is Namespace.Class, TypeName is Namespace.Class.Type) and the OnObjectCreating, this is where we will override the default behavior.  The code behind for this looks like: 

protected void ObjectDataSourcePerson_ObjectCreating(object sender, ObjectDataSourceEventArgs e)    {        
 
IPersonService personService =
StructureMap.ObjectFactory.GetInstance<IPersonService>();        
 
e.ObjectInstance = personService;
 
}

Now, when the ObjectDataSource is created, the concrete type will be used.  The power here is that we did not have to specify the concrete type in the code or DataSource definition, so in the future we can change the implementation and not need to change the UI code.

From here, you can implement the paging and sorting in the custom type and wire it up to the ODS and GridView.

 

Software Development Podcasts

Below is a list of software development podcasts, IT Podcasts, and general computer related podcasts. Please add commnets on podcasts you enjoy...

.NET Rocks - This is a really good (quality and content) podcast that focuses on .NET development.  While the main focus is .NET there is a fair amount of content that applies to general computing and development such as design patterns, agilie practices, and more.

Software Engineerig Radio - This podcast provides an excellent variaty of topics that span technologies, languages, platforms, methodologies, etc. The shows can be rather technical and geared more toward knowledgable developers, but still offer a wealth of knowledge for beginners through experts.

Hansel Minutes - This is a weekly podcast that is primarily focused on Microsoft technologies but also includes other technologies and topics from time to time (for example digital photography).  While Scott is a Microsoft employee, the cast is rather open to non MS technologies and techniques such as TDD, ALT.NET, etc.

The Java Posse - This is an excellent podcast focused on the Java language, platform, and related technologies.  It provides both technical information as well as news and events within the Java community.

Buzz Out Loud - This is a daily podcast that is a summary of news and events relating to technology.  The personalities of the hosts really help to seperate this podcast from the usually news recap type shows...certainly worth checking out...

Polymorphic Podcast - A good podcast focusing on development and .NET.  The podcasts sometimes include screencasts and overall provides a lot of good content.

SSWUG - A database centric podcast dealing mainly with SQL Server but does include topics on other RBDMS as general database best practices.

Slashdot Review -  While at times biased, this can be a quick and informative cast.

Google Developer Podcast - Hopefully this will be a good resource to learn more about the technolgy offerings available through google.

Windows Weekly Podcast - Interesting take on the windows world.

ALT.NET Podcasts - A really good podcast that covers topics that all developrs should familiarize themselves with.

Deep Fried Bytes Podcast - Good podcast focusing on mainly Microsoft development

Thirsty Developer - Another good developer podcast focusing mainly on .NET

herdingcode.com - Good podcast relating to development

WebDevRadio - Covers web development

eCorner - Stanford entreprenure podcast 

Stackoverflow - development podcast 

ThoughtWorks - Business and technology topics

Agile Toolkit - topics relating to Agile development 

Udi Dahan - SOA podcast 

Endpoint.tv - New show hosted by Ron Jacobs on REST, SOA, web services, etc. 

http://pixel8.infragistics.com/default.aspx - UI, UX, RIA info

Finacial Physician

Railscast - webcasts on Ruby on Rails

Rails Podcast - podcasts on RoR

FLOSS - Weekly podcast on free and open source software

GiaOM - weekly show about technology and business 

Pragmatic Programmers - Interesting tech podcasts

Rails Envy - Regular podcast on Ruby on Rails

The Start Up Success Podcast - Name says it all...

This Week in Start Ups - Podcast on business and technology

Mobil Orchard - iPhone development topics

Stuff You Should Know - Variety of interesting topics from science to general knowledge....quite entertaining....

 

 

Visual Studio 2008 no syntax highlighting for XML and config files

I had been running a beta of Visual Studio 2008 and when I uninstalled the beta and installed the RTM version, I found that I had no syntax highlighting or intellisense when editing config files (web.config, app.config, etc.)  When I tried to change the properties in the IDE, I received An error occurred loading this property page from the tools -> options menu.

 To fix the issue I ran the below command from the Visual Studio 2008 command prompt:

 devenv /setup

 

ASP.Net Membership Password Administration

If you need to update user's passwords that are stored in the aspnet_Membership, the below code should help you accomplish this.

If you are using the default SqlMemberShip provider for authentication, you may have a need to manually reset a user's password.  In order to accomplish this, you can hash the password with a salt and then store the password and salt in the aspnet_Membership table.

string salt = GenerateSalt();
string password = EncodePassword("mypassword", 1, salt);

public string EncodePassword(string pass, int passwordFormat, string salt)

{

if (passwordFormat == 0) // MembershipPasswordFormat.Clear

return pass;

byte[] bIn = Encoding.Unicode.GetBytes(pass);

byte[] bSalt = Convert.FromBase64String(salt);

byte[] bAll = new byte[bSalt.Length + bIn.Length];

byte[] bRet = null;

System.Buffer.BlockCopy(bSalt, 0, bAll, 0, bSalt.Length);System.

Buffer.BlockCopy(bIn, 0, bAll, bSalt.Length, bIn.Length);

if (passwordFormat == 1)

{ // MembershipPasswordFormat.Hashed

HashAlgorithm s = HashAlgorithm.Create(Membership.HashAlgorithmType);

bRet = s.ComputeHash(bAll);

}

else

{

//bRet = EncryptPassword(bAll);

}

return Convert.ToBase64String(bRet);

}

private string GenerateSalt()

{

byte[] buf = new byte[SALT_SIZE_IN_BYTES];

(new RNGCryptoServiceProvider()).GetBytes(buf);

return Convert.ToBase64String(buf);

}

 

Once you have the salt and password, you can store them in the database. 

 

UPDATE [aspnet_Membership] SET Password = [use the password hash from above],

PasswordSalt = [use salt from above] WHERE [UserId] = CAST('123456789...' as uniqueidentifier)

 

Finally you can now authenticate the user with a password of "mypassword".

 

Error trying to install or run vsi file

If you are trying to run a .vsi file and have both VS 2005 and VS 2008 you may encounter an error that indicated the file microsoft.wizardframework could not be loaded:

could not load file or assembly 'microsoft.wizardframework

Below are the steps I followed to correct:

  1. Check the GAC (C:\WINDOWS\assembly) for the assembly Microsoft.WizardFramework.dll
  2. If it does not exist, check that it exists in C:\Program Files\Micro
    soft Visual Studio 9.0\Common7\IDE\
  3. If it does, then open a VS command prompt and add the dll to the GAC
    gacutil /i "C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\Microsoft.WizardFramework.dll"
  4. Check for the dll in C:\WINDOWS\assembly
  5. You should now be able to run the VSI