///
/// Generic method to load information in a DropDownList control.
///
/// DropDownList control.
/// List to get data.
/// Column to get the text for the elements.
/// Column to get the value for the elements.
/// This DropDownList control is necessary for fill the DropDownList.
/// Column to filter values from the DropDownList that depends on.
private void LoadDropDownList( DropDownList ddl, SPList list, string titleField, string valueField,
DropDownList ddlDependsOn, string filterField )
{
if ( ddlDependsOn.SelectedIndex < 1 )
{
ddl.Items.Clear();
ddl.Enabled = false;
}
else
{
ddl.Enabled = true;
ArrayList elements = new ArrayList();
ListItem li = new ListItem();
li.Text = "No filtrar";
li.Value = "0";
ddl.Items.Add( li );
for( int i = 0; i < list.ItemCount; i++ )
{
li = new ListItem();
try
{
string filter = "";
if ( null == list.Items[i].ContentType.Fields.GetField(filterField) )
filter = list.Items[i][list.Items[i].Fields[filterField].InternalName].ToString();
else
filter = list.Items[i][list.Items[i].ContentType.Fields[filterField].InternalName].ToString();
if ( filter.Trim().CompareTo( ddlDependsOn.SelectedItem.Text.Trim() ) == 0 )
{
if ( null == list.Items[i].ContentType.Fields.GetField(titleField) )
li.Text = list.Items[i][list.Items[i].Fields[titleField].InternalName].ToString();
else
li.Text = list.Items[i][list.Items[i].ContentType.Fields[titleField].InternalName].ToString();
if ( null != list.Items[i].Fields.GetField( valueField ) )
li.Value = list.Items[i][list.Items[i].Fields[valueField].InternalName].ToString();
else
li.Value = list.Items[i][list.Items[i].ContentType.Fields[valueField].InternalName].ToString();
if ( !elements.Contains( li.Text ) )
{
elements.Add( li.Text );
ddl.Items.Add( li );
}
}
}
catch ( Exception )
{
// This exception is for the elements that are in blank or doesn't exists.
}
}
}
}
|