【问题标题】:Databind fires after ItemUpdated event数据绑定在 ItemUpdated 事件后触发
【发布时间】:2012-10-02 09:38:04
【问题描述】:

我在 VS2008 中使用了一个 asp.net 4 网站项目 (C#),并且我有一个带有 ItemUpdated 事件的 FormView:

<asp:FormView ID="FormView1" runat="server" DataSourceID="ds1" OnItemUpdated="FormView1_ItemUpdated">
    <EditItemTemplate>
        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("col1") %>' />
    </EditItemTemplate>
</asp:FormView>

protected void FormView1_ItemUpdated(object sender, EventArgs e)
{
    FormView1.DataBind();  // adding this line even doesn't help
    TextBox box = FormView1.FindControl("TextBox1") as TextBox;
    box.Enabled = false;
}

但我不明白,为什么在 ItemUpdated 事件之后会发生额外的“FormView1.DataBind()”或 Render(?)。结果是我在 ItemUpdated 事件中的代码变得像“覆盖”并且 TextBox1 没有被禁用。

当我在最后一行设置断点时“box.Enabled = false;”然后我看到在 ItemUpdated 事件之后它再次跳转到 aspx 页面并逐步浏览 TextBoxes。

从另一个 GridView1_SelectedIndexChanged 禁用此 TextBox1 可以正常工作。

有没有办法在调试中查看“当前生命周期进度”?

编辑:

为了澄清推理...... 我有一个 GridView1,其中选择项目会填充上述 FormView1。关键是我需要禁用 FormView1 中的一些文本框,例如,用户访问级别。 从 GridView1 中选择项目可以很好地禁用 TextBox1,但是当我单击 FormView1 上的更新按钮时,所有 TextBoxes 都会启用,即使我在通过 GridView1_SelectedIndexChanged() 函数运行的调试器代码中看到。在我重新选择 gridview 项目后,正确的 TextBoxes 再次被禁用。 甚至使用此代码:

<asp:FormView ID="FormView1" runat="server" DataSourceID="ds1" DefaultMode="Edit" OnItemUpdated="FormView1_ItemUpdated">
    <EditItemTemplate>
        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("col1") %>' />
        <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("col2") %>' />
        <asp:Button ID="Btn1" runat="server" CommandName="Update" Text="Update" />
    </EditItemTemplate>
</asp:FormView>

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
    if (UserAccess() == false) {
        TextBox box2 = FormView1.FindControl("TextBox2") as TextBox;
        box2.Enabled = false;
    }
}
protected void FormView1_ItemUpdated(object sender, EventArgs e)
{
    GridView1_SelectedIndexChanged(sender, e);
}

也许我应该通过另一个事件禁用我的文本框?

【问题讨论】:

  • 挂钩 FormView 的 OnDataBound 事件。
  • 好点,它现在可以工作了。谢谢!

标签: c# asp.net


【解决方案1】:

这没有意义,请详细说明您为什么要禁用 TextBox,您是否刚刚在问题中关闭了 ItemTemplate?还是它实际上丢失了?如果它丢失了,为什么?

TextBox 在 FormView 的 EditItemTemplate 中,所以只有在 FormView 处于编辑模式时才可见。单击更新或取消后,将不再呈现 TextBox,而是呈现 ItemTemplate。所以应该没有必要将 TextBox 设置为禁用。

编辑

好的,因为您编辑了您的问题。您需要使用 FormView 的 OnDataBound 事件,该事件发生在绑定结束时,并在此时禁用您的 TextBoxes。

aspx

<asp:FormView ID="FormView1" runat="server" DataSourceID="ds1" 
    OnDataBound="FormView1_DataBound">
    <EditItemTemplate>
        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("col1") %>' />
    </EditItemTemplate>
</asp:FormView>

aspx.cs

protected void FormView1_DataBound(object sender, EventARgs e)
{
    if (UserAccess() == false) {
        TextBox box2 = FormView1.FindControl("TextBox2") as TextBox;
        box2.Enabled = false;
    }
}

【讨论】:

  • 我在项目中使用的代码比我发布的要多。 FormView 始终处于编辑模式,因为我将它与 Gridview 一起使用来编辑“选定”的 Gridview 项目。另外,我使用了更多的文本框,我想禁用一些文本框,条件是从创建 GridView 项目开始已经过去了 24 小时(用户只允许在 24 小时后更新一些文本框)。
【解决方案2】:

然后使用 gridview 选择索引更改,您可以在 formview 上使用 DataBound 事件,因此每次重新绑定 formview 时都会触发您的逻辑。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-11
    • 1970-01-01
    • 2014-06-24
    • 2013-07-31
    • 2013-07-02
    • 1970-01-01
    相关资源
    最近更新 更多