Text field data binding

When using WebForms for material, you usually get a lot of text fields on the page, which means that you also get a feedback code:

tbMyTextBox.Text = MyViewObjectDataClass.MyStringProperty; 

and

 MyViewObjectDataClass.MyStringProperty = tbMyTextBox.Text; 

It can become quite repetitive ....

I can automate the mappings between the object of my object and the object of my domain using AutoMapper, which leads me to the question ...

Is there a way to make an ASP.Net form element to automatically view an object? Without resorting to dozens of the above lines?

Yes, I can just switch to MVC, but this is not an option for many ongoing projects.

+7
source share
3 answers

How about this approach in aspx files:

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

But we still need to add statements:

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

Another approach uses a custom control as follows:

  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); } } 

Then use it in ASPX files:

  <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> 
+4
source

You might want to check out this post by Rick Stral about the supply of a two-way binding http://msdn.microsoft.com/en-gb/magazine/cc163505.aspx

0
source
 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)); } } } } } 

Using

ASP:

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

FROM#:

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

You can add as many different WebControls as you want, I'm stuck with the two above.

0
source

All Articles