在Kentico 10中从Web部件属性获取文本的技术问询
Hey there! Let's walk through how to properly retrieve the selected value from your custom web part property (populated via that SQL query) in Kentico 10. I'll cover standard implementation steps and common troubleshooting tips to get you unblocked.
Step 1: Confirm Your Property Configuration
First, make sure your web part property is set up to return the DocumentGuid as the selected value:
- If you're using a UniSelector control for the property:
- In the property settings, set the Return value option to "Value field" (this ensures the selected
DocumentGuidis stored, not just the display textDocumentName). - Verify your SQL query returns valid results by running it directly in Kentico's built-in SQL query tool to confirm rows are returned.
- In the property settings, set the Return value option to "Value field" (this ensures the selected
Step 2: Read the Value in Your Web Part's .cs File
In your custom web part class, use Kentico's API to bind and access the selected value:
1. Define a Property to Store the Selected Value
Add a property to your web part class to hold the selected DocumentGuid:
// Use a name that matches your property's purpose public string SelectedDocumentGuid { get; set; }
2. Populate the Property in SetProperties()
Override the SetProperties() method to pull the value from the web part's configuration:
public override void SetProperties() { base.SetProperties(); // Replace "YourPropertyName" with the exact name you gave your web part property SelectedDocumentGuid = ValidationHelper.GetString(GetValue("YourPropertyName"), string.Empty); }
3. (Optional) Convert the Guid to a Kentico Document Object
If you need to work with the actual document (not just the Guid), use Kentico's DocumentHelper to fetch it:
if (!string.IsNullOrEmpty(SelectedDocumentGuid)) { Guid docGuid = new Guid(SelectedDocumentGuid); // Replace "en-us" with your target culture if needed TreeNode selectedDocument = DocumentHelper.GetDocument(docGuid, "en-us"); if (selectedDocument != null) { // Access document properties as needed for your logic string docName = selectedDocument.DocumentName; string docPath = selectedDocument.NodeAliasPath; } }
Common Troubleshooting Tips
- Double-check property names: Ensure the name passed to
GetValue("YourPropertyName")matches exactly what you entered in the web part property definition (it’s case-sensitive). - Validate SQL results: If no value is being returned, confirm your SQL query returns rows when run against your Kentico database.
- Check UniSelector return settings: If you're using UniSelector, make sure you didn’t accidentally set it to return the "Text field" instead of the "Value field".
- Debug the value: Add a debug log or temporarily write the
SelectedDocumentGuidvalue to the page to confirm it’s being populated correctly.
内容的提问来源于stack exchange,提问作者Vidhyardhi Gorrepati

