Right now I have created a bunch of custom controls in the ToolPart panel and I want to set a default value for one of the text boxes. Obviously I want the value to be editable by the user.

I have set it up as follows:

in the class file with the _ascxPath I have the {get; set;} statements like follows:

public string TextBox1 {
 get {return _TextBox1;}
 set {_TextBox1 = value;}
string _TextBoxl 

in the CreateChildControls() method in the same class file I have the following:

WebPart1UserControl control = Page.LoadControl(_ascxPath) as WebPart1UserControl;
if (control != null) {
  control.WebPart = this;

in a public override ToolPart[] GetToolParts() I have the following:

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

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

return allToolParts;

in another class file that I called CustomToolPart I have all of the controls declared in a public override void CreateChildControls() i.e.:

TextBox txtTextBox1; 

and corresponding labels added to a new Panel() I called toolPartPanel. All of the controls get added to the toolPartPanel and then the toolPartPanel gets added to the controls.

I also have a public override void in that class that checks to see if the fields are valid as well as sets the text fields created in the class equal to the webpart controls declared with {get; set;} on the other page.

Everything works lovely, I can access whatever the user types in the textbox and do as I will in the code with it. I cannot, however, set a default value for a textbox. When the textbox is empty I want the textbox.text to be .. say "default value".

Whenever I add an if statement in either classes I the value becomes non-editable and when I add an if statement to the ascx.cs file I get a null exception.

Any ideas on how I can set the default value of the textbox in the toolpart panel that I created using the above method.

Thank you so much for reading and assisting. Your help is truly appreciated.

O In the custom Tool Part class, in the CreateChildControls() I also have try/catches that set the txtTextbox1.Text to the WebPart string declared in the other class file like so:

WebPart1 wp = (WebPart1)this.ParentToolPane.SelectedWebPart;
//the above is the declaration of wp. 
txtTextBox1.Text += wp.TextBox1.ToString() 

1 Answer 1


I believe you can accomplish what you want by adding some code to the property declaration like this:

public string TextBox1 {
 get {
   if(!String.IsNullOrEmpty(_TextBox1)) {
     return _TextBox1;
   } else {
     return "My Default";
 set {_TextBox1 = value;}
string _TextBoxl;

To be honest there is so much information in your question it is hard to tell exactly where the problem is, but if you just want to use a default value whenever the property isn't set (blank) then this should take care of it.

  • Yes that is it, I believe I'll try that. Sorry if the amount of text/code is overwhelming. But that is what I'm after.
    – MyName
    Feb 22, 2012 at 19:22
  • I had if statements like that before, but they didn't work. I found an if statement I put into my class file that checked if the Document Library I was using was a sharepoint list. But that was the problem, I was using SPList instead of SPDocument library so a whole bunch of code wasn't being executed. Thank you so much for the response!! && Yes it worked.
    – MyName
    Feb 22, 2012 at 19:31
  • 1
    +1 I never thought you could have too much information in a question... until now. :P Either way, nice Q&A!
    – Kit Menke
    Feb 22, 2012 at 19:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.