Asp.net self-made template framework and asp.net template framework
Recently, I am working on a website that contains a large number of repeated controls.
If MVC is used, I can write a local view for each Model that requires independent block editing.
Then we can work together based on different logic needs. It is very easy to use a ready-made MVC framework.
However, the environment is 3.5, and MVC2 can only be used. There is no razor syntax, And the development environment is not installed.
So I wanted to write a template to generate an HTML framework. At first, I gave me some interesting ideas.
However, later I thought that the functions were too weak in comparison with MVC, but the degree of freedom was very high, so I was not limited in thinking about any function.
There are not many main codes.
I use json data to save the view configuration, deserialize the instance, and then generate the final Html according to the manually defined view configuration + pre-defined html template code->.
It is divided into several types:
Page: page
Panel: panel
Control: control.
Lazy did not write various control classes, and all attributes are put in this class. Currently, the function is not very good. If there are more extensions, it will be messy.
The Html template code is similar to this: replace data with {attribute name.
<div class="item"><span class="iname"><i>{require}</i>{label}</span>
<input id="{name}" name="{name}" placeholder="{placeholder}" text="{label}"
onblur="" maxlength="{maxlength}" type="text"></div>
Json data is similar to this: (A tool was developed to automatically generate basic json data and generate default json data)
{"Label": "title", "name": "Basic", "controls": [{"name": "id_type", "label": "Document Type ", "type": "combox", "column": "BusinessIDCardInfo", "valid": "checkNull"}, "panels": []}
At last, on the aspx page, call and combine each page. supports multiple page jumps. check2 is triggered by the jump button. Each Page object has a jump function. In this function, the built-in verification function checkfunc (also one for each page) is executed first, and then
You can execute the custom verification function. The final call of next2 () is also the built-in jump function of the Framework. Based on the current page number, decide whether to redirect or submit.
<Asp: content ID = "Content1" ContentPlaceHolderID = "content" Runat = "Server"> <input type = "hidden" name = "pbtype" value = "p"/> <% = WechatPrefill. manager. getHtml ("pBasic", 1, 2, "New title") %> <% = WechatPrefill. manager. getHtml ("pEbank", 2, 2) %> <script>
Function check2 (){
Function check2 (){
If (! Checkfunc2 () return;
Next2 ();
}
</Script>
In fact, there are few functional classes. I write less code.
Paste the main function code:
Replace the specified text in the template by name based on the property of the reflected obj.
In addition to the string and int types, the Dictionary type must be specially processed.
All template replacements are completed in this function.
public static string GetHtml(string tempHtml,object obj) { var t = obj.GetType(); foreach (var field in t.GetFields()) { var value=field.GetValue(obj); if (value == null) { tempHtml = tempHtml.Replace("{" + field.Name + "}", ""); continue; } .....}
This is a Panel class. First, replace the title text, obtain the template data of the panel, and use reflection to replace the template.
public class Panel : Itemplate { public string name; public string label; public string img; public List<Control> controls; public string Html() { label = Manager.GetLabel(label); var temp = Template.GetTemplate("panel"); return Template.GetHtml(temp, this); } }
Finally, there is data generation.
Because reflection has been used to affect efficiency, we didn't want to use recursion, but the attribute level may be very deep (it may be. b. c. d. e. property). Therefore, if the loop is used completely, the code should be written for a long time.
Recursion is relatively simple.
Finally, I want to say that I regret doing so.
Because it is too troublesome, these functions are available in the MVC ready-made framework, and more powerful.
It is good to play games. You should consider it carefully when applying it to actual projects.