dropdown of custom webpart with toolpart as a source URL
-
11 พฤษภาคม 2555 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 11 พฤษภาคม 2555 13:35
ตอบทั้งหมด
-
12 พฤษภาคม 2555 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_ 12 พฤษภาคม 2555 7:12
- ทำเครื่องหมายเป็นคำตอบโดย Qiao WeiMicrosoft, Moderator 17 พฤษภาคม 2555 9:40