Custom Control issues in Business Connecitvity Services
-
Sunday, September 26, 2010 6:50 PM
Hi ,
I am working on a BCS project. I am required to create a custom control(DropDownList) for one of the columns in the external list that is being modeled using BDC Model in Visual Studio. I could create a custom control for the list
Here is the code for the custom control and part of the BDCM file.
Listing 1 : Custom Field Control
public class GLAccountField : SPFieldText
{
public GLAccountField(SPFieldCollection fields, String fieldName) :
base(fields, fieldName)
{ }
public GLAccountField(SPFieldCollection fields, String typeName, String displayName) :
base(fields, typeName, displayName)
{ }
public override BaseFieldControl FieldRenderingControl
{
get
{
BaseFieldControl control = new GLAccountFieldControl();
control.FieldName = this.InternalName;
return control;
}
}
public override string GetValidatedString(object value)
{ …
return base.GetValidatedString(value);
}
}
public class GLAccountFieldControl : BaseFieldControl
{
protected DropDownList GLAccountSelector;
protected override string DefaultTemplateName
{
get { return "GLAccountFieldControl"; }
}
public override object Value
{
get
{
EnsureChildControls();
return this.GLAccountSelector.SelectedValue;
}
set
{
EnsureChildControls();
this.GLAccountSelector.SelectedValue = (string)this.ItemFieldValue;
}
}
protected override void CreateChildControls()
{
if (this.Field == null || this.ControlMode == SPControlMode.Display)
return;
base.CreateChildControls();
this.GLAccountSelector =
(DropDownList)TemplateContainer.FindControl("GLAccountSelector");
if (this.GLAccountSelector == null)
throw new ConfigurationErrorsException("Error: cannot load .ASCX file!");
if (!this.Page.IsPostBack)
{
this.GLAccountSelector.Items.AddRange(new ListItem[]
{
new ListItem(string.Empty, null),
new ListItem("Mom and Pop Shop (1-20)", "1-20"),
new ListItem("Small Business (21-100)", "21-100"),
new ListItem("Medium-sized Business (101-1000)", "101-1000"),
new ListItem("Big Business (1001-20,000)", "1001-20000"),
new ListItem("Enterprise Business (over 20,000)", "20000+")
});
}
}
Listing 2 : Part of BDCM file
…
<TypeDescriptor Name="associated_gl_id" TypeName="Sytem.Int32" DefaultDisplayName="GL Account" IsCollection="false">
<Properties>
<Property Name="SPCustomFieldType" Type="System.String">CustomControls.GLAccountField, MIMASBDCModel</Property>
</Properties>
</TypeDescriptor>
…
associated_gl_id is an integer field in the database. With the above mentioned TypeDescriptor I get following two errors.
- GLAccountField cannot be serialized. It needs a parameter less constructor. (I added one and passed null, null to the base constructor. Is this a correct thing to do?)
- Cannot convert Int32 to CustomControl.GLAccountField. It’s an IMetadataObectException. How do I fix this?
I have tested the control in Custom list and it is working as expected.
Could you please suggest a few corrections/improvements that would help me get over this problem.
Also would creating similar control help me display images in BCS list?
Thanks and Regards,
Zia
All Replies
-
Thursday, September 30, 2010 7:08 AM
Hi, Zia
According the error message, would you please try the following changes in your custom field type solution:
· Try adding a overload constructor without any parameters
· In your code snippet, the GLAccountFieldControl returns a string type value
-------------------------------------------------------------------
public override object Value
{
get
{
EnsureChildControls();
return this.GLAccountSelector.SelectedValue;
}
set
{
EnsureChildControls();
this.GLAccountSelector.SelectedValue = (string)this.ItemFieldValue;
}
}
-------------------------------------------------------------------
Would you please try converting it to Int32 type to match BCS field?
Hope this can help!
Best Regards,
Aaron
-
Friday, October 01, 2010 2:13 PM
Thanks Aaron. I'l try the things you've suggested.
I have tested the control in Custom list and it is working as expected. The issue with External list.
Also can you confirm that the TypeDescriptor for associated_gl_id is defined correctly?
Regards,
Zia

