控件模型绑定控件

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

/// <summary>
///ModelBind 的摘要说明
/// </summary>
public class ModelBind
{
    public ModelBind()
    {
        //
        //TODO: 在此处添加构造函数逻辑
        //
    }
    public static bool flagDbug = false;//标识是否启动调试
    #region 获取控件值
    public static string getcValue(Control page, string str)
    {
        string strResponse = "模型属性:" + str + "/";
        string returnStr = "";
        if (page.FindControl(str) != null)
        {
            string controlsType = page.FindControl(str).GetType().Name.Trim();
            strResponse = strResponse + "控件类型:" + controlsType;
            if (controlsType == "TextBox")
            {
                TextBox obj = (TextBox)page.FindControl(str);
                returnStr = obj.Text;
            }
            else if (controlsType == "HiddenField")
            {
                HiddenField obj = (HiddenField)page.FindControl(str);
                returnStr = obj.Value;
            }
            else if (controlsType == "Literal")
            {
                Literal obj = (Literal)page.FindControl(str);
                returnStr = obj.Text.Trim();
            }
            else if (controlsType == "DropDownList")
            {
                DropDownList obj = (DropDownList)page.FindControl(str);
                returnStr = obj.SelectedValue;
            }

            else if (controlsType == "RadioButtonList")
            {
                RadioButtonList obj = (RadioButtonList)page.FindControl(str);
                returnStr = obj.SelectedValue;
            }
            else if (controlsType == "CheckBoxList")
            {
                CheckBoxList obj = (CheckBoxList)page.FindControl(str);
                bool flag = true;
                for (int i = 0; i < obj.Items.Count; i++)
                {
                    if (flag)
                    {
                        returnStr = obj.Items[i].Value;
                    }
                    else {
                        returnStr = returnStr + "," + obj.Items[i].Value;
                    }
                }
            }
            else if (controlsType == "CheckBox")
            {
                CheckBox obj = (CheckBox)page.FindControl(str);
                if (obj.Checked == true)
                {
                    returnStr = obj.Text;    
                }
            }
            else if (controlsType == "RadioButton")
            {
                RadioButton obj = (RadioButton)page.FindControl(str);
                if (obj.Checked == true)
                {
                    returnStr = obj.Text;
                }
            }
            else
            {
                returnStr = "no";
            }
            strResponse = strResponse + ".value=" + returnStr;
        }
        else
        {
            returnStr = "no";
            strResponse = "控件类型:no  值 no";
        }
        ModelBind.theWrite(strResponse);
        return returnStr;
    }
    #endregion

    #region 设置控件值
    public static string setcValue(Control page, string str, string value)
    {
        string returnStr = "";
        if (page.FindControl(str) != null)
        {
            string controlsType = page.FindControl(str).GetType().Name.Trim();
            returnStr = "<br>属性:" + controlsType + " --controlsId:" + str + "---value:" + value;
            if (controlsType == "TextBox")
            {
                TextBox obj = (TextBox)page.FindControl(str);
                obj.Text = value;
                returnStr = returnStr + " --true/";
            }
            else if (controlsType == "HiddenField")
            {
                HiddenField obj = (HiddenField)page.FindControl(str);
                obj.Value = value;
                returnStr = returnStr + " --true/";
            }
            else if (controlsType == "DropDownList")
            {
                DropDownList obj = (DropDownList)page.FindControl(str);
                obj.SelectedValue = value;
                returnStr = returnStr + " --true/";
            }
            else if (controlsType == "RadioButtonList")
            {
                RadioButtonList obj = (RadioButtonList)page.FindControl(str);
                obj.SelectedValue = value;
                returnStr = returnStr + " --true/";
            }
            else if (controlsType == "CheckBoxList")
            {
                CheckBoxList obj = (CheckBoxList)page.FindControl(str);
                string[] cblList = value.Split(',');
                for (int i = 0; i < obj.Items.Count; i++)
                {
                    for (int j = 0; j < cblList.Length; j++)
                    {
                        if (obj.Items[i].Value.Trim() == cblList[j].Trim())
                        {
                            obj.Items[i].Selected = true;
                        }
                    }
                }
                returnStr = returnStr + " --true/";
            }
            else if (controlsType == "CheckBox")
            {
                CheckBox obj = (CheckBox)page.FindControl(str);
                if (obj.Text == value)
                {
                    obj.Checked = true;
                }
                returnStr = returnStr + " --true/";
            }
            else if (controlsType == "RadioButton")
            {
                RadioButton obj = (RadioButton)page.FindControl(str);
                if (obj.Text == value)
                {
                    obj.Checked = true;
                }
                returnStr = returnStr + " --true/";
            }
            else if (controlsType == "Literal")
            {
                Literal obj = (Literal)page.FindControl(str);
                obj.Text = value;
                returnStr = returnStr + " --true/";
            }
            else
            {
                returnStr = returnStr + "--  congId:" + str + " -- noControl/";
            }
        }
        else
        {
            returnStr = "<br>-" + str + "--noFind/";
        }
        
        return returnStr;
    }
    public static string setcValue(Control page, string str)
    {
        string returnStr = "";
        if (page.FindControl(str) != null)
        {
            string controlsType = page.FindControl(str).GetType().Name.Trim();
            returnStr = "<br>属性:" + controlsType + " --controlsId:" + str;
            if (controlsType == "TextBox")
            {
                TextBox obj = (TextBox)page.FindControl(str);
                obj.Text = "";
                returnStr = returnStr + " --true/";
            }
            else if (controlsType == "HiddenField")
            {
                HiddenField obj = (HiddenField)page.FindControl(str);
                obj.Value = "";
                returnStr = returnStr + " --true/";
            }
            else if (controlsType == "DropDownList")
            {
                DropDownList obj = (DropDownList)page.FindControl(str);
                obj.SelectedIndex = 0;
                returnStr = returnStr + " --true/";
            }
            else if (controlsType == "RadioButtonList")
            {
                RadioButtonList obj = (RadioButtonList)page.FindControl(str);
                obj.SelectedIndex = 0;
                returnStr = returnStr + " --true/";
            }
            else if (controlsType == "CheckBoxList")
            {
                CheckBoxList obj = (CheckBoxList)page.FindControl(str);
                for (int i = 0; i < obj.Items.Count; i++)
                {
                    obj.Items[i].Selected = false;
                }
                returnStr = returnStr + " --true/";
            }
            else if (controlsType == "CheckBox")
            {
                CheckBox obj = (CheckBox)page.FindControl(str);
                obj.Checked = false;
                returnStr = returnStr + " --true/";
            }
            else if (controlsType == "RadioButton")
            {
                RadioButton obj = (RadioButton)page.FindControl(str);
                obj.Checked = false;
                returnStr = returnStr + " --true/";
            }
            else if (controlsType == "Literal")
            {
                Literal obj = (Literal)page.FindControl(str);
                obj.Text = "";
                returnStr = returnStr + " --true/";
            }
            else
            {
                returnStr = returnStr + "--  congId:" + str + " -- noControl/";
            }
        }
        else
        {
            returnStr = "--" + str + "--noFind/";
        }
        ModelBind.theWrite(returnStr);
        return returnStr;
    }
    #endregion

