Welcome to deBUG.to Community where you can ask questions and receive answers from Microsoft MVPs and other experts in our community.
0 like 0 dislike
1.1k views
in SharePoint Server by 39 47 54
I have a wired issue related to custom property in Visual WebPart where the custom property is shown properly and already holds a value when editing the web part properties, but when calling the custom property value in code-behind it always returns NULL?

Anyone can help me to solve this problem and guide me How can I use custom property in Visual WebPart without returning a NULL value.

1 Answer

1 like 0 dislike
by 159 196 380
selected by
 
Best answer

Why Custom property is always returned null in Visual WebPart?

Looks like you are using Visual WebPart (Farm Solution only) that needs different configurations to implement custom property.

custom property in Visual WebPart (Farm Solution only)

How to configure Custom Property in Visual Visual WebPart (Farm Solution only)?

To configure custom property in Visual WebPart (Farm Solution only) in a correct way, you have to do the following:

  1. In your Visual WebPart, Open the "CS" file, and under the class definition add your Property definition.
  2. Go back to open the ".ascx.cs"
  3. Below the class definition, create a new instance from the class name that was used in the "CS" file.

     public ContactForm contactform { get; set; }
    
  4. Again, go back to "CS" file. file, and at CreateChildControls() function, add the below code
    how to use custom property in Visual WebPart Farm Solution Only
  5. To get the property value in the "ascx.cs", you have to do the following:

     String value = contactform.PropertName;
    

Check the detail steps and the final code at How to create a custom property in Visual WebPart Farm Solution Only?


See Also

by 39 47 54
0 0
You saved my day ♥ and the link is very helpful, thank you!
If you don’t ask, the answer is always NO!
...