vb asp.net:避免在 ItemTemplate 和 EditItemTemplate 中重复公共代码的方法
我是 ASP.NET 的新手。
使用FormView时,ItemTemplate、EditItemTemplate和InsertItemTemplate中有大量代码,几乎完全相同。
例如:(
<asp:ListBox ID="ListBox2" runat="server" Rows="1" CssClass="field"
DataSourceID="StatusList" DataTextField="DESCRIPTION"
DataValueField="STAT_ID" SelectedValue='<%# Bind("STAT_ID") %>'>
</asp:ListBox>
注意:例外情况是在 ItemTemplate 中使用 Eval() 而不是 Bind())
我一直在尝试避免重复此代码,但没有达到预期结果:
ListView 允许使用 LayoutTemplate -但我没有看到任何在 LayoutTemplate 中插入此类代码的示例。在 LayoutTemplate 中插入此代码会导致错误。
DetailView 允许自动生成代码,但我想使用特定的设计(例如使用包含某些字段的“fieldset”)。
避免重复此类代码的最佳方法是什么?
I'm a newbie in asp.net.
When using FormView, there is a big amount of code in ItemTemplate, EditItemTemplate and InsertItemTemplate which is almost identical.
For example:
<asp:ListBox ID="ListBox2" runat="server" Rows="1" CssClass="field"
DataSourceID="StatusList" DataTextField="DESCRIPTION"
DataValueField="STAT_ID" SelectedValue='<%# Bind("STAT_ID") %>'>
</asp:ListBox>
(Note: at the exception that Eval() would be used instead of Bind() in ItemTemplate)
I've been trying to avoid repeating this code but without the expecting result:
ListView allows the use of LayoutTemplate - but I didn't see any examples that insert this kind of code in LayoutTemplate. And inserting this code in LayoutTemplate would result in an error.
DetailView allows to produce code automatically but I'd like to use a specific design (for ex. using "fieldset" that encompasses some fields).
What would be the best way to avoid repeating this kind of code ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不必对单独指定 Bind/Eval 部分进行太多选择,但您确实可以对其他部分进行一些控制。您可以创建一个包含您的布局的自定义用户控件。
通常,我在此用户控件上包含一个名为“模式”的属性,将其设置为“编辑”或“查看”,然后根据此属性更改控件上的启用/可见属性。您还需要为要在用户控件中绑定/显示的每个值包含一个属性。
在设计器中放置一些标签、文本框等...并将它们连接到后面代码中的属性,将用户控件放在项目/编辑模板中的页面上,并将数据评估/绑定到各种属性(确保设置模式,使其显示正确)。
You don't have to much choice about seperately specifying the Bind/Eval part, but you do have some control over the other pieces. You can make a custom UserControl that contains your layout.
Usually I include a property on this usercontrol called "Mode" which I either set to Edit or View, then based off of this property I change enabled/visible properties on the controls. You'll also need to include a property for each value you want bound/displayed in the usercontrol.
Put some labels, textboxes, etc... in your designer and hook them up to properties in your code behind, put the usercontrol on your page in your item/edit template and eval/bind to your data to the various properties (make sure to set the mode so it displays right).