【问题标题】:How to 2-way bind a textbox in a template fields programmatically如何以编程方式在模板字段中双向绑定文本框
【发布时间】:2010-02-06 16:19:38
【问题描述】:

我有一个网格视图,我正在以编程方式向其中添加模板字段。每个模板字段都有一个文本框。我想让这个文本框有 2 向绑定到数据库列。请看下面的代码。

public class CustomEditItemTemplate : ITemplate
{
private DataControlRowType templateType;
private string columnName;

public CustomEditItemTemplate(DataControlRowType type, string colname)
{
    this.templateType = type;
    this.columnName = colname;
}

public void InstantiateIn(System.Web.UI.Control container)
{
TextBox tb = new TextBox();
tb.ID = columnName;
tb.DataBinding += new EventHandler(this.tb_DataBinding);
container.Controls.Add(tb);
}

private void tb_DataBinding(Object sender, EventArgs e)
{
    TextBox t = (TextBox)sender;
    DetailsView dv = (DetailsView)t.NamingContainer;

    //This line does only one way binding. It takes the rows from the database and displays
    //them in the textboxes. The other way binding is not done. This is why my code fails
    t.Text = DataBinder.Eval(dv.DataItem, columnName).ToString();        
}

}

我是这样调用上面的类的

tf = new TemplateField();
tf.HeaderText = "My First Names";
tf.EditItemTemplate = new CustomEditItemTemplate(DataControlRowType.DataRow, "firstName");
dvModify.Fields.Add(tf);

如何使文本框在我编辑文本时也反映在数据库中?

感谢大家的宝贵时间

【问题讨论】:

    标签: c# asp.net templatefield


    【解决方案1】:

    也许您可以将t.Text = DataBinder.Eval(dv.DataItem, columnName).ToString(); 行更改为t.Text= string.Format("<%# Bind(\"{0}\") %>", columnName); 之类的东西?

    这只是猜测...

    如果这不起作用,我发现一些文章实际上编写了用于处理双向数据绑定的新类:

    Article at CodeProject circa 2005

    Article at Programmer's Heaven

    希望这些选项之一会有用。

    【讨论】:

      【解决方案2】:

      当然,在您看过一次之后,以编程方式处理模板字段实际上并不算太糟糕。以下是我们的做法,简略:

      TemplateField tf = new TemplateField();
      //Do some config like headertext, style, etc;
      
      tf.ItemTemplate = new CompiledTemplateBuilder(delegate(Control container)
      {
         //Add the regular row here, probably use a Label or Literal to show content
         Label label = new Label();
         lable.DataBinding += delegate(object sender, EventArgs e)
         {
              label.Text = ((MyDataType)DataBinder.GetDataItem(label.BindingContainer)).MyLabelDataField;
         };
      });
      
      tf.EditItemTemplate = new CompiledBindableTemplateBuilder(delegate(Control container)
      {
         //Here we do the edit row.  A CompiledBindableTemplateBuilder lets us bind in both directions
         TextBox text = new TextBox();
         text.DataBound += delegate(object sender, EventArgs e)
         {
             text.Text = ((MyDataType)DataBinder.GetDataItem(text.BindingContainer)).MyLabelDataField;
         }
      },
      delegate(Control container)
      {
         OrderedDictionary dict = new OrderedDictionary();
         dict["myLabelDataColumn"] = ((TextBox)container.FindControl(text.ID)).Text;
         return dict;
      });
      

      所以这就是正在发生的事情。我们使用 CompiledBindableTemplateBuilder 和 CompiledTemplateBuilder 来实际构建模板字段的内容。使用委托只是一种简单的设置方法。

      回答您的问题的关键在于 CompiledBindableTemplateBuilder 的第二个参数,它是建立绑定的委托。编辑后,在提交期间,模板字段将调用 ExtractValuesFromCell 并恢复 IBindableTemplate,从中提取字典,然后从中提取值,将它们添加到自己的字典中,最终变成上传的字典数据。这就是您从 Binding 委托返回 OrderedDictionary 的原因。希望对您有所帮助!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-06-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-06-12
        • 1970-01-01
        相关资源
        最近更新 更多