The design is a little more complex. Completing the Ajax reading function involves many pages. Even so, it feels more flexible.
The only difference from the traditional method is that there is an additional layer of Data container control, which provides HTML elements for displaying data and corresponding JS methods.
In this way, the data control refers to generating pure data.
Ajax asynchronous reading
With jquery. Ajax, request the background to process the ashx page through Ajax post and pass relevant parameters.
Ashx
After the user control is dynamically loaded, the properties of the control are assigned a value based on the received parameters.
After loading the control, the link cannot be found by means of a blog post by Lao Zhao from the blog garden. Then, add the link later.
public class ViewManager<T> where T : System.Web.UI.UserControl { private System.Web.UI.Page m_pageHolder; public T LoadViewControl(string path) { this.m_pageHolder = new System.Web.UI.Page(); return (T)this.m_pageHolder.LoadControl(path); } public string RenderView(T control) { StringWriter output = new StringWriter(); this.m_pageHolder.Controls.Add(control); HttpContext.Current.Server.Execute(this.m_pageHolder, output, false); return output.ToString(); } }
View code
The code is very small and practical.
Reflection assignment
foreach (System.Reflection.PropertyInfo p in control.GetType().GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance)) { if (string.IsNullOrEmpty(context.Request[p.Name])) continue; try { Convert.ChangeType(context.Request[p.Name], p.PropertyType); p.SetValue(control, Convert.ChangeType(context.Request[p.Name], p.PropertyType), null); } catch (System.InvalidCastException e) { } }
View code
Usage
ViewManager<Web.controls.PageControl> viewManager = new ViewManager<Web.controls.PageControl>();Web.controls.PageControl control = viewManager.LoadViewControl("~/upload/controls/" + name); foreach (System.Reflection.PropertyInfo p in control.GetType().GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance)) { if (string.IsNullOrEmpty(context.Request[p.Name])) continue; try { Convert.ChangeType(context.Request[p.Name], p.PropertyType); p.SetValue(control, Convert.ChangeType(context.Request[p.Name], p.PropertyType), null); } catch (System.InvalidCastException e) { } } context.Response.Write(viewManager.RenderView(control));
View code
Data Control
Use ASP: repeater to display data.