Use Webfrom to simulate some of the MVC model and view data interaction capabilities

Source: Internet
Author: User

One of the most blinding features of MVC is the ability to directly bind page and data models based on the model and view views, so we don't need to assign a variety of controls when we want the client to send the page, so we don't have to worry about how to render it. When the customer submits the form to the server, it does not need to do too much processing to directly call the form to model.

So we can achieve similar functions in Webfrom, the answer is yes.

The first step: Build the model. That is, the form is looked at as an object, creating related properties.

namespace models{    using System;    Using System.Collections.Generic;        public partial class Cst_cust_info    {public        int cust_id {get; set;}        public string Cust_no {get; set;}        public string Cust_full_name {get; set;}        public string Cust_master_org_no {get; set;}        public string Contract_type {get; set;}        public string Cust_short_name {get; set;}        public string Cust_mail {get; set;}}     }

The second step: Create a page corresponding to it (here to understand a concept is that the form, regardless of how the change, the final submission to the server after the server for the form is only recognized name, do not know the other. If the form is repeated, he doesn't care about the simple combination, and the client knows that the control is identified by the runat= "Server" property and ID, so the name and ID two things must be handled well, of course, usually in the case of runat= ' server, The ID is assigned to the name attribute directly when the system renders.

<form id= "FORMCC" runat= "Server" >                                     <asp:dropdownlist id= "txt_card_type_id"  runat= "Server" >                         </asp:DropDownList>                                    <asp:dropdownlist id= "txt_person_sex" runat= "Server" width= "94px" >                         </asp:DropDownList>                                     <asp:textbox id= "txt_brith_date" runat= "Server" ></asp:TextBox>                                    <asp:textbox id= "txt_certificate_date" runat= "Server" ></asp:TextBox>                                   <asp:textbox id= "Txt_ Person_no "runat=" Server "   ></asp:TextBox> </from> .....

  

Step Three: Create a method to render the model directly to the foreground (this is just a simple assignment, which is even more wonderful if you pass render)

Ps:1, where the ID name is easily conflicting in the project, replace the name of the reflection interaction with the name=> "Txt_" +name mode for mapping

2, the need to understand the types of control commonly used in the project, generally mostly is WebControl and HtmlInputControl, wherein WebControl refers to inherit WebControl this type of control, generally have textbox,label and so on, The HtmlInputControl control is some <input runat= "Server" > control, the difference is that the rendering mechanism is different, but finally

       <summary>///handling one-to-one information//</summary>//<param name= "Custpage" ></par        am>//<param name= "Lablename" ></param>//<param name= "Cstmodel" ></param> private static void Fillmaincust<t> (Custpage custpage, dictionary<string, string> lablename, T CSTModel, I Nt?                sort = null) {if (Cstmodel! = null) {foreach (string key in Lablename.keys) {PropertyInfo pinfo = Cstmodel.gettype ().                    GetProperty (key);                    Control txtname = custpage.findcontrol<control> ("Txt_" + key); if (pinfo! = NULL && txtname! = null) {Object pinvalue = (pinfo. GetValue (Cstmodel)??                        "");                        String pinstr = ""; if (Pinvalue is DateTime | |                        Pinvalue is DateTime?)                       {     Pinstr = ((DateTime) pinvalue).                        ToString ("Yyyy-mm-dd"); } else {if (Pinvalue is decimal | |                            Pinvalue is decimal?) {pinstr = ((Decimal) pinvalue).                            ToString ("0.00"); } else {pinstr = pinvalue.tostring ()                            ;                        }} #region is assigned a value by type if (Txtname is TextBox) {(TextBox) txtname).                        Text = Pinstr; } else if (Txtname is Label) {(label) txtname). T                        ext = PINSTR; } else if (Txtname is HiddenField) {(hiddenfieLD) (txtname).                        Value = Pinstr;                        } #endregion #region Special handling if (Txtname is Label)                            {if (CustDictory.Special_Dictionary.Keys.Contains (key)) {(Label) txtname).                            Text = P2ptable.getspecialvalue (Pinstr, Custdictory.special_dictionary[key]); }} #endregion #region processing DropDownList                                                        Assignment if (txtname is DropDownList) { if (((DropDownList) txtname). Items.findbyvalue (PINSTR)! = null) {((DropDownList) txtname).                            SelectedValue = Pinstr;                          } else {      if (((DropDownList) txtname). Items.findbytext (PINSTR)! = null) {((DropDownList) Txtna Me). Items.findbytext (PINSTR).                                Selected = true; }}} #endregion #region                            The Hidden Control assignment if (Txtname is System.Web.UI.HtmlControls.HtmlInputHidden) { ((System.Web.UI.HtmlControls.HtmlInputHidden) txtname).                        Value = Pinstr; } #endregion}}}

Fourth step: When submitting the from Form fill into the Model object ps:1, at this time do not need to consider the control problem only need to consider the data submitted from the form, here from the form submission we are already in front of the rendering of the time to prepare

2, the code in order to save performance, so do not traverse the object properties, directly set up a dictionary to save the value to be taken, if you do not need to consider these can be directly evaluated

   foreach (string key in Lablename.keys)            {               String value = custpage.request.form["Txt_" + key];                PropertyInfo pinfo = Custinfo.gettype (). GetProperty (key);                if (value! = NULL && pinfo! = null)                {                    Object newvlue = value. Stringtoobject (pinfo);                    pinfo. SetValue (CustInfo, Newvlue);                }            }

Good ~ a simple reflection mechanism the MV with the From form feature has been completed

What we need to know about this example is: 1. The from form is submitted according to name.

2, server controls are ultimately rendered to the client, some controls are not processed by the server

3, simple some reflection use

4, to correctly understand the server and the client to do things

Use Webfrom to simulate some of the MVC model and view data interaction capabilities

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.