Mallu,
This can be achieved by creating Editorpart for your custom web part.
- Lets say the web part that we are building is RightNav.ascx
- Create a .CS file under your web part (.ascx)
- Full code of the EditorPart.cs is as follows
You may have to edit the code as per your requirements
class RightNavEditorPart : EditorPart
{
#region Properties
public string ListId { get; set; }
public string ListName { get; set; }
RightNav ParentWebPart { get; set; }
#endregion
#region Controls
public Label ListPrompt = new Label() { Text = "Right Nav Links" };
public DropDownList ddlLists = new DropDownList() { Width = 175, ID = "ddlLists" };
#endregion
#region Apply and Sync Changes
public override bool ApplyChanges()
{
bool returnValue = false;
try
{
EnsureChildControls();
ParentWebPart = WebPartToEdit as RightNav;
if (ParentWebPart != null)
{
ParentWebPart.ListId = GetListSelectedValue(ddlLists);
ParentWebPart.ListName = GetListSelectedText(ddlLists);
returnValue = true;
}
}
catch (Exception ex)
{
//Logging.Log(ex);
}
return returnValue;
}
public override void SyncChanges()
{
EnsureChildControls();
ParentWebPart = WebPartToEdit as RightNav;
if (ParentWebPart != null)
{
SetListSelectedText(ParentWebPart.ListName, "--Select List--", ddlLists);
}
}
#endregion
#region Methods
private string GetListSelectedValue(DropDownList ddl)
{
string returnValue = string.Empty;
if (ddl.SelectedIndex != 0)
{
returnValue = ddl.SelectedValue;
}
return returnValue;
}
public string GetListSelectedText(DropDownList ddl)
{
string returnValue = string.Empty;
if (ddl.SelectedIndex != 0)
{
returnValue = ddl.SelectedItem.Text;
}
return returnValue;
}
private void SetListSelectedText(string text, string defaultValue, DropDownList ddl)
{
if (!string.IsNullOrEmpty(text))
{
if (ddl.Items.FindByText(text) != null)
{
ddl.Items.FindByText(text).Selected = true;
}
}
else if (!string.IsNullOrEmpty(defaultValue))
{
if (ddl.Items.FindByText(defaultValue) != null)
{
for (int i = 0; i < ddl.Items.Count; i++)
{
if (ddl.Items[i].Text == defaultValue)
{
ddl.SelectedIndex = i;
break;
}
}
}
}
}
#endregion
#region Control Overrides
protected override void CreateChildControls()
{
base.CreateChildControls();
this.ChromeType = PartChromeType.None;
try
{
Controls.Add(ListPrompt);
Controls.Add(ddlLists);
LoadWebLists();
}
catch (Exception ex)
{
// Log(ex);
}
}
protected override void RenderContents(HtmlTextWriter writer)
{
try
{
writer.Write("<div class='ms-TPBody' id='SGRightNav-editorpart'>");
writer.Write("<table>");
writer.Write("<tr><td>");
writer.Write("<div class='UserSectionHead'>");
ListPrompt.RenderControl(writer);
writer.Write("</div>");
writer.Write("<div class='UserSectionBody'>");
writer.Write("<div class='UserControlGroup'>");
ddlLists.RenderControl(writer);
writer.Write("</div>");
writer.Write("</div>");
writer.Write("</td></tr>");
writer.Write("</table>");
writer.Write("</div>");
}
catch (Exception ex)
{
//Logging.Log(ex);
}
}
#endregion
#region Data Loading Methods
private void LoadWebLists()
{
try
{
//SPSecurity.RunWithElevatedPrivileges(delegate()
//{
ddlLists.Items.Clear();
List<ListItem> imageListItems = new List<ListItem>();
SPListCollection docLibraryColl = SPContext.Current.Web.GetListsOfType(SPBaseType.GenericList);
foreach (SPList list in docLibraryColl)
{
if (!list.Hidden)
{
//if (list.GetType() == typeof(SPPictureLibrary) ||
// list.TemplateFeatureId.ToString() == "4bcccd62-dcaf-46dc-a7d4-e38277ef33f4" ||
// list.TemplateFeatureId.ToString() == "bb8630c8-73c5-4a62-abf8-6d7d8bcb0e81")
{
imageListItems.Add(new ListItem() { Text = list.Title, Value = list.ID.ToString() });
}
}
}
imageListItems.Sort(delegate(ListItem l1, ListItem l2)
{
return l1.Text.CompareTo(l2.Text);
});
imageListItems.Insert(0, new ListItem() { Text = "--Select List--" });
foreach (ListItem li in imageListItems)
{
ddlLists.Items.Add(li);
}
ParentWebPart = WebPartToEdit as RightNav;
SetListSelectedText(ParentWebPart.ListName, "--Select List--", ddlLists);
//});
}
catch (Exception ex)
{
// Log(ex);
}
}
#endregion
}
- In your .ascx.cs file place the following method
You may have to write some extra code to utilize the properties here in the .ascx.cs file
public override EditorPartCollection CreateEditorParts()
{
EditorPartCollection epc = null;
try
{
List<EditorPart> lstEditorPart = new List<EditorPart>();
RightNavEditorPart ep = new RightNavEditorPart();
ep.ID = this.ID + "_EditorPart";
lstEditorPart.Add(ep);
epc = base.CreateEditorParts();
epc = new EditorPartCollection(epc, lstEditorPart);
}
catch (Exception ex)
{
}
return epc;
}
Let me know if you have any questions