Using Custom ExpressionBuilder to set the ValidationGroup property in a UserControl.

Expression Builder lets us add inline declarative syntax to any controls’s property.
ExpressionBuilders are executed before the page life cycle even begins. It executes when the html of the controls are developed.ExpressionBuilders do NOT replace the value at render — but way way early than that.
So it’s very useful for static values, such as Web.config/app settings based values,resources & any other hardcoded values.

Plus —   It reduces load on ViewState.
It lets us use any static hardcoded value at many different places in the code , with the hardcoded part being centralized.So if in future, any change shows up — we can just change the hardcoding & that will be reflected everywhere. To learn more

The Validation controls belonging to a UserControl needs to have the ValidationGroup property set dynamically.I tried to use Custom ExpressionBuilder for that. However it’s possible that we hardcode possible ValidationGroup values in WebConfig & use the in-built <%$ appsettings: MyValue %> expression-builder. But here I tried to use HttpContext. Here’s an working example

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CallUCExpressionBuilder.aspx.cs" Inherits="CallUCExpressionBuilder" %>
<%@ Register  Src="~/UserControls/TestExpressionBuilder.ascx"  TagName="TestExpressionBuilder" TagPrefix="uc1"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="">
<head runat="server">
<form id="form1" runat="server">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1" 
 ErrorMessage="RequiredFieldValidator" ValidationGroup="<%$ ValContextItem:Page1 %>"></asp:RequiredFieldValidator>    
<br /><br />
UserControl now
<uc1:TestExpressionBuilder ID="TestExpressionBuilder1"  runat="server" />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Submit" />
//User Control
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="TestExpressionBuilder.ascx.cs" Inherits="UserControls_TestExpressionBuilder" %>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1" 
ValidationGroup="<%$ ValContextItem:Page1 %>" 
// The class file for the custom Expression builder

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Compilation;
using System.CodeDom;
using System.Web.UI;

namespace GCustomerExpressionBuilders
    public class ValContextItemExpressionBuilder : ExpressionBuilder
        public override CodeExpression GetCodeExpression
            (BoundPropertyEntry entry, object parsedData,
            ExpressionBuilderContext context)
            return new CodeMethodInvokeExpression(
                new CodeTypeReferenceExpression(
                    new CodeExpression[] { new CodePrimitiveExpression(parsedData) });

        public static object GetContextItem(string expression)
            Page p1 = HttpContext.Current.CurrentHandler as Page;
            if (p1 != null)
                HttpContext.Current.Items["Page1"] = "ValidationGrp1";
                HttpContext.Current.Items["Page2"] = "ValidationGrp2";
            if (HttpContext.Current == null)
                return string.Empty;

            return HttpContext.Current.Items[expression];

        public override object ParseExpression(string expression,
            Type propertyType, ExpressionBuilderContext context)
            return expression;

// From Web.config
        <compilation debug="true" strict="false" explicit="true" targetFramework="4.0">
                <add type="GCustomerExpressionBuilders.ValContextItemExpressionBuilder" expressionPrefix="ValContextItem"/>

This entry was posted in General ASP.Net C#. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s