Design of the Ajax asynchronous data structure used in the project

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.