Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use PreRender over PageLoad?

Related question: Get All Web Controls of a Specific Type on a Page

In the question above I asked how I can get all controls, works like a charm but something just doesn't quite fit so I thought it might be me. I have the following code but it's not manipulating the controls on the page but in my theory it should work.

List<DropDownList> allControls = new List<DropDownList>();
ControlEnhancer.GetControlList<DropDownList>(Page.Controls, allControls);

foreach (DropDownList childControl in allControls)
        {
            foreach (ListItem li in childControl.Items)
            {
                li.Attributes.Add("title", li.Text);
            }

            childControl.Attributes.Add("onmouseover", "this.title=this.options[this.selectedIndex].title");
        }

Thats the code, GetControlList() code you can get from the related question which shows how it gets all controls, its just my manipulation. I am trying to get all dropdownlist listitems and add a title to them so I can have a tooltip.

It's a quick fix for IE8 and below which cuts of long text in drop down boxes.

like image 439
Anicho Avatar asked Sep 12 '11 09:09

Anicho


People also ask

What is the use of PreRender?

Use this event to perform any updates before the server control is rendered to the page. Any changes in the view state of the server control can be saved during this event. Such changes made in the rendering phase will not be saved.

Which of the following is the correct difference between Page_Load and Page_PreRender?

Page_PreRender is the last event you have a chance to handle prior to the page's state being rendered into HTML. Page_Load is the more typical event to work with. Save this answer.

Which of the following is the correct difference between page load and page PreRender events in asp net?

Load - It is raised when the page or a control is loaded. PreRender - It is raised when the page or the control is to be rendered. Unload - It is raised when the page or control is unloaded from memory.

What is PreRender in asp net?

PreRender is the event that takes place before the HTML for a given page is generated and sent to the browser.


2 Answers

Page_Load happens often too soon; Page_PreRender is the last moment before the page's HTML is actually rendered for the browser and in many cases is the best place to set attributes on user controls.

This because during the web form (page) life cycle there are other events in the page (and in the user controls contained in the page...) which sometimes remove/replace/overwrite (really) those attributes so the only way you can get those attributes to the browser is to append them after all other life cycle events have been fired and handled, in the Page_PreRender.

like image 129
Davide Piras Avatar answered Nov 06 '22 21:11

Davide Piras


Actually, even PreRender might be too early in some cases (e.g. you could have DropDownList controls added to the control tree during databinding of controls that use DataSourceID).

There are two further events that might be more appropriate:

  • PreRenderComplete. At this point, all controls are created and the page is ready to render.

  • SaveStateComplete. Occurs after view state and control state have been saved. Any changes you make here won't be persisted to view state.

In your example (adding client-side attributes), I'd use the SaveStateComplete event to avoid unnecessary view state bloat.

like image 41
Joe Avatar answered Nov 06 '22 21:11

Joe