0

I have 2 Repeaters one inside another.

<div id="result">
    <asp:Repeater runat="server" id="results">
        <Itemtemplate>
            <asp:Button ID="Button1" runat="server" Text="Add Color"></asp:Button>
            <asp:Repeater runat="server">
                <Itemtemplate>
                    <tr class="gradeX odd">
                        <asp:Button ID="Button2" runat="server" Text="Add Size"></asp:Button>
                        <td><asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></td>
                        <td><asp:TextBox ID="TextBox2" runat="server"></asp:TextBox></td>
                    </tr>
                </Itemtemplate>
                </asp:Repeater>
        </Itemtemplate>
    </asp:Repeater>
</div>

And on PageLoad I am populating both with DataBinding to List and List. The Button1 and Button2 is responsible for adding rows to each Repeater when clicked. The code is adding the rows to both perfectly but I noticed when I click the Buttons the data from the textbox isn't posted to the server so I collect from code behind. Is there something wrong I am doing?

2 Answers 2

1

Make sure you re-bind your data on page postback as well. Page control tree needs to be recreated before you can access the posted values.

If you re-create and add the control in page_load, it would allow you to access the post back value, but any view state changes may get lost/over written.

If you can, then recreating the control tree in page_init results in cleanest solution as it would allow the dynamic controls view state to restored properly.

Read this excellent article on Dynamic Web Controls, Postbacks, and View State

Adding Controls at the Right Time

We already know that when adding controls dynamically through the page's code portion the controls must be added on every postback. But when in the page lifecycle should the controls be added? At first guess, we might decide to put such code in the Page_Load event handler, causing the controls to be added during the Load stage of the page's lifecycle. This would work fine if we don't need to worry about saving the controls' view state across postbacks, but if we do need to persist the view state of the dynamically added controls the Load stage is not where we should be adding these controls.

See following for The ASP.NET Page Life Cycle

Sign up to request clarification or add additional context in comments.

7 Comments

No I am just populating when it's not Postback on page load.
The problem is, buttons should send data to server.
@Umair Ashraf: Control needs to be created and part of the page's control tree only then LoadPostData() is called. If you somehow add the control to page control tree (rebinding), you should be able to access the post back data.
But you know what, the first Repeaters child controls do send data to server on post back. It's only nested repeater which can not send the data. The first repeater is bound on Page_Load and as soon as it gets populated the second Repeater is bound on Item_Bound event of first Repeater.
|
0

In order for the server to read in the posted data, all ASP server controls have to exist right after Page_Init. Controls that do not exist at that point are not "deserialized", aka given values that were sent by the browser. Controls you create in Page_Load only arrive after the deserialization party.

Does it work if you move the DataBind from Page_Load to Page_Init?

3 Comments

not right after Page_Init, but before end of Page_Load. Also, just existing is not enough, they should be part of page control tree as well. If @Umair binds the control (create) it in page_load every time, it would work.
@YetAnotherUser: Well, if I follow the link you posted to the life cycle page, it says that LoadPostbackData is called before Page_Load. I'm pretty sure that's correct, as I've had to explain this to most new developers on our team :)
it's different for Dynamic controls. Best place to add them is page_init, but if they are added before end of page_load, it would still work. Its rather simple to test :). Mean while I'll try to find a link that explains it better. Also, it may impact the view state; control loosing properties changed in code behind from previous states but not user entered values.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.