Hej, Jag håller på göra en enkel ToolPart men får problem när jag går in på "Modify Shared Page" för Web Part och Det står bara: "an error has occurred" inte något mer, Det enklaste och bästa är nog att debugga din ToolPart. Vet du hur man gör? Aha. Debugga web parts är lika enkelt som att debugga vanlig kod. Custom ToolPart problem
här är min kod:
using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.WebPartPages;
namespace ForToolPart
{
[DefaultProperty("Text"),
ToolboxData("<{0}:ForToolPartClass runat=server></{0}:ForToolPartClass>"),
XmlRoot(Namespace="ForToolPart")]
public class ForToolPartClass : Microsoft.SharePoint.WebPartPages.WebPart
{
private const string defaultText = "Testing Tool Parts";
private string text = defaultText;
[Browsable(true),
Category("Custom Properties"),
DefaultValue(defaultText),
WebPartStorage(Storage.Personal),
FriendlyName("Text"),
Description("Text Property")]
[XmlElement(ElementName="SuperString")]
public string Text
{
get
{
return text;
}
set
{
text = value;
}
}
public override ToolPart[] GetToolParts()
{
ToolPart[] toolparts = new ToolPart[3];
WebPartToolPart wptp = new WebPartToolPart();
CustomPropertyToolPart custom = new CustomPropertyToolPart();
toolparts[0] = wptp;
toolparts[1] = custom;
toolparts[3] = new ForToolPart.Tool();
return toolparts;
}
protected override void RenderWebPart(HtmlTextWriter output)
{
output.Write(SPEncode.HtmlEncode(Text));
}
}
}
--------------------------------------------------------------------
my custom toolpart:
using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.WebPartPages;
using System.Text.RegularExpressions;
using System.Text;
namespace ForToolPart
{
[ToolboxData("<{0}:Tool runat=server></{0}:Tool>")]
class Tool : ToolPart
{
protected TextBox txtProperty;
protected Label lblMessage;
private string inputname;
private void Tool_Init(object sender, System.EventArgs e)
{
inputname = this.UniqueID + "message";
}
public Tool()
{
// Set default properties
this.Title="Custom Text";
this.Init += new EventHandler(Tool_Init);
}
protected override void CreateChildControls()
{
lblMessage = new Label();
lblMessage.Width = Unit.Percentage(100);
Controls.Add(lblMessage);
txtProperty = new TextBox();
txtProperty.Width = Unit.Percentage(100);
Controls.Add(txtProperty);
}
public override void ApplyChanges()
{
try
{
//base.ApplyChanges();
StringBuilder TelNr = new StringBuilder();
TelNr.Append(@"^\d{3}-\d{3}-\d{4}");
//Regex objRegEx = new Regex("\b\\d{3}-\\d{3}-\\d{4}\b");
Regex objRegEx = new Regex(TelNr.ToString());
Match objMatch = objRegEx.Match(txtProperty.Text);
if (objMatch.Success)
{
ForToolPartClass objWebPart = (ForToolPartClass)this.ParentToolPane.SelectedWebPart;
objWebPart.Text = txtProperty.Text;
lblMessage.Text = "";
}
else
{
lblMessage.Text = "Invalid phone number";
txtProperty.Text = "###-###-####";
}
}
catch (ArgumentException ex) { throw ex; }
}
public override void SyncChanges()
{
//base.SyncChanges();
try
{
Regex objRegEx = new Regex("\b\\d{3}-\\d{3}-\\d{4}\b");
Match objMatch = objRegEx.Match(txtProperty.Text);
if (objMatch.Success)
{
ForToolPartClass objWebPart = (ForToolPartClass)this.ParentToolPane.SelectedWebPart;
txtProperty.Text = objWebPart.Text;
lblMessage.Text = "";
}
else
{
lblMessage.Text = "Invalid Phone Number..";
txtProperty.Text = "###-###-####";
}
}
catch (ArgumentException ex) { throw ex; }
}
public override void CancelChanges()
{
//base.CancelChanges();
ForToolPartClass objWebPart = (ForToolPartClass)this.ParentToolPane.SelectedWebPart;
objWebPart.Text = "";
txtProperty.Text = "###-###-####";
lblMessage.Text = "";
}
protected override void RenderToolPart(HtmlTextWriter output)
{
//base.RenderToolPart(output);
ForToolPartClass objWebPart = (ForToolPartClass)this.ParentToolPane.SelectedWebPart;
txtProperty.Text = objWebPart.Text;
lblMessage.RenderControl(output);
output.Write("<br>");
txtProperty.RenderControl(output);
}
}
}Sv: Custom ToolPart problem
/JohanSv: Custom ToolPart problem
Microsoft har bra beskrivningar:
http://msdn.microsoft.com/library/en-us/spptsdk/html/DebuggingWebParts_SV01022252.asp
http://msdn.microsoft.com/library/en-us/odc_SP2003_ta/html/sharepoint_northwindwebparts.asp
/Johan