using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using System;
using System.ComponentModel;
using System.Web.UI.WebControls.WebParts;namespace FieldValidApp.VisualWebPart1
{
[
ToolboxItemAttribute(false)]publicpartial classVisualWebPart1 : WebPart
{
// Uncomment the following SecurityPermission attribute only when doing Performance Profiling using
// the Instrumentation method, and then remove the SecurityPermission attribute when the code is ready
// for production. Because the SecurityPermission attribute bypasses the security check for callers of
// your constructor, it's not recommended for production purposes.
// [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Assert, UnmanagedCode = true)
]
public VisualWebPart1()
{
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);InitializeControl();
protected void Page_Load(object sender,EventArgs e)
{
}
protected void Button1_Click(object sender,EventArgs e)
{
try{SPWeb web = SPContext.Current.Web;
SPList list = web.Lists["Student"];
var field = list.Fields.GetField("Age");
field.ValidationFormula = "[Age]>= 18";
field.ValidationMessage = "Age Must Be Greater or Equal to 18 To Work Here";
Label5.Text = "Custom Field Validation Enabled";
Label5.ForeColor = System.Drawing.Color.Green;field.Update();
}
catch (SPFieldValidationException ex)
{
Label5.Text = ex.Message;
}
}
} |