Question

I am creating an ASP.net application. I fill a ListBox control with items and I use the PreRender event of the Listbox to set its width according to the largest item in the control.

But after the ListBox is rendered, the first time I click an item in the ListBox the PreRender event fires and focus is removed from the ListBox (this event fires unnecessarily because the ListBox is already rendered). The second time I click on an item in ListBox the PreRender event doesnt fire and focus is maintained.

Is this behaviour normal? What could cause this behaviour?

The ListBox is filled on PageLoad if is not postback, with a default value. The Listbox is filled on the TextChanged event of a TextBox (the TextBox is used for searching in a DB)

EDIT:

I commented out the PreRender event and the behaviour is not gone. Now I am thinking this may be caused by a PostBack from the TextBox when I remove the focus from it and select an item from the ListBox.

The evidence to this regard: I have a gif that is shown between the beginning and end of a request from the TextBox (loading indicator) and the gif is visible when I select an item from the ListBox for the first time after a render. The second time there is no problem.

Use the AutoPostBack property to specify whether an automatic postback to the server will occur when the TextBox control loses focus. Pressing the ENTER or the TAB key while in the TextBox control is the most common way to change focus.

I have found out why the behaviour I reported happens. Can I somehow make the TextBox not to postback on losing focus? It is aleady posting back on TextChanged and it is enough.

Was it helpful?

Solution

Use the AutoPostBack property to specify whether an automatic postback to the server will occur when the TextBox control loses focus. Pressing the ENTER or the TAB key while in the TextBox control is the most common way to change focus.

I have found out why the behaviour I reported happens. I will now search how to stop the autopostoback on losing focus.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top