Saturday, January 24, 2009

Setting custom property to the webpart - usercontrol

There are occasions when it may be necessary to provide web part users with a richer interface for setting custom properties than SharePoint provides out-of-the-Box

Setting up the custom properties for a webpart is very easy and straight
farward approach.

Recently i came acorss requirement in one of my webpart i need to set the custom property.
we usually go for usercontrol for better look and easily maintainable instead of writing entire code in webpart itself.
In my scenario i have one usercontrol form which has a cancel button, on clicking on cancel the redirection happens basing on the configurable property.
So for this usercontrol i need to pass the custom property value, i am getting the value in webpart but had very tough time trying to pass the value to the usercontrol.


But after tweaking my head at last got succeded by doing

1. Not enabling the click event for the Cancel button in usercontrol itself, declare the click event for the usercontrol in webpart itself
2. In the create child controls mehtod find the cancel button Id, and writing the event for the cancel button

Code:

publice string _navigateUrl=string.empty;

public class CustomWebpartProperties : System.Web.UI.WebControls.WebParts.WebPart
{
UserControl customUsercontrol ;
[WebBrowsable(true), WebDisplayName("Navigate Url"),
WebDescription("set the path")]
public string CustomURL
{
get
{
return _navigateUrl;
}
set
{
_navigateUrl = value;
}
}
Button btnCancel;
public override ToolPart[] GetToolParts()
{

ToolPart[] allToolParts = new ToolPart[3];
WebPartToolPart standardToolParts = new WebPartToolPart();
CustomPropertyToolPart customToolParts = new CustomPropertyToolPart();

allToolParts[0] = standardToolParts;
allToolParts[1] = customToolParts;
allToolParts[2] = new CustomToolPart();

return allToolParts;
}







protected override void CreateChildControls()
{
customUsercontrol = (UserControl)Page.LoadControl(@"/wpresources/TesUsercontrol.ascx");

btnCancel = (Button)UserControlContactUsByEmail.FindControl("btnCancel");
btnCancel.Click += new EventHandler(btnCancel_Click);


base.Controls.Add(customUsercontrol);
base.CreateChildControls();
}

protected override void Render(HtmlTextWriter writer)
{
writer.Write(NavigateUrl);
customUsercontrol.RenderControl(writer);
}

void btnCancel_Click(object sender, EventArgs e)
{
//redirection code
SPUtility.Redirect(NavigateUrl, SPRedirectFlags.UseSource, HttpContext.Current);
}

}

This class is used to set the new custom tool part and provides the user to enter the
value for url.
public class CustomToolPart : Microsoft.SharePoint.WebPartPages.ToolPart
{
Label lbl;
Panel customToolPart;
TextBox txt;


protected override void CreateChildControls()
{
customToolPart = new Panel();
txt = new TextBox();
txt.ID = "tb";

lbl = new Label();
lbl.ID = "lbl";
lbl.Text = "Navigate Url";

customToolPart.Controls.Add(lbl);
customToolPart.Controls.Add(new LiteralControl("
"));
customToolPart.Controls.Add(txt);
Controls.Add(customToolPart);
base.CreateChildControls();
}

public override void ApplyChanges()
{
ContactUsByEmail wp = (ContactUsByEmail)this.ParentToolPane.SelectedWebPart;
wp.NavigateUrl = txt.Text;
//Button btnClick = (Button)wp.FindControl("btnCancel");
//btnClick.Click += new EventHandler(btnClick_Click);
}

}


shoot a mail to shreecanth@gmail.com if you need any further assistance.

No comments: