In FormView, how to change the label property in ItemTemplate?

In ASP.NET, I have a FormView bound to an ObjectDataSource. FormView has an ItemTemplate with a Delete button and a label. I am handling the OnItemDeleted FormView event to determine if my business class throws an exception on deletion. If an exception is detected, I change the label text to everything that is in the exception message.

Well, it just doesn't work.

I detect an Exception penalty, but the label text never changes. When the page reloads, the default text remains. I also tried rebuilding FormView using DataBind () after assigning new text, but it also does not work.

In a desperate attempt to track down the problem, I took the shortcut out of FormView and it works great.

What am I 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 for:

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

!

EDIT: , FormView "", :

  • OnInit
  • OnItemCreated
  • OnLoad
  • OnItemCommand
  • OnItemDeleting
  • OnItemDeleted
  • OnItemCreated
  • OnDataBound
  • OnPreRender
  • OnUnload

, , OnItemCreated , OnItemDeleted, , , , , . , ?

+3
3

( ):

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

:

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

, - "", MyFormViewItemDeleted FormView.

, , .

0

Here's how I did it by modifying the code a bit. In the code behind the field in the class is declared: private string str_feedbackmsg;

try
    {
        cnn.Open();
        SqlCommand cmd = new SqlCommand(dbcmd, cnn);
        cmd.ExecuteNonQuery();

        str_feedbackmsg = "Database Work Done!";
    }
    catch (SqlException err)
    {

        str_feedbackmsg = "Database Error: Please Notify the Site Administrators.";
    }
    finally
    {
        cnn.Close();
    }

Then in the Databound event for FormView:

protected void gv_main_DataBound(object sender, EventArgs e)
{
    if (IsPostBack)
    {
        Label l = fv_main.FindControl("lbl_feedback") as Label;
        l.Text = str_feedbackmsg;
    }
}
0
source

Source: https://habr.com/ru/post/1779282/


All Articles