vendredi 22 octobre 2010

jQuery Autocomplete : set autocomplete field to a value

I struggled to find this and the solution is so simple…

In the select event, you can set the value of the field and if you return false, the value won’t be the selected item.

 $(function() {
$("input#ZipCode").autocomplete({
source: function(request, response) {
$.ajax({
url: '<%= Url.Action("ZipCodeAutoComplete", "Home", new { area = "" }) %>',
data: { term: request.term },
success: function(data) {
response($.map(data, function(item) {
return { label: item.ZipCodel + " - " + item.City, ZipCode: item.ZipCodel, City: item.City }
}))
}
})
},
minLength: 3,
select: function(event, ui) {
$("#City").val(ui.item.Ville);
$("#ZipCode").val(ui.item.ZipCode);
return false;
}
});
});

jeudi 21 octobre 2010

Connect to Oracle using NHibernate

Here is my setup to connect to an Oracle database using the ODP.NET provider.

Install Oracle client v9 R2 or greater.

Reference Oracle.DataAccess dll in your project.

Configure your database connection in the tnsnames.ora file. This file is located in $ORACLE_HOME/network/admin/tnsnames.ora.

Configure your connection string in your application. I usually use this syntax:

<add name="default" connectionstring="Data Source=tnsnames_alias;User ID=username;Password=pwd">

Then use the OracleDataClientDriver in your NHibernate configuration or OracleDataClientConfiguration if you use FluentNHibernate.


This should do it!

mercredi 21 juillet 2010

User/role extension methods in an ASP.NET MVC view

This is a small Html helper that I use when I need to check the role in a view

public static class PageHelper
{
public static bool IsAdmin(this ViewUserControl pg)
{
return pg.Page.User.IsInRole("Admin");
}

public static bool IsCustomer(this ViewUserControl pg)
{
return pg.Page.User.IsInRole("Customer");
}
}
Don't forget to add the namespace for your extension methods in your Web.config :
<namespaces>
<add namespace="MyProject.Web.Helpers" />
</namespaces>

samedi 3 juillet 2010

Blogger open link in new window

In quest for the perfect setup for my blog, I know want to have my links open in a new browser window. The simplest way to do it is to add this tag in the <head></head> part of your template :
<base target="_blank" />

vendredi 2 juillet 2010

Source code syntax highlighting in blogger

I finally figured out how to have syntax highlighting for the source code in blogger thanks to this blog post.
This is the list of brushes I use :
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJScript.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushBash.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushSql.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushXml.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushCSharp.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushRuby.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushPython.js' type='text/javascript'/>

AvalonEdit in a WPF application with MVVM

According to this forum message, it's not possible to bind the Text property from the Avalon Editor to a simple property of a ViewModel.
Instead, you can use the Document property from the editor and bind it to a property of your ViewModel.
Here is the code for the view :
<Window x:Class="AvalonEditIntegration.UI.View"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:AvalonEdit="clr-namespace:ICSharpCode.AvalonEdit;assembly=ICSharpCode.AvalonEdit"
        Title="Window1"
        WindowStartupLocation="CenterScreen"
        Width="500"
        Height="500">
    <DockPanel>
        <Button Content="Show code"
                Command="{Binding ShowCode}"
                Height="50"
                DockPanel.Dock="Bottom" />
        <AvalonEdit:TextEditor ShowLineNumbers="True"
                               Document="{Binding Path=Document}"
                               FontFamily="Consolas"
                               FontSize="10pt" />
    </DockPanel>
</Window>

And the code for the ViewModel :
namespace AvalonEditIntegration.UI
{
    using System.Windows;
    using System.Windows.Input;
    using ICSharpCode.AvalonEdit.Document;

    public class ViewModel
    {
        public ViewModel()
        {
            ShowCode = new DelegatingCommand(Show);
            Document = new TextDocument();
        }

        public ICommand ShowCode { get; private set; }
        public TextDocument Document { get; set; }

        private void Show()
        {
            MessageBox.Show(Document.Text);
        }
    }
}