Tuesday, 28 June 2011

Autocomplete ForeignKey Field Template Now on NuGet

I have just added the ForeignKey field template based on the old Futures Autocomplete filter to NuGet at NotAClue.DynamicData.AutocompleteForeignKey 

NotAClue.DynamicData.AutocompleteForeignKeySample

Autocomplete in action

All you need is to add a UIHint attribute to the to set the field template,

Note: only works on Foreign Key columns

you can als0 set the number of characters you need to type before autocomplete kicks in.

[UIHint("Autocomplete", null, "MinimumPrefixLength",2)]
public Supplier Supplier { get; set; }

By setting the MinimumPrefixLength in the UIHint’s ControlParameters.

24 comments:

Alejandro said...

i saw in your posthow to upload an image to the database, but i am interested in uploading an image gallery , how can i dothis? what should i do?

FishOfPrey said...

Very helpful, thanks. Is there an equivalent for a filter?

Steve said...

hio FishOfPrey see http://csharpbits.notaclue.net/2011/06/second-nuget-package-for-dynamic-data.html it's one of these filters and was originally a filter in the old Futures project on aspnet.codeplex.com

Steve

oterrada said...

I'm trying your package from NuGet and I have always the same exception:
"'customerID' is not a foreign key column and cannot be used here."

Where customerID is defined in metadata class as
[UIHint("Autocomplete",null, "MinimumPrefixLength",2)]
public global::System.Int32 customerID { get; set; }

Could it be a problem use Entity Data for DD? Your example seems to be with Linq2SQL.

Thx in advance,

Uri

Steve said...

no I have tested it wil EF and it works no problem. I will be supplying all the source code soon as I creating an OSS project on codeplex with all my stuff in.

Regards
Steve

oterrada said...

Ups! sorry, it's true, and it works fine! :)

I was trying the autocomplete with the foreign key ID field not from Navigation Properties from the model.

I'm trying to fix a transparency problem with webkit based browsers.

Thanks for all your shared work!
Uri

Steve said...

Your welcome Uri :)

Steve

oterrada said...

To fix the transparency bug (tested on FF, Chrone, Opera & Safari), we changed the ccs file.

.autocomplete_completionListElement {
[...]
background-color : window;/*inherit*/
[...]
}

Uri

Steve said...

Thanks Uri, I'll give that a try and add to the main project when i release on Codeplex.com

Steve

oterrada said...

Hi Steve,

I think I found a bug (and a fix), when you add an entity from a filtered list where the filter is with an Autocomplete field, the filtered value is not traspassed to the new entity by default.

I changed the code in Autocomplete_Edit.asc.cs:

protected override void OnDataBinding(EventArgs e)
{
base.OnDataBinding(e);

if (Mode == DataBoundControlMode.Edit || Mode == DataBoundControlMode.Insert) /*changed*/
{
string selectedValueString = GetSelectedValueString();
if (selectedValueString.Trim().Length > 0) /*added*/
{
MetaTable parentTable = Column.ParentTable;
IQueryable query = parentTable.GetQuery();
// multi-column PK values are separated by commas
var singleCall = LinqExpressionHelper.BuildSingleItemQuery(query, parentTable, selectedValueString.Split(','));
var row = query.Provider.Execute(singleCall);
string display = parentTable.GetDisplayString(row);

AutocompleteTextBox.Text = display;
AutocompleteValue.Value = selectedValueString;
}
}

}

Steve said...

Hi oterrada, I have not seen that myself I will take you word for it thanks for posting.

Steve

Unknown said...

Hi Steve,

Thanks a lot for all of the material. I am not sure if this is a mistake that I made, but it seems that this tool does not work if the display column of the parent table is a data type other than a string, specifically an integer, because the autocomplete query relies on the string method "StartsWith". Have you seen this problem?

Steve said...

That is correct, you would need to do some tweaking to get aroung that.

Steve

JT said...

Hi Steve. I just installed the field template via NuGet, but now I'm getting compile errors on the two new field templates (Autocomplete.ascx and Autocomplete_Edit.ascx).

In each case, the error is thrown on line 1 of the file, and the error message reads "Could not load type 'ASP.AutocompleteField'" or "Could not load type 'ASP.Autocomplete_EditField'" (respectively).

My first response was to go into the codebehind file, which is throwing an error on line 47: "The name 'HyperLink1' does not exist in the current context." This has me baffled because HyperLink1 is defined in the parent ascx file.

Is this anything you've ever seen before? I'm wondering if there's something really basic that I'm completely missing here!

Thanks! -- JT

JP said...

When I try to edit the value of a field using the autocomplete, it works fine.
But when I try to remove a value (by emptying the field), the value is still there after I click on Update.
Any idea why ?
Also I noticed you have a unused function in Autocomplete_EditField.cs
ClearButton_Click()

JP said...

When I try to edit the value of a field using the autocomplete, it works fine.
But when I try to remove a value (by emptying the field), the value is still there after I click on Update.
Any idea why ?
Also I noticed you have a unused function in Autocomplete_EditField.cs
ClearButton_Click()

Stephen Naughton said...

Good point, I think having looked at the field template the issue is with the Autocomplete control from the ajax control toolkit you will have to look there sorry.

I abandoned the Ajax Control toolkit lonfg ago sorry.

Steve

JP said...

Hi Steve,

First : thanks a lot for making so much code available to download/use !
Dynamic Data is nice but with all the features you have added, it became really great.
At some point, I should put my project on GitHub.
I used Dynamic Data (+Steve's features) to build a system to manage the telephone inventory of the organization I work for.
We have over 300k employees, over 500k phone devices and over 15k buildings.
The system is used by telecom agents to edit information about these devices.

Back to the feature I need to implement :
- A telecom agent wants to assign a device to a building
- But we have so many buildings that I can't use a dropdownlist.
- Each building is identified by a StructureNumber but I currently use the auto-complete to allow the user to type the address so he does not have to know the StructureNumber. It's work relatively well but I still need to improve it a little, for example : when the user enters an invalid building (using copy/paste instead of auto-complete), it's not obvious that he is making a mistake (it becomes obvious only when he clicks on Update and gets FK error).

What do you use instead of this Ajax Control ?

JP said...

FYI, I was thinking about my 'problem' (an alternative for the long building dropdownlist).
I think the best might be to restructure my building table in this format : Country->State->City (current missing table)->Building and I could use your CascadeHierarchicalFieldTemplate !

Btw, sometimes I feel like I'm a good programmer, then I look at your code and I feel small :P

Thanks !
JP

Stephen Naughton said...

You certainly are a very good programmer, when I write this down I have already been through all the pain of getting it working like you would, but it look slick when it's all typed up nice and people think that just came out of my head like that but it doesn't :D

Steve

Xaad Rauf said...

my project is complete and working..now i want to add an autocomplete textbox..
I have installed this package and put that attribute..but it is not working still..do i have to create controller and views again?

Stephen Naughton said...

Hi Xaad, sorry this is for Web Forms Dynamic Data not MVC

Steve

bestuur said...

Hi Stephen,

Still trying to make this work... but I have the same problem as JP (18 April 2012 at 21:23)

Is there something obvious I am missing? Is there a better way of doing this?

Many thanks!

Stephen Naughton said...

Hi Bestuur, sorry I don't have a version that works currently I have not used the Ajax Control Toolkit for a few years now sorry.

Steve