繁体   English   中英

在ItemUpdated事件后触发数据绑定

[英]Databind fires after ItemUpdated event

我在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中的某些TextBox。 从GridView1选择项目可以很好地禁用TextBox1,但是当我单击FormView1上的Update按钮时,所有的TextBox都被启用,即使我在调试器代码中看到通过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);
}

也许我应该通过其他事件禁用我的文本框?

这没有任何意义,请详细说明为什么您要尝试禁用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;
    }
}

而不是使用gridview选择的Index更改,您可以在formview上使用DataBound事件,因此您的逻辑将在每次重新绑定formview时触发。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM