Datapolis Process System SDK
Creating Control Tutorial
This walkthrough will guide you through creating a custom control. The control consists of textbox and drop down list. Drop down list contains list of data types. Text box allows us to input value. Validation is based on type selected in drop down list.
Prerequisites
Develop control
<
asp:TextBox
runat
=
"server"
ID
"tbMyValue"
Text
""
/>
asp:DropDownList
"ddlMyType"
>
</
Edit code behind
Open MyCustomFieldControl.ascx.cs file. Add using statement for Datapolis.WorkBox.Common namespace and change inheritance to WBCustomFieldControlBase.
using
System;
System.Web.UI;
System.Web.UI.WebControls;
System.Web.UI.WebControls.WebParts;
Datapolis.WorkBox.Common;
namespace
QuickLauchForm.CustomFieldControl.ControlTemplates.QuickLaunchForm.CustomFieldControl
{
public
partial
class
MyCustomFieldControl : WBCustomFieldControlBase
protected
void
Page_Load(
object
sender, EventArgs e)
}
Add Types region to the class. It has MyDataType enum for selectable types in drop down list and MyValue struct for storing data. Value of the control will be passed in following format: [type];#[value]
#region Types
// used types
private
enum
MyDataType { Integer, String, YesNo }
// wrapper struct for value
struct
MyValue
MyDataType Type;
String Value;
MyValue(
string
rawValue)
try
[] parts = rawValue.Split(
new
[] {
";#"
},StringSplitOptions.RemoveEmptyEntries);
Type = (MyDataType)Enum.Parse(
typeof
(MyDataType), parts[0]);
if
(parts.Length > 1)
Value = parts[1];
else
Value = String.Empty;
catch
(Exception)
throw
ArgumentException(
"Value is in wrong format."
);
override
ToString()
return
String.Format(
"{0};#{1}"
, Type, Value);
#endregion Types
Edit Page_Load event to populate controls with data.
#region Control Events
(!Page.IsPostBack)
// fill drop down list
foreach
(
typename
in
Enum.GetNames(
(MyDataType)))
ddlMyType.Items.Add(
ListItem(typename, typename));
// set controls values
tbMyValue.Text = _myValue.Value;
ddlMyType.SelectedValue = _myValue.Type.ToString();
#endregion Control Events
Lastly override base methods of WBCustomFieldConstrolBase.
#region WBCustomFieldControlBase
SetDefaultValue(
value)
// store default value passed from launch form
_myValue =
MyValue(value);
GetValue()
// get values from form
_myValue.Type = (MyDataType)Enum.Parse(
(MyDataType), ddlMyType.SelectedValue);
_myValue.Value = tbMyValue.Text;
// return formated
_myValue.ToString();
bool
Validate(
out
validationErrorMsg)
// validate value by selected type
validationErrorMsg = String.Empty;
isValid =
false
;
MyDataType type = (MyDataType)Enum.Parse(
switch
(type)
case
MyDataType.Integer:
int
intVal;
.TryParse(tbMyValue.Text,
intVal))
true
// set validation message
validationErrorMsg =
"Expected integer value."
break
MyDataType.String:
MyDataType.YesNo:
boolVal;
boolVal))
(tbMyValue.Text.Equals(
"yes"
, StringComparison.InvariantCultureIgnoreCase) ||
tbMyValue.Text.Equals(
"no"
, StringComparison.InvariantCultureIgnoreCase))
"Expected boolean or yes/no value."
isValid;
IsRequired()
#endregion WBCustomFieldControlBase
Safe Control Entry Edit Package.Template.xml by adding Safe Control Entry.
Paste the code below
<?
xml
version
"1.0"
encoding
"utf-8"
?>
Solution
xmlns
"http://schemas.microsoft.com/sharepoint/"
SolutionId
"2ab3374a-7777-4b24-9646-c7616cb10139"
SharePointProductVersion
"15.0"
Assemblies
Assembly
Location
"QuickLaunchForm.CustomFieldControl.dll"
DeploymentTarget
"GlobalAssemblyCache"
SafeControls
SafeControl
"$SharePoint.Project.AssemblyFullName$"
Namespace
"QuickLaunchForm.CustomFieldControl.ControlTemplates.QuickLaunchForm.CustomFieldControl"
TypeName
"*"
Safe
"True"
Deploy Now you are ready to deploy solution.
Next Steps
Optionally, after reading this walkthrough you can see the same custom control code attached in SDK Samples in Download section.