文本框数据绑定

时间:2011-07-07 22:40:56

标签: asp.net data-binding webforms

当使用WebForms进行处理时,您往往会在页面上出现大量文本框,这意味着您也倾向于使用来回的数据绑定代码:

tbMyTextBox.Text = MyViewObjectDataClass.MyStringProperty;

MyViewObjectDataClass.MyStringProperty = tbMyTextBox.Text;

这可能会非常重复......

我可以使用AutoMapper自动化我的视图对象和我的域对象之间的映射,这引出了我的问题......

有没有办法让ASP.Net表单元素自动查看对象?没有诉诸上述几十行?

是的,我可以切换到MVC,但这不是许多正在进行的项目的选项。

4 个答案:

答案 0 :(得分:5)

ASPX文件中的这种方法如何:

<asp:TextBox ID="TextBox1" runat="server" Text='<%#MyViewObjectDataClass.Name %>'></asp:TextBox>

但我们仍然需要添加语句:

  protected void Page_Load(object sender, EventArgs e)
  {
         if (!Page.IsPostBack)
         {
          this.DataBind();
         }
  }

另一种方法使用这样的自定义控件:

            public class NullPanel : Panel
            {
                private bool _autoBind = true;

                public override void RenderBeginTag(System.Web.UI.HtmlTextWriter writer)
                {
                    //base.RenderBeginTag(writer);
                }

                public override void RenderEndTag(System.Web.UI.HtmlTextWriter writer)
                {
                    //base.RenderEndTag(writer);
                }

                protected override System.Web.UI.HtmlTextWriterTag TagKey
                {
                    get
                    {
                        return System.Web.UI.HtmlTextWriterTag.Unknown;
                    }
                }

                [DefaultValue(true)]
                public bool AutoBind
                {
                    get
                    {
                        return _autoBind;
                    }
                    set
                    {
                        _autoBind = value;
                    }
                }

                protected override void OnLoad(EventArgs e)
                {
                    base.OnLoad(e);
                    //Set Visible property as true that intent to rise OnPreRender method
                    this.Visible = true;
                }

                protected override void DataBindChildren()
                {
                    if (this.Visible && this.AutoBind)
                    {
                        base.DataBindChildren();
                    }
                }

                protected override void OnPreRender(EventArgs e)
                {
                    if (this.AutoBind)
                    {
                        this.DataBind();
                    }
                    base.OnPreRender(e);
                }
            }

然后在ASPX文件中使用它:

 <asp:NullPanel ID="panelFenInfo" runat="server" 
         Visible='<%#this.IsEditMode%>' >
   <asp:TextBox ID="tbName" runat="server" Text='<%#this.MyViewObjectDataClass.Name %>'></asp:TextBox>
 <asp:NullPanel>
 <asp:NullPanel ID="panelInfo" runat="server" 
         Visible='<%#!Page.IsPostBack%>' >
   <asp:TextBox ID="tbCompany" runat="server" Text='<%#this.MyViewObjectDataClass.Name %>'></asp:TextBox>
 <asp:NullPanel>

答案 1 :(得分:0)

您可能想查看Rick Strahl关于供应双向绑定的这篇文章 http://msdn.microsoft.com/en-gb/magazine/cc163505.aspx

答案 2 :(得分:0)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Lib
{
public class MVAchiever<T>
{
    private T model;
    public T PullData(string ServerClass, Control cr)
    {
        model = (T)Activator.CreateInstance(typeof(T));
        if (cr.HasControls())
        {
            loopControl(cr, ServerClass);
        }
        return model;
    }
    private void loopControl(Control cr, string ServerClass)
    {
        foreach (Control c in cr.Controls)
        {
            if (c is WebControl)
            {
                WebControl wc = (WebControl)c;
                getThisDataFrom(wc, ServerClass);
            }
            if (c.HasControls())
            {
                loopControl(c, ServerClass);
            }
        }
    }
    private void getThisDataFrom(WebControl wc, string ServerClass)
    {
        string className = wc.Attributes["ServerClass"];
        string propName = wc.Attributes["ServerProperty"];
        string val = "";
        if (className == ServerClass && !string.IsNullOrEmpty(propName))
        {
            if (wc is DropDownList)
            {
                val = ((DropDownList)wc).SelectedValue;
            }
            else if (wc is TextBox)
            {
                val = ((TextBox)wc).Text;
            }                
            PropertyInfo pi = typeof(T).GetProperty(propName);
            Type pt = pi.PropertyType;
            if (!string.IsNullOrEmpty(val) && TypeDescriptor.GetConverter(pt).IsValid(val))
            {
                pi.SetValue(model, Convert.ChangeType(val, pt));
            }
        }
    }
}
}

<强>用法

ASP:

<asp:TextBox ID="TextBox1" runat="server" ServerClass="Model" ServerProperty="PropertyName"></asp:TextBox>

C#:

Model m = new MVAchiever<Model>().PullData("Model", Panel);

您可以添加任意数量的WebControl,我坚持使用上面的两个。

答案 3 :(得分:0)

直接数据绑定.aspx文件中的文本框:

<asp:repeater id="rptRem" runat="server" datasourceid="ObjectDataSource_ValidTicket">
     <itemTemplate>
           <asp:TextBox ID="TextBox_v_rem" runat="server" MaxLength="650" Width="100%" Text='<%# Eval("v_rem") %>' />
     </itemTemplate>
</asp:repeater>