dropdown of custom webpart with toolpart as a source URL
-
2012年5月11日 13:32
i have a custom webpart with dropdownlist control. i want the items of the dropdown to come from a custom list..
also i need the url of the custom list to be available on the toolpane view, when we modify webpart. can you pls redirect me to correct URL or guide. thanks
cal_bonjovi
here's my code
public class DropDownWP : WebPart
{
DropDownList ddlCountry;
private string strMessage = "";
[Personalizable(PersonalizationScope.Shared), WebBrowsable(true),
WebDisplayName("Enter source URL List (http://servername/lists/Sample.aspx)"),
WebDescription("Enter source URL List (http://servername/lists/Sample.aspx)"),
Category("Custom Category")]
public string Message
{
get { return strMessage; }
set { strMessage = value; }
}
protected override void CreateChildControls()
{
SPSite mySite = new SPSite("http://servername/en-US");
SPList targetList = targetSite.OpenWeb().Lists["List of Country"];
ddlCountry = new DropDownList();
foreach (SPListItem myItem in myList.Items)
{
ddlCountry.Items.Add(myItem["Title"].ToString());
}
this.Controls.Add(ddlCountry);}
protected override void RenderContents(System.Web.UI.HtmlTextWriter writer)
{
writer.Write("<b>");
ddlCountry.RenderControl(writer);
writer.Write("</b>");
writer.Write("<br />");
writer.Write("<br />");
}
}- 已编辑 cal_bonjovi 2012年5月11日 13:35
全部回复
-
2012年5月12日 7:01
Look at there great posts about web parts: part 1 and part 2, etc.
Comments to your code:
1. Create controls in CreateChildControls method (as you do), but add items to DropDownList in PreRender method, otherwise you will unable to get selected item. Also, clear DropDownControl control items before adding.
2. Webpart properties can be written in shorter form:
[Personalizable(PersonalizationScope.Shared), WebBrowsable(true), WebDisplayName("Enter source URL List"), WebDescription("Example: http://servername/lists/Sample.aspx"), Category("List source")] public string Message { get; set; }3. The better practice is to add two properties: site url and list title. Also you can use PickerTreeDialog to select list, but it's not simple.
My contributions: SharePoint 2010 Solution Installer
SharePoint How-To: Create WebPart Page programmatically
- 已编辑 Aviw_ 2012年5月12日 7:12
- 已标记为答案 Qiao WeiMicrosoft Contingent Staff, Moderator 2012年5月17日 9:40

