Question

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;
  Controls.Add(control);
}

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() 
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top