示例#1
0
 private void setControlValue(Control control, String value)
 {
     if (control is ITextControl)
     {
         ITextControl txt = (ITextControl)control;
         txt.Text = value == null ? "" : value;
     }
     else if (control is ICheckBoxControl)
     {
         ICheckBoxControl chk = (ICheckBoxControl)control;
         chk.Checked = value != null;
     }
     else if (control is ListControl)
     {
         ListControl ddl = (ListControl)control;
         if (value == null)
         {
             ddl.SelectedIndex = -1;
         }
         else
         {
             ddl.SelectedValue = value;
         }
     }
 }
示例#2
0
 private int ConvertBoleanToInt(ICheckBoxControl objCheck)
 {
     return(objCheck.Checked ? 1 : 0);
 }
示例#3
0
    /// <summary>
    /// 以控件ID为Key取出参数值,使用默认值为:
    /// Boolean:false, String:string.emty
    /// </summary>
    /// <param name="ctrl"></param>
    public void SetValue(Control control)
    {
        bool cannotHandle = false;

        if (control != null)
        {
            string name  = control.ID;
            string value = Pop(name);

            if (control is HtmlControl) //HtmlControls
            {
                if (control is HtmlInputControl)
                {
                    if (control is HtmlInputCheckBox)
                    {
                        ((HtmlInputCheckBox)control).Checked = Magic.Framework.Utils.Cast.Bool(value);
                    }
                    else if (control is HtmlInputRadioButton)
                    {
                        ((HtmlInputCheckBox)control).Checked = Magic.Framework.Utils.Cast.Bool(value);
                    }
                    else if (control is HtmlInputImage)
                    {
                        ((HtmlInputImage)control).Src = value;
                    }
                    else
                    {
                        ((HtmlInputControl)control).Value = value;
                    }
                }
                else if (control is HtmlContainerControl)
                {
                    if (control is HtmlSelect)
                    {
                        ((HtmlSelect)control).Value = value;
                    }
                    else if (control is HtmlTextArea)
                    {
                        ((HtmlTextArea)control).Value = value;
                    }
                    else
                    {
                        cannotHandle = true;
                    }
                }
                else if (control is HtmlImage)
                {
                    ((HtmlImage)control).Src = value;
                }
                else if (control is HtmlTitle)
                {
                    ((HtmlTitle)control).Text = value;
                }
                else
                {
                    cannotHandle = true;
                }
            }
            else if (control is WebControl) // WebControls
            {
                if (control is ListControl)
                {
                    ListControl list      = (ListControl)control;
                    string[]    selValues = GetMultiValue(name);
                    if (selValues != null && selValues.Length > 0)
                    {
                        WebUtil.SetSelectedValues(list, selValues);
                    }
                }
                else if (control is ITextControl)
                {
                    ITextControl txt = (ITextControl)control;
                    txt.Text = value;
                }
                else if (control is ICheckBoxControl)
                {
                    ICheckBoxControl chk = (ICheckBoxControl)control;
                    chk.Checked = Cast.Bool(value);
                }
                else
                {
                    cannotHandle = true;
                }
            }
            else
            {
                cannotHandle = true;
            }
        }
        if (cannotHandle)
        {
            throw new Exception(string.Format("It hasn't implemented the value getter for control {0}, id is {1};\n\t you would better get the value by youself and then add the value use Push()! ", control.GetType().FullName, control.ID));
        }
    }
示例#4
0
    /// <summary>
    /// 将控件对象中的值取出来,以控件ID作为key,push到参数中
    /// </summary>
    /// <param name="control"></param>
    public void GetValue(Control control)
    {
        bool cannotHandle = false;

        if (control != null)
        {
            string key   = control.ID;
            string value = string.Empty;
            if (control is HtmlControl) //HtmlControls
            {
                if (control is HtmlInputControl)
                {
                    if (control is HtmlInputCheckBox)
                    {
                        value = ((HtmlInputCheckBox)control).Checked.ToString();
                    }
                    else if (control is HtmlInputRadioButton)
                    {
                        value = ((HtmlInputRadioButton)control).Checked.ToString();
                    }
                    else if (control is HtmlInputImage)
                    {
                        value = ((HtmlInputImage)control).Src;
                    }
                    else
                    {
                        value = ((HtmlInputControl)control).Value;
                    }
                }
                else if (control is HtmlContainerControl)
                {
                    if (control is HtmlSelect)
                    {
                        value = ((HtmlSelect)control).Value;
                    }
                    else if (control is HtmlTextArea)
                    {
                        value = ((HtmlTextArea)control).Value;
                    }
                    else
                    {
                        cannotHandle = true;
                    }
                }
                else if (control is HtmlImage)
                {
                    value = ((HtmlImage)control).Src;
                }
                else if (control is HtmlTitle)
                {
                    value = ((HtmlTitle)control).Text;
                }
                else
                {
                    cannotHandle = true;
                }
            }
            else if (control is WebControl) // WebControls
            {
                if (control is ListControl)
                {
                    ListControl    list      = (ListControl)control;
                    IList <string> selValues = WebUtil.GetSelectedValues(list);

                    value = FormatMultiValue(selValues);
                }
                else if (control is ITextControl)
                {
                    ITextControl txt = (ITextControl)control;
                    value = txt.Text;
                }
                else if (control is ICheckBoxControl)
                {
                    ICheckBoxControl chk = (ICheckBoxControl)control;
                    value = chk.Checked.ToString();
                }
                else
                {
                    cannotHandle = true;
                }
            }
            else
            {
                cannotHandle = true;
            }

            if (cannotHandle)
            {
                throw new Exception(string.Format("It hasn't implemented the value getter for control {0}, id is {1};\n\t you would better get the value by youself and then add the value use Push()! ", control.GetType().FullName, control.ID));
            }
            Push(key, value);
        }
    }