    #region 把控件的值传入给模型
    public static void setModel(Control page, Object obj)
    {
        Type type = obj.GetType();
        foreach (System.Reflection.PropertyInfo info in type.GetProperties())
        {
            if (ModelBind.getcValue(page, info.Name) != "no")
            {
                info.SetValue(obj, Convert.ChangeType(ModelBind.getcValue(page, info.Name), info.PropertyType), null);
            }
        }
    }
    public static void setModel(Object obj)
    {
        Type type = obj.GetType();
        foreach (System.Reflection.PropertyInfo info in type.GetProperties())
        {
            if (System.Web.HttpContext.Current.Request[info.Name] != null)
            {
                string value = System.Web.HttpContext.Current.Request[info.Name];
                if (!string.IsNullOrEmpty(value))
                {
                    info.SetValue(obj, Convert.ChangeType(value, info.PropertyType), null);
                }
                info.SetValue(obj, Convert.ChangeType(value, info.PropertyType), null);
            }
        }
    }
    #endregion

    #region 把模型的值传给控件
    public static string setControls(Control page, Object obj)
    {
        string returnStr = "";
        Type type = obj.GetType();
        foreach (System.Reflection.PropertyInfo info in type.GetProperties())
        {
            if (info.GetValue(obj, null) != null)
            {
                returnStr = returnStr + ModelBind.setcValue(page, info.Name, info.GetValue(obj, null).ToString().Trim());
            }
        }
        ModelBind.theWrite(returnStr);
        return returnStr;
    }
    public static string setControlsNull(Control page, Object obj)
    {
        string returnStr = "";
        Type type = obj.GetType();
        foreach (System.Reflection.PropertyInfo info in type.GetProperties())
        {
            returnStr = returnStr + ModelBind.setcValue(page, info.Name);
        }
        return returnStr;    
    }
    #endregion

    #region 公共函数
    public static void foreachModel(object obj)
    {
        Type type = obj.GetType();
        foreach (System.Reflection.PropertyInfo info in type.GetProperties())
        {
            System.Web.HttpContext.Current.Response.Write("<br>name:" + info.Name + "--value:--" + info.GetValue(obj, null));
        }
    }
    public static void theWrite(string str)
    {
        if (ModelBind.flagDbug)
        {
            System.Web.HttpContext.Current.Response.Write("<br>" + str);
        }
    }
    public static void foreachRequest()
    {
        for (int i = 0; i < System.Web.HttpContext.Current.Request.Form.Count; i++)
        {
            ControlsGetval.rWrite(System.Web.HttpContext.Current.Request.Form.Keys[i].ToString());
            ControlsGetval.rWrite(System.Web.HttpContext.Current.Request.Form[i].ToString());
        }
    }
    #endregion
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值