views:

327

answers:

1

Here's my HTML

<asp:UpdatePanel runat="server" ID="panel1" UpdateMode="Conditional">
        <ContentTemplate>
            <asp:FormView runat="server" ID="formViewUno" DataSourceID="odsBob" DefaultMode="Insert">
                                    <InsertItemTemplate>
                    <span>Name:</span>
                    <asp:Literal ID="Literal4" runat="server" Text="&nbsp;&nbsp;&nbsp;" />
                    <asp:TextBox runat="server" ID="tbxName" Text='<%# Bind("Name") %>' />
                    <br />
                    <span>Age:</span>
                    <asp:Literal ID="Literal5" runat="server" Text="&nbsp;&nbsp;&nbsp;" />
                    <asp:TextBox runat="server" ID="tbxAge" Text='<%# Bind("Age") %>' />
                    <br />
                    <span>City:</span>
                    <asp:Literal ID="Literal6" runat="server" Text="&nbsp;&nbsp;&nbsp;" />
                    <asp:TextBox runat="server" ID="tbxCity" Text='<%# Bind("City") %>' />
                    <br />
                    <asp:Button ID="Button1" runat="server" CommandName="Insert" Text="Insert" />
                </InsertItemTemplate>
            </asp:FormView>
              <asp:Panel runat="server" ID="msgs">

            </asp:Panel>
        </ContentTemplate>
    </asp:UpdatePanel>

Here's my C#

 private void odsBob_Inserted(object sender, ObjectDataSourceStatusEventArgs e)
    {

            var p = e.ReturnValue as Person;
            if (p != null)
            {
                var msg = new Label
                              {
                                  Text =
                                      String.Format("{0} [Age:{1}, City:{2}] was successfully added", p.Name, p.Age,
                                                    p.City)
                              };

                var br = new LiteralControl { Text = "<br/>" };
                msgs.Controls.Add(br);
                msgs.Controls.Add(msg);


            }

    }

How can I persist (add a new one after the insert) the label controls? It is being wiped out. The new one added is added each time correctly. How can I keep the control collection in tact? Thanks for any help.

Cheers, ~ck

+1  A: 

It looks like you're dynamically creating a label object during the event handler.

Dynamic controls are problematic because they need to be recreated on every postback. Remember that a postback creates a new instance of your Page object - which means that the controls you added to your last page are gone - your Panel is initialized as empty with each new request, so only the latest literal/label pair will be added.

One solution may be to add all the necessary textual information to Session, and have your Panel generate dynamic labels and literals from whatever is in Session during Prerender.

Another solution would be more complex, but you could have the Panel add labels and literals dynamically during the Init phase. If you can ensure that the same number of controls is added in the same order during Init, then the ViewState for those controls will be properly tracked on each PostBack. You would basically need to store the most recently added label and literal into Session, and have the Panel fetch it out on the next request to ensure it got added back in during Init. You'd also need to store a counter so that the Panel knew how many sets of controls to add during Init.

womp
I am using an objectDatasouce. I am not calling databind explicitly anywhere. Should I be? Should I only bind if !IsPostback? I thought databind was called implicitly when you use a DataSourceId. Please point in the direction to do this correctly. Thanks. :)
Hcabnettek
Man talk about blind. I misread your code. I'll update my answer for you.
womp