簡體   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