I am building a custom control. Part of the UI for this custom control is an XML defined data entry form. The fields, layout, etc... are defined in the XML. I would like to allow for certain properties, such as default values for the controls specified
in the xml, to be code expressions.
A specific Example for use on an Intranet: i want to be able to write something like
And then, when I build the control assign that as an Expression to the Text property of the field (a textbox control). I know I can build a CodeSnippetExpression to represent it. Basically new CodeSnippetExpression ("System.Web.HttpContext.Current.Request.UserHostName"),
but how do i then, from code and not declaritively, assign this expression to the Text property of a dynamically created textbox control?
That is one of the ways I am considering already, and I appreciate the link as it has a good working example i can use. But I am looking for an approach that works kind of the same way that expressions work. Kind of like the
Code Expression (http://weblogs.asp.net/infinitiesloop/archive/2006/08/09/The-CodeExpressionBuilder.aspx), but that can be used in code-behind/custom
control.
I kind of assumed that if I Can do it in design view i should be able to do it in code.
Member
30 Points
31 Posts
Dynamic Code Compile/Execution in Custom Control
Sep 25, 2009 01:05 PM|pawicks|LINK
I am building a custom control. Part of the UI for this custom control is an XML defined data entry form. The fields, layout, etc... are defined in the XML. I would like to allow for certain properties, such as default values for the controls specified in the xml, to be code expressions.
A specific Example for use on an Intranet: i want to be able to write something like
<Field ...other properties... DefaultValueExpression="System.Web.HttpContext.Current.Request.UserHostName" />
And then, when I build the control assign that as an Expression to the Text property of the field (a textbox control). I know I can build a CodeSnippetExpression to represent it. Basically new CodeSnippetExpression ("System.Web.HttpContext.Current.Request.UserHostName"), but how do i then, from code and not declaritively, assign this expression to the Text property of a dynamically created textbox control?
Any help with this would be great.
Thanks,
Peter
Star
10925 Points
2220 Posts
Re: Dynamic Code Compile/Execution in Custom Control
Sep 25, 2009 03:08 PM|Steelymar|LINK
have a good time friend:
http://www.west-wind.com/presentations/dynamicCode/DynamicCode.htm
Member
30 Points
31 Posts
Re: Dynamic Code Compile/Execution in Custom Control
Sep 25, 2009 04:11 PM|pawicks|LINK
That is one of the ways I am considering already, and I appreciate the link as it has a good working example i can use. But I am looking for an approach that works kind of the same way that expressions work. Kind of like the Code Expression (http://weblogs.asp.net/infinitiesloop/archive/2006/08/09/The-CodeExpressionBuilder.aspx), but that can be used in code-behind/custom control.
I kind of assumed that if I Can do it in design view i should be able to do it in code.
Star
10925 Points
2220 Posts
Re: Dynamic Code Compile/Execution in Custom Control
Sep 25, 2009 04:23 PM|Steelymar|LINK
in my opinion this is quite different think
None
0 Points
53 Posts
Re: Dynamic Code Compile/Execution in Custom Control
Oct 19, 2009 07:30 AM|babuji_godem|LINK
using System;
using System.CodeDom.Compiler;
using Microsoft.CSharp;
using System.Reflection;
public partial class Default4 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
ICodeCompiler objCodeCompiler = new CSharpCodeProvider().CreateCompiler();
CompilerParameters objCompilerParameters = new CompilerParameters();
objCompilerParameters.ReferencedAssemblies.Add("System.dll");
objCompilerParameters.ReferencedAssemblies.Add("System.Web.dll");
objCompilerParameters.GenerateInMemory = true;
string strCode = "";
strCode += "\t\tusing System;";
strCode += "\t\tusing System.IO;";
strCode += "\t\tusing System.Web;";
strCode += "\t\tnamespace NS";
strCode += "\t\t{";
strCode += "\t\t\tpublic class DynamicCode";
strCode += "\t\t\t{";
strCode += "\t\t\t\tpublic void ExecuteCode()";
strCode += "\t\t\t\t{";
strCode += "\t\t\t\t\tHttpContext.Current.Response.Write(\"Hello World\");";
strCode += "\t\t\t\t}";
strCode += "\t\t\t}";
strCode += "}";
CompilerResults objCompileResults = objCodeCompiler.CompileAssemblyFromSource(objCompilerParameters, strCode);
if (objCompileResults.Errors.HasErrors == true)
{
Response.Write("Error: Line>" + objCompileResults.Errors[0].Line.ToString() + ", " + objCompileResults.Errors[0].ErrorText);
return;
}
System.Reflection.Assembly objAssembly = objCompileResults.CompiledAssembly;
Object objTheClass = objAssembly.CreateInstance("NS.DynamicCode");
if (objTheClass == null)
{
Response.Write("Can't load class..... ");
return;
}
try
{
//object objResult = objTheClass.GetType().InvokeMember("ExecuteCode", BindingFlags.InvokeMethod, null, objTheClass, objFunctionParameters);
object objResult = objTheClass.GetType().InvokeMember("ExecuteCode", BindingFlags.InvokeMethod, null, objTheClass, null);
Response.Write(objResult);
}
catch (Exception ex)
{
Response.Write("Error:" + ex.Message);
}
}
}
Human Logic Pvt Ltd.