DetailsView 中动态添加的 TemplateField 未设置绑定属性
我需要使用 DetailsView 构建一个网页,其中包含一堆文本框和绑定到 ObjectContainerDataSource 的下拉列表。这是使用 InsertItemTemplate 的单向绑定。
我可以使用静态定义的字段轻松地在文本框和下拉列表中使用此功能,但是我的特殊情况需要在运行时添加字段,因为网页需要高度可配置。
对于文本框,通过 BoundField 类很容易做到这一点,但是当我尝试使用 TemplateField 作为下拉列表时,一切都会出现严重错误。我尝试了许多不同的方法,包括编写自己的 TemplateField 类,以及直接使用 CompiledBindableTemplateBuilder 设置 InsertItemTemplate。但无论我做什么,基本问题是单击插入按钮后,TemplateFields 不会绑定到对象数据源。我相信这是因为 InsertItemTemplate 在回发时为 null(请注意,当在标记中声明所有字段时,InsertItemTemplate 不为 null)。如果有人已经完成了我想做的事情,请回复,因为我现在对此已经束手无策了。
我已恢复到模板字段内的简单文本框以进行概念验证,但它也不起作用。我已将我所做的粘贴在下面。在本例中,我尝试将 TextBox 输入绑定到名为“CustomerRef”的属性。我认为我的问题可能与我如何设置 Text 属性有关,但我无法找到任何其他方法在后面的代码中将 Text 属性设置为 Bind(...) 。
这就是我插入模板字段的方式:
var tf = new TemplateField();
tf.InsertItemTemplate = new CompiledBindableTemplateBuilder(delegate(Control container)
{
TextBox text = new TextBox();
text.ID = "MyTextBoxId";
text.Attributes.Add("Text", "<%# Bind(\"CustomerRef\") %>");
container.Controls.Add(text);
},
delegate(Control container)
{ // I've tried throwing an exception in here but it never gets executed
OrderedDictionary dict = new OrderedDictionary();
dict["CustomerRef"] = ((TextBox)container.Controls[0]).Text;
return dict;
});
这就是我将字段添加到“详细信息视图字段”列表中的方式:
ShipmentSearchView.Fields.Add(tf);
我在文本框中放入一些文本并按插入按钮...然后在我的插入事件的事件处理程序中尝试获取这样的数据源:
var dataSource = e.Instance as MyClassName;
此时在调试环境中,我注意到相关字段不再具有 InsertItemTemplate - 它为 null,并且未设置 MyCustomerRef。
我尝试在 Page_Load() 中的各个位置添加字段 - 无论是在每次回发时还是仅在第一次加载时;在 Page_Init() 以及详细信息视图的 Init() 事件中,但我的操作方式似乎从来没有什么区别。预先感谢您对此问题的任何帮助。
I need to build a web page with a DetailsView which has a bunch of text boxes and drop down lists which bind to an ObjectContainerDataSource. This is for one-way binding using the InsertItemTemplate.
I can easily get this working for both text boxes and drop down lists using statically-defined fields, however my particular situation requires the fields to be added at runtime as the web page needs to be highly configurable.
For textboxes, this is easy via the BoundField class, however it all goes horribly wrong when I try to use the TemplateField for a Drop Down List. I have tried many different methods, including writing my own TemplateField class, and also setting the InsertItemTemplate directly using CompiledBindableTemplateBuilder. But no matter what I do, the basic issue is after the insert button is clicked, TemplateFields do not bind to the object data source. I believe this is because the InsertItemTemplate is null on postback (note that when all fields are declared in mark-up, InsertItemTemplate is not null). If anyone has done exactly what I'm trying to do, please reply, because I am now at the end of my tether with this.
I've reverted back to the simple TextBox inside a template field for proof of concept, but it will not work either. I've pasted what I've done below. In this case I'm trying to bind the TextBox input to a property called "CustomerRef". I think my issue may relate to how I'm setting the Text property, but I have not been able to find any other way to set the Text property to Bind(...) in code behind.
This is how I insert my templated field:
var tf = new TemplateField();
tf.InsertItemTemplate = new CompiledBindableTemplateBuilder(delegate(Control container)
{
TextBox text = new TextBox();
text.ID = "MyTextBoxId";
text.Attributes.Add("Text", "<%# Bind(\"CustomerRef\") %>");
container.Controls.Add(text);
},
delegate(Control container)
{ // I've tried throwing an exception in here but it never gets executed
OrderedDictionary dict = new OrderedDictionary();
dict["CustomerRef"] = ((TextBox)container.Controls[0]).Text;
return dict;
});
And this is how I add the field to my DetailsView Fields list:
ShipmentSearchView.Fields.Add(tf);
I put some text in the textbox and press the insert button... then in my Event Handler for the insert event I try to get the data source like this:
var dataSource = e.Instance as MyClassName;
At this point in debug environment I notice that the field in question no longer has the InsertItemTemplate - it is null, and MyCustomerRef is not set.
I have tried adding the fields in various places, in Page_Load() - both on every post back and only on first load; in Page_Init() and also in the Init() event of the Details View, but it never seems to make a difference how I do it. Thanks in advance for any help with this issue.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我最终编写了自己的自定义用户控件来实现这一目标 - 这确实并不难做到。如果有人对我的解决方案感兴趣,我很乐意将其发布在这里。但是由于缺乏针对我原始帖子的评论,我猜测我在网页上所做的事情并不是很多人都在尝试的事情!
I ended up writing my own custom user control to achieve this - it really isn't that hard to do. If anyone is interested in my solution I'd be happy to post it here. But going by the lack of comments against my original post, I'm guessing that what I'm doing on my web page is not something many people are attempting!