如何以编程方式在模板字段中双向绑定文本框
我有一个 gridview,我正在以编程方式向其中添加模板字段。每个模板字段都有一个文本框。我想让这个文本框以两种方式绑定到数据库列。请看下面的代码。
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);
如何制作文本框,以便在编辑文本时,此更改也会反映在数据库中?
感谢大家抽出时间
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许您可以将行
t.Text = DataBinder.Eval(dv.DataItem, columnName).ToString();
更改为类似t.Text= string.Format("<% # Bind(\"{0}\") %>", columnName);
?这只是一个猜测...
如果这不起作用,我发现一些文章实际上编写了用于处理双向数据绑定的新类:
2005 年左右 CodeProject 上的文章
程序员天堂的文章
希望这些选项之一会有用。
Perhaps you could changed the line
t.Text = DataBinder.Eval(dv.DataItem, columnName).ToString();
to something liket.Text= string.Format("<%# Bind(\"{0}\") %>", columnName);
?This is just a guess...
If that doesn't work, I found some articles that actually write new classes for handling two way databinding:
Article at CodeProject circa 2005
Article at Programmer's Heaven
Hopefully one of these options will be useful.
当然,在您看过一次之后,以编程方式执行模板字段实际上并不算太糟糕。以下是我们的做法(精简版):
所以这就是发生的事情。我们使用 CompiledBindableTemplateBuilder 和 CompiledTemplateBuilder 来实际构建模板字段的内容。使用委托只是一种简单的设置方法。
回答您的问题的关键在于 CompiledBindableTemplateBuilder 的第二个参数,它是建立绑定的委托。编辑后,在提交期间,模板字段将调用 ExtractValuesFromCell 并恢复 IBindableTemplate,从中提取字典,然后从中提取值,将它们添加到它自己的字典中,最终变成上传的字典数据。这就是为什么您从 Binding 委托返回 OrderedDictionary 的原因。希望有帮助!
It's actually not too bad to do template fields programatically, after you've seen it once, of course. Here's how we do it, abridged:
So here's what's going on. We use CompiledBindableTemplateBuilder and CompiledTemplateBuilder to actually build our template field's contents. Using a delegate is just an easy way to set this up.
The key to answering your question is in the second argument to the CompiledBindableTemplateBuilder, which is a delegate establishing the binding. After an edit, during submit, the template field will call ExtractValuesFromCell and recover an IBindableTemplate, from which it will extract a Dictionary and then pull the value(s) out of it, adding them to it's own dictionary which eventually gets turned into the uploaded data. So that's why you return an OrderedDictionary from the Binding delegate. Hope that helps!