如何初始化 InsertItemTemplate 中的控件?

发布于 2024-08-26 11:51:45 字数 1260 浏览 7 评论 0原文

例如,我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你如我软肋 2024-09-02 11:51:45

尝试 ModeChanged 事件。

其中您可以使用

TextBox _tb = this.FormView1.FindControl("TextBox1") as TextBox
if(_tb != null)
{
  _tb.Text = Mybusinessobject.property;
}

If .FindControl 确实返回 null,请尝试这样做:

this.FormView1.DataBind();
TextBox _tb = this.FormView1.FindControl("TextBox1") as TextBox
if(_tb != null)
{
  _tb.Text = Mybusinessobject.property;
}

Try the ModeChanged event.

Therein you can use

TextBox _tb = this.FormView1.FindControl("TextBox1") as TextBox
if(_tb != null)
{
  _tb.Text = Mybusinessobject.property;
}

If .FindControl does return null, try to this:

this.FormView1.DataBind();
TextBox _tb = this.FormView1.FindControl("TextBox1") as TextBox
if(_tb != null)
{
  _tb.Text = Mybusinessobject.property;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文