ASP.NET Error - ProfileCommon could not be found

While converting a Web site project to a Web App project, you may encounter an error: The type or namespace name 'ProfileCommon' could not be found (are you missing a using directive or an assembly reference?)

It so happens that if you are using the ASP.NET Web site project template, you have Profiles autogenerated for you, out of the box. However Web App projects don't support the auto generation of the ProfileCommon.

What is Profile Common?

ASP.NET uses the ProfileBase class to create the class used for the user profile. When an application that has the user profile enabled is started, ASP.NET creates a new class of type ProfileCommon, which inherits from the ProfileBase class.

A simple fix is to install the Web Profile Builder which is a build task. When you configure this build task in your project file, it creates a wrapper class for the Web Profile and keep it updated every time your project builds. You will then have strongly typed access to the Profile object in Web Application Projects.

The installation is pretty simple : Run the installer and add an Import statement to your project file.

<Import Project="$(MSBuildExtensionsPath)\WebProfileBuilder\WebProfileBuilder.targets" />

That’s it. You are good to go!

Note: For VS 2010 projects, click the ‘Show All Files’ icon in the Solution explorer, right click the webprofile file and include it in the project.

Partial Views in ASP.NET MVC 3

A Partial View is a reusable fragment of content and code that can be embedded in another view and improves the usability of a site, while reducing duplicate code.

Note: A Partial View in ASP.NET MVC is similar to a user control in ASP.NET Web Forms and is rendered using the ViewUserControl class, which derives from the ASP.NET UserControl
class. If you are using the Razor view engine, the partial view is a .cshtml template and for the ASPX view engine, an .ascx template.

A simple use of a partial view can be an online e-commerce site where a view can contain various sections like - items in the shopping cart, suggested items, wish list, items others brought etc. To reduce the complexity of such views, you can put each section into a separate partial view and then use all these partial views into a single view. Another simple use of the partial views is to use it to display social bookmarking icons across multiple pages. Partial Views are also used in many AJAX scenarios.

Difference between View and Partial view

A View when rendered produces a full HTML document, whereas a partial view when rendered produces a fragment of HTML. A partial view does not specify the layout.

Creating a Partial View

In order to create a partial view, right click the /Views/Shared folder > Add > View. Type a View Name, type or choose a class from the Model class and check the Create as a partial view option, as shown below

aspnet-mvc-partial-view

Click Add and a partial view file at Views/Shared/RegistrationSummary.cshtml gets created.

Note: It’s not mandatory to create a partial view in the Shared folder, but looking at it’s usage of reusability, the Shared folder is a good place to store it.

Inserting Partial Views

Partial Views can be inserted by calling the Partial or RenderPartial helper method. The difference between the two is that the Partial helper renders a partial view into a string whereas RenderPartial method writes directly to the response stream instead of returning a string.

mvc-render-partial
Return Partial Views from Action method

Here’s how to use a partial view to return from an action method

image

You can also use jQuery to make an AJAX request and load a Partial View into a View. Suppose there’s a paragraph element called ‘para’

$(‘#para’).load(‘/account/registrationsummary’);

jQuery: Change ASP.NET Image Source at Runtime

In this post, we will see a quick tip of how to change the source of an ASP.NET Image at runtime using jQuery.

Create a new website. Create two folders called Scripts and Image. In the scripts folder, download the latest jQuery minified file. In the Image folder, add two different images of the same size. I have added dnc.png and devc.png.

In the Default.aspx page, add a reference to the jQuery file. Now add an ASP.NET Image Control pointing to one of the images and a button control using which we will change the image source at runtime. Here’s how the markup will look like

jquery-change-image

Time for some jQuery action!

In the <head> section, add the following code:

<script type="text/javascript">
    $(function () {
        $("#btnLogo").on("click", function (e) {
            e.preventDefault();
            $("#imgLogo").attr("src", "image/dnc.png");
            $("#btnLogo").attr("disabled", "disabled");
        });
    });
</script>

The code is pretty simple. We are using the new .on() function introduced in jQuery 1.7 to attach the click event to the button. We have provided an anonymous handler function at the point of the .on() call. The first line prevents the default form submission, the second line updates the image source and the last line disables the button control.

You must be wondering why haven’t we used .bind() here. To know more, read my post jQuery 1.7: What happened to live and die?

OUTPUT

Before

image

After

image

jQuery 1.7: What happened to live and die?

In case you did not notice, in jQuery 1.7, the .live() and .die() methods have been deprecated. That does not mean you cannot use them but .on() and .off() are now the new and recommended ways to attach event handlers!

If you open the jQuery code file, you will observe that  .bind(), .live() and .delegate()  now map to .on(). Similarly .unbind(), .die() and .undelegate() now map to .off()

jquery-on

Why were .bind() and .live() deprecated?

The jQuery documentation states: For earlier versions, the .bind() method is used for attaching an event handler directly to elements. Handlers are attached to the currently selected elements in the jQuery object, so those elements must exist at the point the call to .bind() occurs. .on() provides a more streamlined, simplified and unified way of attaching events.

One of the biggest issues with .live() was performance. After using the .live() in your code, one would eventually end up attaching plenty of listeners. With .on(). since you have to specify a parent container, only one listener gets added.

StackOverflow user Interstellar_Coder has put forward a nice list of drawbacks of .live()
  • jQuery attempts to retrieve the elements specified by the selector before calling the .live()method, which may be time-consuming on large documents.
  • Chaining methods is not supported. For example, $("a").find(".offsite, .external").live( ... ); is not valid and does not work as expected.
  • Since all .live() events are attached at the document element, events take the longest and slowest possible path before they are handled.
  • Calling event.stopPropagation() in the event handler is ineffective in stopping event handlers attached lower in the document; the event has already propagated to document.
  • The .live() method interacts with other event methods in ways that can be surprising, e.g.,$(document).unbind("click") removes all click handlers attached by any call to .live()!
Changing from .live to .on()

Here’s an example of changing your existing .live() code to .on()

jquery-on-vs-live

See a Live Demo

You may also want to read jQuery 1.7 Custom Events Using .on Method

Expression Blend Preview for Silverlight 5

Microsoft has released an updated version of the Expression Blend Preview for Silverlight 5 which provides support for creating Silverlight 5 and Silverlight 5 SketchFlow projects.

You may probably recollect that Expression Blend Preview for SL 5 was released in September 2011. This update comes with a GoLive license and its ration date has been extended to June 30, 2013. Moreover the Silverlight 5 SDK installed with Blend supports the RTM version of Silverlight 5.

Note: Expression Blend Preview for Silverlight 5 can be installed side-by-side with Blend 4. Blend Preview for Silverlight 5 supports only Silverlight 5 projects. For Silverlight 3 and 4 projects, please use Expression Blend 4.

You can download this release from the Microsoft Download Center: Microsoft Blend Preview for Silverlight 5.