Dynamic controls based on user feedback

Suppose I have a treeview where each treenode contains an identifier for a different set of user controls. When the user clicks the node button, these controls should be loaded on the page. As I understand it, the life cycle of an ASP page, dynamic controls should be added at the initialization stage, and postback events will fire later.

So, if the treeview click event occurs after I need to add my controls, how can I dynamically add controls based on user postback events?


Edit: I tried a suggestion from ArronLS:

What I did was add the node value to the session array and use this when I do init to select which form elements are loaded into the controls of the records control. In the treeview click event, I update the node in the session array, clear the old form elements in the placeholder, and add new form elements to the controls. When the page loads again, it should now find the node during init, so browsing problems will work around.

Now I haven’t fully tested this yet, but there was another similar post that talked about problems that might arise in the viewstate. They offer a solution that polls part of the context request [] (in their case dropbox) in the Init control, manually processing some postback functions.

My new question is, how can I access the selected node in the tree structure using the Request array?

+4
source share
5 answers

The consequence of not loading controls in init is that when properties are changed in the view state, they will not be saved in the controls. For example, if at the first request of the page you dynamically create controls in init, and then in the response message you create them again in init, then after init all the property elements in the viewstate will be applied to the control.

So, if you first created the control in the click event of the TreeView, I would suggest that this should be good, because there is not yet any drive that will be applied to the control since it was just created. However, I'm not sure if this will cause the control to not maintain the viewstate status. You will have to experiment with this.

In subsequent postbacks after the first, now you will need to create a control in init for the copied viestate, which will be applied to it, so you need some mechanism to “remember” that you created the control once before, the response to the click event and then on subsequent postbacks again create the control in init. You need to recreate the control for each request if you did not know this.

So, the question is, how important is the viewtate to the control.

Edit: I will also add that I'm not quite sure if there will be other consequences, besides how this affects the viewstate.

0
source

This may not be the answer to your direct question, but since I never found the answer myself, here is a workaround that I used.

The approach that I have always used when working with TreeView is to declare controls on an aspx page once, and then in the click event, bind the controls to the data based on the identifier. If necessary, you can first set the visibility to visible = "false" and change it when binding. This approach works well because it only escapes the riddle you describe.

If you do not mind rejecting tree views, then the nested repeater works.

+1
source

Just throwing one more thought, hoping to get even more feedback ...

I could use the postback event to determine the selected value in the session array, and then force the page to redirect to itself. Then, the initialization that the user sees will be performed efficiently after the event handler starts.

It seems to be a bad idea, so I hope for something else.

0
source

If I understand correctly, you want different contents of the node to be displayed for each tree. I assume that theres a treeview on the left and some content area in the middle.

From a user interface perspective, I usually solve this using MultiView, where each individual view represents a separate user control with the required content. The treenode click event simply changes the MultiView ActiveIndex contained in the Node property property (the ID is stored in the DataItem), and you simply turn off the content area.

As a rule, even if tree nodes are dynamically generated, from the data, for example, there will always be only a finite number of Node View "user controls that need to be determined.

Note. Be careful when using the MultiView control, since all the Submitted images load during the life of the page, so do not put a "heavy lift" in Page_Load, etc.

0
source

This can help remember that the identifier of the selected node is passed as the form value, which is always available from the Request.Form collection, even during the Init event. The key will look like ctl00_Content1_TreeView1_SelectedNode . However, this identifier will probably not give you the value you need, so you would like to look at Request.Form["__EVENTARGUMENT"] and also use Request.Form["__EVENTTARGET"] to make sure that it is indeed the TreeView that called PostBack.

Most likely, the necessary information can be extracted from the Form collection. It is simply a matter of setting a breakpoint and exploring values. Such code always feels terribly hacked, but in this case you simply cannot wait for the TreeView control event to be processed when you need to use the values ​​presented in the form to do something during Page_Init. Just don’t be afraid to look at the values ​​of the form, and not wait until .NET packs everything well with strongly typed properties. By then it will be too late.

0
source

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


All Articles