Note that there are some explanatory texts on larger screens.

plurals
  1. POIn a FormView, how to change the property of a Label in the ItemTemplate?
    primarykey
    data
    text
    <p>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.</p> <p>Well, it's just not working.</p> <p>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.</p> <p>In a desperate attempt at tracking the problem, I've taken the Label out of the FormView and it's working fine.</p> <p>What I am doing wrong?</p> <p>ASPX page:</p> <pre><code>&lt;asp:ObjectDataSource ID="MyObjectDataSource" TypeName="MyScopeRepository" SelectMethod="GetById" DeleteMethod="Delete" runat="server"&gt; &lt;SelectParameters&gt; &lt;%-- The id param here is from a DropDownList, not included in the example for clarity. --%&gt; &lt;asp:ControlParameter Name="id" Type="Int32" ControlID="MyDropDownList" PropertyName="SelectedValue" /&gt; &lt;/SelectParameters&gt; &lt;DeleteParameters&gt; &lt;asp:Parameter Name="id" Type="Int32" /&gt; &lt;/DeleteParameters&gt; &lt;/asp:ObjectDataSource&gt; &lt;asp:FormView ID="MyFormView" DataSourceID="MyObjectDataSource" RenderOuterTable="false" DataKeyNames="Id" OnItemDeleted="MyFormViewItemDeleted" runat="server"&gt; &lt;ItemTemplate&gt; &lt;asp:Button CssClass="Button Small" Text="Delete" CommandName="Delete" runat="server" /&gt;&lt;br /&gt; &lt;asp:Label ID="ErrorLabel" Text="Default text" runat="server" /&gt; &lt;/ItemTemplate&gt; &lt;/asp:FormView&gt; </code></pre> <p>Code-behind:</p> <pre><code>protected void MyFormViewItemDeleted(object sender, FormViewDeletedEventArgs e) { if (e.Exception != null &amp;&amp; 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(); } } </code></pre> <p>Thanks a lot!</p> <p>EDIT: I've checked all events fired by the FormView when clicking the Delete button and here is what I got:</p> <ol> <li>OnInit</li> <li>OnItemCreated</li> <li>OnLoad</li> <li>OnItemCommand</li> <li>OnItemDeleting</li> <li>OnItemDeleted</li> <li>OnItemCreated</li> <li>OnDataBound</li> <li>OnPreRender</li> <li>OnUnload</li> </ol> <p>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?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload