如何初始化 InsertItemTemplate 中的控件?
例如,我有一个 asp:FormView,它支持读取、插入、更新、删除并绑定到数据源:
<asp:FormView ID="FormView1" runat="server"
DataSourceID="ObjectDataSource1" >
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("MyText") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("MyText") %>' />
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("MyText") %>' />
</InsertItemTemplate>
</asp:FormView>
如果我处于读取模式或编辑模式,则控件将使用属性 MyText。
但是当我进入插入模式时,我没有“当前对象”(FormView1.DataItem
确实是null
)并且控件是空的。
如果我想用特定值初始化我的 TextBox 控件,我该怎么做?在哪种情况下我可以挂钩来为 InsertItemTemplate 中的控件设置默认值?
特别是我想到使用 ObjectDataSource
。我期望 InsertItemTemplate 使用一个业务对象进行初始化,该业务对象是我的 ObjectDataSource 的基础,并且是由 ASP.NET 框架在 InsertItemTemplate 激活时简单地使用其默认构造函数创建的。在默认构造函数中,我会将类成员初始化为我希望在 InsertItemTemplate 控件中具有的默认值。但不幸的是,情况并非如此:没有创建“默认”对象并将其绑定到 FormView。所以看来我必须单独初始化所有控件或手动创建默认对象并将其绑定到 FormView 的 InsertItemTemplate。但我如何以及在哪里可以做到这一点呢?
提前致谢!
I have - for instance - an asp:FormView which supports Read, Insert, Update, Delete and is bound to a DataSource:
<asp:FormView ID="FormView1" runat="server"
DataSourceID="ObjectDataSource1" >
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("MyText") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("MyText") %>' />
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("MyText") %>' />
</InsertItemTemplate>
</asp:FormView>
If I am in Read-Mode or Edit-Mode the control is initialized with the property MyText
of the current object which is bound to the FormView.
But when I go to Insert-Mode I do not have a "current object" (FormView1.DataItem
is indeed null
) and the controls are empty.
If I want to have my TextBox control initialized with a specific value how can I do that? In which event can I hook in to set default values to the controls in the InsertItemTemplate?
Especially I have in mind using an ObjectDataSource
. I was expecting that the InsertItemTemplate is initialized with a business object which underlies my ObjectDataSource and which is created by the ASP.NET framework simply by using its default constructor when the InsertItemTemplate gets activated. In the default constructor I would init the class members to the default values I'd like to have in my controls of the InsertItemTemplate. But unfortunately that's not the case: No "default" object is created and bound to the FormView. So it seems I have to initialize all controls separately or to create the default object manually and bind it to the InsertItemTemplate of the FormView. But how and where can I do that?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试 ModeChanged 事件。
其中您可以使用
If .FindControl 确实返回 null,请尝试这样做:
Try the ModeChanged event.
Therein you can use
If .FindControl does return null, try to this: