views:

23

answers:

0

When you have a foreign key to a table with a lot of rows, the default dynamic data dropdown box is not really handy. So I decided to make an autocomplete control.

I copied the ForeignKey_Edit.ascx and changed the layout, see the ascx at the end of the post. (The PersonSelectedId textbox will be hidden in the future...)

With some jquery magic I convert Textbox1 to an autocomplete input box, that works. When I select something I put the ID of the selected item in the PersonSelectedId box. That works too. So when I post the form Textbox1 has a name and the PersonSelectedId box has it corresponding ID.

When I post I get an System.Web.UI.WebControls.EntityDataSourceValidationException exception: Error setting value of locationmangerid: Cannot convert the value for parameter locationmanagerid to System.Int32. LocationmanagerId is the foreign key field.

I checked to see that the value in ExtractValues is indeed the same as it would be when it is an ForeignKey_Edit control. It is.

I also tried to return PersonSelectedId in the override DataControl, but that didn't help.

Should I also do something in the OnDataBinding, or is my fail in some other part?

The ascx:

<%@ Control Language="C#" CodeBehind="PersonSelect_Edit.ascx.cs" Inherits="SB2Onderhoud.PersonSelect_EditField" %>
<span>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# FieldValueEditString %>' CssClass="DDTextBox roaringPersonselect_edit"></asp:TextBox>
<asp:TextBox ID="PersonSelectedId" cssClass="roaringPersonselect_hidden" runat="server" Text="<%# FieldValueEditString %>"/>
<asp:RequiredFieldValidator runat="server" ID="RequiredFieldValidator1" CssClass="DDControl DDValidator" ControlToValidate="TextBox1" Display="Static" Enabled="false" />
<asp:RegularExpressionValidator runat="server" ID="RegularExpressionValidator1" CssClass="DDControl DDValidator" ControlToValidate="TextBox1" Display="Static" Enabled="false" />
<asp:DynamicValidator runat="server" ID="DynamicValidator1" CssClass="DDControl DDValidator" ControlToValidate="TextBox1" Display="Static" />
</span>

The code behind:

public partial class PersonSelect_EditField : System.Web.DynamicData.FieldTemplateUserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Column.MaxLength < 20)
        {
            TextBox1.Columns = Column.MaxLength;
        }
        TextBox1.ToolTip = Column.Description;

        SetUpValidator(RequiredFieldValidator1);
        SetUpValidator(RegularExpressionValidator1);
        SetUpValidator(DynamicValidator1);
    }

    protected override void OnDataBinding(EventArgs e)
    {
        base.OnDataBinding(e);
        if (Column.MaxLength > 0)
        {
            TextBox1.MaxLength = Math.Max(FieldValueEditString.Length, Column.MaxLength);
        }
    }

    protected override void ExtractValues(IOrderedDictionary dictionary)
    {
        string value = PersonSelectedId.Text;
        if (String.IsNullOrEmpty(value))
        {
            value = null;
        }
        ExtractForeignKey(dictionary, value);
        //dictionary[Column.Name] = ConvertEditedValue(PersonSelectedId.Text);
    }

    public override Control DataControl
    {
        get
        {
            return TextBox1;
        }
    }

}