RandomNumber Expression Builder : ExpressionBuilders « Data Binding « ASP.NET Tutorial

ASP.NET Tutorial
1. ASP.Net Instroduction
2. Language Basics
3. ASP.net Controls
4. HTML Controls
5. Page Lifecycle
6. Response
7. Collections
8. Validation
9. Development
10. File Directory
11. Sessions
12. Cookie
13. Cache
14. Custom Controls
15. Profile
16. Configuration
17. LINQ
18. ADO.net Database
19. Data Binding
20. Ajax
21. Authentication Authorization
22. I18N
23. Mobile
24. WebPart
25. XML
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
ASP.NET Tutorial » Data Binding » ExpressionBuilders 
19. 14. 1. RandomNumber Expression Builder
using System;
using System.Web.UI;
using System.Web.Compilation;
using System.CodeDom;
using System.ComponentModel;

public class RandomNumberExpressionBuilder : ExpressionBuilder
{
  public static string GetRandomNumber(int lowerLimit, int upperLimit)
  {
    Random rand = new Random();
    int randValue = rand.Next(lowerLimit, upperLimit + 1);
    return randValue.ToString();
  }

  public override CodeExpression GetCodeExpression(BoundPropertyEntry entry, object parsedData,ExpressionBuilderContext context){
    if (!entry.Expression.Contains(","))
    {
      throw new ArgumentException("Must include two numbers separated by a comma.");
    }
    else
    {
      string[] numbers = entry.Expression.Split(',');

      if (numbers.Length != 2)
      {
        throw new ArgumentException("Only include two numbers.");
      }
      else
      {
        int lowerLimit, upperLimit;
        if (Int32.TryParse(numbers[0], out lowerLimit&& Int32.TryParse(numbers[1], out upperLimit)){
          Type type = entry.DeclaringType;
          PropertyDescriptor descriptor = TypeDescriptor.GetProperties(type)[entry.PropertyInfo.Name];
          CodeExpression[] expressionArray = new CodeExpression[2];
          expressionArray[0new CodePrimitiveExpression(lowerLimit);
          expressionArray[1new CodePrimitiveExpression(upperLimit)
          return new CodeCastExpression(descriptor.PropertyType, new CodeMethodInvokeExpression(new CodeTypeReferenceExpression(base.GetType())"GetRandomNumber", expressionArray));
        }
        else
        {
          throw new ArgumentException("Use valid integers.");
        }
      }
    }
  }
}

File: Web.config

<?xml version="1.0"?>
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
  <appSettings/>
  <connectionStrings>
    <add name="Northwind" connectionString="Data Source=localhost;Initial Catalog=Northwind;Integrated Security=SSPI"/>
  </connectionStrings>
  
  <system.web>
   <compilation debug="true">
        <expressionBuilders>
          <add expressionPrefix="RandomNumber" type="RandomNumberExpressionBuilder"/>
        </expressionBuilders>
    </compilation>
    <authentication mode="Windows"/>
  </system.web>
</configuration>
19. 14. ExpressionBuilders
19. 14. 1. RandomNumber Expression Builder
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.