SurfRay

Home Tech Blog Tech Talk Creating a custom list field – Part 2

Creating a custom list field – Part 2

E-mail Print PDF

In this blog I will extend the example from the previous blog, so that the FieldControl will show a nice list instead of the xml string.

Method

I will keep this very simple and create a html form and add a label and a list box, where the label will just say “Students” and the list box will contain the students’ names and birthdays.

Implementation

In part one of this blog we overrode the Value parameter, now we are going to override two methods, namely CreateChildControls and Render. These will be covered in the next two sections.

CreateChildControls

The CreateChildControls is responsible for creating all controls it needs in preparation for a post back or for rendering.

Let’s start by looking at the code:

protected override void CreateChildControls()

{

if (ControlMode == SPControlMode.Invalid)

return;

 

base.CreateChildControls();

 

_students = ItemFieldValue as List;

 

_table = new HtmlTable();

ListBox studentListBox = null;

 

HtmlTableRow row = new HtmlTableRow();

_table.Rows.Add(row);

 

HtmlTableCell cell = new HtmlTableCell();

row.Cells.Add(cell);

cell.InnerText = "Students:";

cell.ColSpan = 2;

 

row = new HtmlTableRow();

_table.Rows.Add(row);

 

cell = new HtmlTableCell();

row.Cells.Add(cell);

 

studentListBox = new ListBox();

studentListBox.Rows = 12;

 

if (_students != null)

{

foreach (Student student in _students)

studentListBox.Items.Add(student.Name + " " +

student.BirthDay.ToShortDateString());

 

cell.Controls.Add(studentListBox);

}

 

base.Controls.Add(_table);

 

}

To begin with we check what the control mode is. In a later post we will use this to render the control differently depending on, if we are in the list mode or if we are in the edit mode.

Then we make sure that we get the value of the field as a List.

Now we create a new table and add rows to it. The first row contains a label saying “Students:”. The next row contains a list box (with 12 rows).

We then iterate through the _students list and create a string with the name and birthday of each student. We add that string to the list box.

Now we have added all the controls to the listbox.

Render

The Render method is obviously responsible for rendering our control.

As all our controls have been added to the _table variable, it is easy to render the control, as we will just need to call this the RenderControl on this object.

protected override void Render(HtmlTextWriter output)

{

_table.RenderControl(output);

}

Going to a list with the column we created in Part 1 of this blog, we can see the result.

 

 
Get Trial
Get Price Quote

Upgrading Ontolica 4.0 to Ontolica 2010

SurfRay’s commitment is to provide the best possible search solution for SharePoint. By continuing to refine our software and deliver improvements that delight and enable...

More:

How to index PDF files in SharePoint

Microsoft SharePoint does not index Adobe PDF files by default. Hence, additional steps are required to enable and perform indexing, to be able to search for the content ...
More: