开发者

Adding controls and their events dynamically

开发者 https://www.devze.com 2023-04-10 15:35 出处:网络
I have a problem in asp.net I\'m developing a website in visual studio 2008 using (asp.net/vb) I have to allow registered users to build pages and add controls that are already defined by the same u

I have a problem in asp.net

I'm developing a website in visual studio 2008 using (asp.net/vb)

I have to allow registered users to build pages and add controls that are already defined by the same user.

My problem is how to build this page dynamically?

In other words, I'd like to make like any CMS (e.g. Joomla). CMSs allow us to ma开发者_如何学Goke new pages and add our controls then save them...

I think alot and I find a solution by making a code-generator (from A->Z) that generates the page and its code-behind (apsx file & aspx.vb file)

Is there any tool or solution can help me better than my solution??

Any help is appreciated.


If you look at the designer cs file for an aspx page you will notice that the designer is adding objects to the controls for the page at runtime. If you want, you could the compiler services at run time to generate a class for the page, or you could create a function that creates an instance of the control by it's type name and then add that object to the page.

Type.GetType("myControl").GetConstructor(new Type[] { typeof(string) }).Invoke(new object[] { "myString" });

Which will should return the created control, that you can then hookup in the same way that the designer does it.

EDIT: Or you could look-up compiler services. I once made a program that took it's xml based config file, and turned it into a compilable .cs file using string builder. Then I used compiler services to compile that text into a class which I executed. It started life like this:

using System;
public class MyConfigClass
{
        {0}
        {1}
        {2}
    public MyConfigClass()
    {
    }
}

and ended up like this

using System;
public class MyConfigClass
{
    public string FirstName{
        get;
        set;
    }
    public string LastName{
        get;
        set;
    }
    public string Age {
        get;
        set;
    }

    public MyConfigClass()
    {

    }
}

based on this information

<?xml version="1.0" encoding="utf-8"?>
<ConfigProperties>
  <Property>
    <Name>FirstName</Name>
    <Value>John</Value>
  </Property>
  <Property>
    <Name>LastName</Name>
    <Value>Smith</Value>
  </Property>
  <Property>
    <Name>Age</Name>
    <Value>12</Value>
  </Property>
</ConfigProperties>

Basically the string builder took: "public {0} {1}{get;set;}" for each property then built the final string based on the values of the xml. Compiler services then turned it into a class. Once the class was made it could be stored for future use. Another attempt I did was to run the code file out disk in a web site project, then called the site to restart (which effectively recompiled all the that IIS finds in the directory, including your new code files).

The possibilities :)

0

精彩评论

暂无评论...
验证码 换一张
取 消