ASP.NET偷懶大法四(動態產生表單對象)

來源:互聯網
上載者:User

做表單提交時,經常的做法是拖TextBox等控制項到頁面,然後點提交按鈕,cs代碼把控制項的值一個一個的賦給一個對象,然後調用一個Save的方法 提交成功!煩瑣的操作,基本在每一個表單添加修改的時候做一遍。我們這些煩瑣的事情交給.net做吧。

從Panel繼承一個自訂控制項,當成一個區間,寫一個方法遍曆這個區間所有的控制項,自動給類賦值

首頁給這個控制項一個屬性

public string Entity
        {
            get
            {
                String s = (String)ViewState["Entity"];
                return ((s == null) ? String.Empty : s);
            }

            set
            {
                ViewState["Entity"] = value;
            }
        }

屬性的值就是這個表單所操作的實體類

然後寫個方法,反射實體類,變數區間中所有的控制項,賦值

public object GetEntity()
        {
            Type t = Type.GetType(Entity, true);
            object entity = Activator.CreateInstance(t);
            foreach (object var in this.Controls)
            {
                Type ControlType = var.GetType();
                switch (ControlType.Name)
                {
                    case "SMTextBox":
                        if (!string.IsNullOrEmpty((var as FrameworkWeb.Web.SMTextBox).ID))
                        {
                            System.Reflection.PropertyInfo propertyInfo = t.GetProperty((var as FrameworkWeb.Web.SMTextBox).ID);
                            if (propertyInfo != null)
                            {
                                string tempText = (var as FrameworkWeb.Web.SMTextBox).Text;
                                if (!string.IsNullOrEmpty(tempText))
                                    propertyInfo.SetValue(entity, Convert.ChangeType(tempText, propertyInfo.PropertyType), null);
                            }
                        }
                        break;
                    case "TextBox":
                        if (!string.IsNullOrEmpty((var as TextBox).ID))
                        {
                            System.Reflection.PropertyInfo propertyInfo = t.GetProperty((var as TextBox).ID);
                            if (propertyInfo != null)
                            {
                                string tempText = (var as TextBox).Text;
                                if (!string.IsNullOrEmpty(tempText))
                                    propertyInfo.SetValue(entity, Convert.ChangeType(tempText, propertyInfo.PropertyType), null);
                            }
                        }
                        break;
                    case "SMDTPicker":
                        if (!string.IsNullOrEmpty((var as SMDTPicker).ID))
                        {
                            System.Reflection.PropertyInfo propertyInfo = t.GetProperty((var as SMDTPicker).ID);
                            if (propertyInfo != null)
                            {
                                string tempText = (var as SMDTPicker).Text;
                                if (!string.IsNullOrEmpty(tempText))
                                    propertyInfo.SetValue(entity, Convert.ChangeType(tempText, propertyInfo.PropertyType), null);
                            }
                        }
                        break;
                    case "DropDownList":
                        if (!string.IsNullOrEmpty((var as DropDownList).ID))
                        {
                            System.Reflection.PropertyInfo propertyInfo = t.GetProperty((var as DropDownList).ID);
                            if (propertyInfo != null)
                            {
                                string tempText = (var as DropDownList).SelectedValue;
                                propertyInfo.SetValue(entity, Convert.ChangeType(tempText, propertyInfo.PropertyType), null);
                            }
                        }
                        break;

                    case "HiddenField":
                        if (!string.IsNullOrEmpty((var as HiddenField).ID))
                        {
                            System.Reflection.PropertyInfo propertyInfo = t.GetProperty((var as HiddenField).ID);
                            if (propertyInfo != null)
                            {
                                string tempText = (var as HiddenField).Value;
                                propertyInfo.SetValue(entity, Convert.ChangeType(tempText, propertyInfo.PropertyType), null);
                            }
                        }
                        break;
                }
            }

            return entity;
        }

前台調用的時候直接執行這個方法就可以得到這個對象了,如果有一些特殊的情況,再二次處理就ok啦。

然後就是修改啦,修改的時候需要先給控制項賦值,然後使用者修改後提交。這個也可以用類似的原來簡化操作。

給我們的控制項增加一個資料庫源的屬性

public virtual object DataSource
        {
            get
            {
                return this.dataSource;
            }

            set
            {
                this.dataSource = value;
            }
        }

增加一個資料繫結的時候的事件

