Accessing controls created dynamically (c#)

后端 未结 3 1540
刺人心
刺人心 2020-12-01 20:10

In my code behind (c#) I dynamically created some RadioButtonLists with more RadioButtons in each of them. I put all controls to a specific Panel. What I need to know is how

3条回答
  •  既然无缘
    2020-12-01 20:52

    I don't think creating controls in the PageLoad is the right away of doing, first the asp.net life cycle goes from Initialization;Load ViewState Data;Load PostData; Object Load etc.

    if you create controls at the Page_Load you'll lose the ViewState, events etc.

    The right away is doing at PageInit, or if is a control (OnInit).

    The next difficult is that at PageInit, you don't have the ViewState Available, if you need to reconstruct the number of objects you need to store some context/info in a hidden field ant then retrieve that information at PageInit, Create the objects and voila!

    Example:

    imagine that you need to create 1..N TextBoxes, you create html hidden field (not with runat=server) e.g. NumberOfTextBoxes.

    When you are executing PageInit Code: you retrieve the value e.g. numberOfTextBoxes = Request.Form["NumberOfTextBoxes"], then you create the TextBoxes.

    Remember the most important thing is to match the number and the order of existent Controls stored the ViewState.

提交回复
热议问题