示例#5
0
        public static void GetModel(object viewObj, Control parentControl = null)
        {
            if (parentControl == null)
            {
                parentControl = Current;
            }
            var list = GetPostPair(viewObj, parentControl);

            if (list.Count == 0)
            {
                throw new InvalidOperationException("PostPair's empty!");
            }

            Page page = parentControl.Page;

            foreach (PostPair pair in list)
            {
                Control control = page.FindControl(pair.UniqueID);
                if (control == null)
                {
                    throw new InvalidOperationException(string.Format("GetPost:'{0}' isn't exists. Remark:如果此ID是OutputCache用户控件,那么在回发的时候可能是Null。", pair.UniqueID));
                }

                WebControl webControl = control as WebControl;
                if (webControl != null && !webControl.Enabled)
                {
                    continue;
                }
                PropertyAccess property = pair.Property;
                Type           propType = property.EntityProperty.PropertyType;
                if (!TypeHelper.IsStringOrValueType(propType))
                {
                    continue;
                }

                object value = null;
                Type   underlyingType;
                if (TypeHelper.IsNullableType(propType, out underlyingType))
                {
                    propType = underlyingType;
                }
                CheckBoxList listControl;
                if (propType.IsEnum &&
                    (listControl = control as CheckBoxList) != null && Attribute.IsDefined(propType, typeof(FlagsAttribute)))
                {
                    int flag = 0;
                    foreach (ListItem item in listControl.Items)
                    {
                        if (item.Selected)
                        {
                            int itemValue = int.Parse(item.Value);
                            flag |= itemValue;
                        }
                    }
                    value = flag;
                }
                else
                {
                    ITextControl textControl = control as ITextControl;
                    if (textControl != null)
                    {
                        value = textControl.Text;
                    }
                    else
                    {
                        ICheckBoxControl checkBoxControl = control as ICheckBoxControl;
                        if (checkBoxControl != null)
                        {
                            value = checkBoxControl.Checked;
                        }
                        else
                        {
                            HiddenField hiddenField = control as HiddenField;
                            if (hiddenField != null)
                            {
                                value = hiddenField.Value;
                            }
                        }
                    }
                }

                if (object.Equals(value, string.Empty))
                {
                    if (!property.IsNullable)
                    {
                        throw new InvalidCastException(property.MappedName);
                    }
                    property.SetValue(viewObj, null);
                }
                else
                {
                    if (property.EntityProperty.PropertyType == typeof(Guid))
                    {
                        property.SetValue(viewObj, new Guid(value.ToString()));
                    }
                    else
                    {
                        // property.ChangeType 处理了类型为Enum的情况
                        property.SetValue(viewObj, property.ChangeType(value));
                    }
                }
            }
        }
示例#6
0
        public static void SetModel(object viewObj, Control parentControl = null, bool asState = false)
        {
            if (parentControl == null)
            {
                parentControl = Current;
            }
            var list = GetPostPair(viewObj, parentControl);

            if (list.Count == 0)
            {
                throw new InvalidOperationException("PostPair's empty!");
            }

            Page page = parentControl.Page;

            foreach (PostPair pair in list)
            {
                Control control = page.FindControl(pair.UniqueID);
                if (control == null)
                {
                    throw new InvalidOperationException(string.Format("SetPost:'{0}' isn't exists", pair.UniqueID));
                }

                PropertyAccess property = pair.Property;
                object         value    = property.GetValue(viewObj);
                Type           propType = property.EntityProperty.PropertyType;
                if (!TypeHelper.IsStringOrValueType(propType))
                {
                    if (value != null)
                    {
                        if (value is System.Collections.IEnumerable)
                        {
                            BindData(control, value);
                        }
                        else
                        {
                            SetModel(value, parentControl);
                        }
                    }
                    continue;
                }

                Type underlyingType;
                if (TypeHelper.IsNullableType(propType, out underlyingType))
                {
                    propType = underlyingType;
                }
                if (propType.IsEnum)
                {
                    if (value == null)
                    {
                        continue;
                    }

                    value = Convert.ChangeType(value, typeof(int));

                    ListControl listControl = control as ListControl;
                    if (listControl != null)
                    {
                        if (listControl.Items.Count == 0)
                        {
                            BindEnum(listControl, propType);
                        }
                        if ((listControl is CheckBoxList) && Attribute.IsDefined(propType, typeof(FlagsAttribute)))
                        {
                            int flag = (int)value;
                            foreach (ListItem item in listControl.Items)
                            {
                                int itemValue = int.Parse(item.Value);
                                if ((flag & itemValue) == itemValue)
                                {
                                    item.Selected = true;
                                }
                            }
                            continue;
                        }
                    }
                }
                if (value == null)
                {
                    value = string.Empty;
                }
                ITextControl textControl = control as ITextControl;
                if (textControl != null)
                {
                    textControl.Text = value.ToString();
                }
                else
                {
                    ICheckBoxControl checkBoxControl = control as ICheckBoxControl;
                    if (checkBoxControl != null)
                    {
                        checkBoxControl.Checked = Convert.ToBoolean(value);
                    }
                    else
                    {
                        HiddenField hiddenField = control as HiddenField;
                        if (hiddenField != null)
                        {
                            hiddenField.Value = value.ToString();
                        }
                    }
                }
            }
            if (asState)
            {
                var dict = (ListDictionary)page.Session["ModelState"];
                if (dict == null)
                {
                    page.Session["ModelState"] = dict = new ListDictionary();
                }
                dict[viewObj.GetType()] = viewObj;
            }
        }