public delegate void SMFormDataBindEventHandler(object source, SMFormEventArgs e);

[Category("Action"), Description("DataBind")]
public event SMFormDataBindEventHandler DataBindCommand;

///這裡存的是自動賦好值的一個對象,方便二次處理
public class SMFormEventArgs : EventArgs
    {
        public readonly object tempObject;
        public SMFormEventArgs(object tempObject)
        {
            this.tempObject = tempObject;
        }
    }

這是為了處理特殊的情況,有時候有些控制項可能不是直接取資料庫的值,就可以利用這個事件進行二次處理啦

然後給控制項賦值

protected new void DataBind()
        {
            PropertyInfo p;

            foreach (Control var in this.Controls)
            {
                switch (var.GetType().Name.ToUpper())
                {
                    case "TEXTBOX":
                        System.Web.UI.WebControls.TextBox textBox = var as System.Web.UI.WebControls.TextBox;
                        p = DataSource.GetType().GetProperty(var.ID);
                        if (p != null)
                            textBox.Text = p.GetValue(DataSource, null).ToString().Trim();
                        break;
                    case "SMTEXTBOX":
                        FrameworkWeb.Web.SMTextBox smtextBox = var as FrameworkWeb.Web.SMTextBox;
                        p = DataSource.GetType().GetProperty(var.ID);
                        if (p != null)
                            smtextBox.Text = p.GetValue(DataSource, null).ToString().Trim();
                        break;
                    case "DROPDOWNLIST":
                        System.Web.UI.WebControls.DropDownList ddl = var as System.Web.UI.WebControls.DropDownList;
                        p = DataSource.GetType().GetProperty(var.ID);
                        if (p != null)
                        {
                            object o = p.GetValue(DataSource, null);
                            if (o!=null)
                            ddl.SelectedValue = p.GetValue(DataSource, null).ToString().Trim();
                        }
                        break;
                    case "LITERAL":
                        System.Web.UI.WebControls.Literal lit = var as System.Web.UI.WebControls.Literal;
                        p = DataSource.GetType().GetProperty(lit.ID);
                        if (p != null)
                            lit.Text = p.GetValue(DataSource, null).ToString().Trim();
                        break;
                    case "HIDDENFIELD":
                        System.Web.UI.WebControls.HiddenField hf = var as System.Web.UI.WebControls.HiddenField;
                        p = DataSource.GetType().GetProperty(hf.ID);
                        if (p != null)
                            hf.Value = p.GetValue(DataSource, null).ToString().Trim();
                        break;
                    case "SMDTPICKER":
                        FrameworkWeb.Web.SMDTPicker smDTPicker = var as FrameworkWeb.Web.SMDTPicker;
                        p = DataSource.GetType().GetProperty(var.ID);
                        if (p != null)
                            smDTPicker.Text = p.GetValue(DataSource, null).ToString().Trim();
                        break;
                        break;
                }
            }
        }

這樣這個控制項就基本完成了,調用的時候基本不用寫什麼代碼啦

拖好控制項,在提交的事件中只需要寫

Model.BTS item = smform.GetEntity() as Model.BTS;
DAL.BTS.Save(item);

修改的時候

Model.BTS btsModel== DAL.BTS.GetModel("sCode");
smform.DataSource = btsModel;

然後在事件中做二次處理

protected void smform_DataBindCommand(object source, FrameworkWeb.Form.SMFormEventArgs e)
    {
        Model.BTS item = e.tempObject as Model.BTS;
        if (item != null)
        {
            DropDownList dp = smform.FindControl("isEnable") as DropDownList;
            dp.Items.FindByValue(item.isEnable.ToString()).Selected = true;
            if (item.dicSectorType>0)
                (smform.FindControl("dicSectorType") as DropDownList).Items.FindByValue(item.dicSectorType.ToString()).Selected = true;
            if (item.dicAreaType > 0)
            (smform.FindControl("dicAreaType") as DropDownList).Items.FindByValue(item.dicAreaType.ToString()).Selected = true;

            //if (item.fkDeviceID > 0)
            //    (smform.FindControl("fkDeviceID") as DropDownList).Items.FindByValue(item.fkDeviceID.ToString()).Selected = true;
        }
    }

然後儲存
Model.BTS item = smform.GetEntity() as Model.BTS;
DAL.BTS.Update(item);

一切就是如此easy

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.