As you might have noticed, i already wrote some posts about custom fieldtypes. But i didn’t wrote about this: How can you access the other fields and its values and maybe change them or use them within your custom fieldtype code?
UPDATE: watch this post
My posts about custom fieldtypes:
- SharePoint 2010 I like custom fieldtypes
- SharePoint 2010 loading templates from subfolders
- SharePoint 2010 A custom fieldtype for Hyperlinks
I luckily found this great post. The explanation is really good. So i put it in my code and it worked. I just changed the code a little bit.
[sourcecode language=”csharp”]
//Function to interact with other fields
protected BaseFieldControl GetFieldControlByName(String fieldNameToSearch)
{
String iteratorId = GetIteratorByFieldControl(this).ClientID;
foreach (IValidator validator in Page.Validators)
{
if (validator is BaseFieldControl)
{
BaseFieldControl baseField = (BaseFieldControl)validator;
String fieldName = baseField.FieldName;
if ((fieldName == fieldNameToSearch) &&
(GetIteratorByFieldControl(baseField).ClientID == iteratorId))
{
return baseField;
}
}
}
return null;
}
//Function to search the field in the form
private ListFieldIterator GetIteratorByFieldControl(BaseFieldControl fieldControl)
{
Control iterator = this;
while (iterator != null && !(iterator is ListFieldIterator))
iterator = iterator.Parent;
return iterator as ListFieldIterator;
}
//Now writing values into the other field
TextField fieldText = (TextField)GetFieldControlByName(“MeinTest”);
fieldText.Value = “Wert1”;
[/sourcecode]
You should use this lines of code in your custom fieldtype, if you want to
- fill certain values into the other fields
- get values of certain fields
- change values dependent on an event (e.g. button click)
In the end it could look like in the screens below.
On click “inserting values” the values Wert 1 and Wert2 will be stored into the fields which are below of the button.
And it is saving the values into the list:
Hope this helps you.
..:: I LIKE SHAREPOINT ::..
Leave a Reply
You must be logged in to post a comment.