Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3491784
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T11:40:43+00:00 2026-05-18T11:40:43+00:00

In ASP.NET, I have a FormView which is bound to an ObjectDataSource. The FormView

  • 0

In ASP.NET, I have a FormView which is bound to an ObjectDataSource. The FormView has an ItemTemplate with a Delete button, and a Label. I’m handling the OnItemDeleted event of the FormView to detect if my business class throws an exception upon deletion. If an exception is detected, I change the text of the Label to whatever the exception message is.

Well, it’s just not working.

I detect the Exception fine, but the Text of the Label never gets changed. When the page reloads, the default text stays. I have also tried to rebind the FormView with DataBind() after assigning the new Text, but it’s not working either.

In a desperate attempt at tracking the problem, I’ve taken the Label out of the FormView and it’s working fine.

What I am doing wrong?

ASPX page:

<asp:ObjectDataSource ID="MyObjectDataSource" 
    TypeName="MyScopeRepository" 
    SelectMethod="GetById"
    DeleteMethod="Delete"
    runat="server">

    <SelectParameters>
        <%-- The id param here is from a DropDownList, not included in the example for clarity. --%>
        <asp:ControlParameter Name="id" Type="Int32" ControlID="MyDropDownList" PropertyName="SelectedValue" />
    </SelectParameters>
    <DeleteParameters>
        <asp:Parameter Name="id" Type="Int32" />
    </DeleteParameters>
</asp:ObjectDataSource>

<asp:FormView ID="MyFormView" DataSourceID="MyObjectDataSource"
    RenderOuterTable="false"
    DataKeyNames="Id"
    OnItemDeleted="MyFormViewItemDeleted"
    runat="server">

    <ItemTemplate>
        <asp:Button CssClass="Button Small" Text="Delete" CommandName="Delete" runat="server" /><br />
        <asp:Label ID="ErrorLabel" Text="Default text" runat="server" />
    </ItemTemplate>
</asp:FormView>

Code-behind:

protected void MyFormViewItemDeleted(object sender, FormViewDeletedEventArgs e)
{
    if (e.Exception != null && e.Exception.InnerException is RepositoryException)
    {
        Label errorLabel = (Label)MyFormView.FindControl("ErrorLabel");
        errorLabel.Text = e.Exception.InnerException.Message;
        e.ExceptionHandled = true;

        // I also tried this to no avail.
        //MyFormView.DataBind();
    }
}

Thanks a lot!

EDIT: I’ve checked all events fired by the FormView when clicking the Delete button and here is what I got:

  1. OnInit
  2. OnItemCreated
  3. OnLoad
  4. OnItemCommand
  5. OnItemDeleting
  6. OnItemDeleted
  7. OnItemCreated
  8. OnDataBound
  9. OnPreRender
  10. OnUnload

So we can see that OnItemCreated gets fired twice, and the second time it’s fired is AFTER OnItemDeleted, which means that whatever change I make is overwritten, I suppose. Now how am I supposed to handle that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T11:40:44+00:00Added an answer on May 18, 2026 at 11:40 am

    Here is working solution (You may want to improve it):

    <asp:FormView ID="FormView1" runat="server" DataSourceID="ObjectDataSource1" 
        AllowPaging="true" OnItemDeleted="FormView1_ItemDeleted" ondatabound="FormView1_DataBound" >
        <ItemTemplate>
            Key:
            <asp:Label ID="KeyLabel" runat="server" Text='<%# Bind("Key") %>' />
            <br />
            Value:
            <asp:Label ID="ValueLabel" runat="server" Text='<%# Bind("Value") %>' />
            <br />
            <asp:Button ID="DeleteButton" runat="server" CausesValidation="False" CommandName="Delete"
                Text="Delete" />
            <hr />
            <asp:Label ID="Label1" runat="server" Text="does not work"></asp:Label>
        </ItemTemplate>
    </asp:FormView>
    <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="GetList"
        DeleteMethod="Delete" TypeName="MyProject.Repository">
        <DeleteParameters>
            <asp:Parameter Name="key" Type="Int32" />
        </DeleteParameters>
    </asp:ObjectDataSource>
    

    codebehind:

    public string MyProperty { get; set; }
            protected void FormView1_DataBound(object sender, EventArgs e)
            {
                if (!string.IsNullOrEmpty(MyProperty))
                {
                    Label l = FormView1.FindControl("Label1") as Label;
                    l.Text = "it works. " + MyProperty;
                    MyProperty = null;
                }
            }
    
            protected void FormView1_ItemDeleted(object sender, FormViewDeletedEventArgs e)
            {
                if (e.Exception != null)
                {
                    MyProperty = e.Exception.Message;
                    e.ExceptionHandled = true;
                }
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.