简体   繁体   中英

Databind fires after ItemUpdated event

I'm using an asp.net 4 web site project (C#) in VS2008 and I have a FormView with ItemUpdated event:

<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;
}

But I can't figure out, why an extra "FormView1.DataBind()" or Render(?) happens AFTER the ItemUpdated event. The result is that my code in the ItemUpdated event gets like "overwritten" and the TextBox1 doesn't get disabled.

When I set a breakpoint to the last line "box.Enabled = false;" then I see that after the ItemUpdated event it jumps to the aspx page again and steps through the TextBoxes.

Disabling this TextBox1 from another GridView1_SelectedIndexChanged works fine.

Is there any way to see the "current lifecycle progress" in debugging?

EDIT:

To clarify the reasoning... I have a GridView1 where selecting the item populates the abovementioned FormView1. The point is that I need to disable some of the TextBoxes in the FormView1 based on, for example, user access levels. Selecting the item from GridView1 disables the TextBox1 just fine, but when I click the Update button on the FormView1 then all TextBoxes are enabled, even if I see in the debugger code running through the GridView1_SelectedIndexChanged() function. And after I re-select the gridview item, the correct TextBoxes are disabled again. Using even this code:

<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);
}

Maybe I should disable my Textboxes via another event?

This doesn't make sense, please explain more about why you are trying to disable the TextBox, have you just left the ItemTemplate off in your question? or is it actually missing? if it's missing why?

The TextBox is in the FormView's EditItemTemplate, so will only be visible when the FormView is in edit mode. After clicking update or cancel the TextBox will no longer be rendered, and the ItemTemplate is rendered instead. So there should be no need to set the TextBox to be disabled.

EDIT

Ok since you edited your question. You need to use the OnDataBound event of the FormView, which occurs at the end of binding, and disable your TextBoxes at that point.

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时触发。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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