1

Right now I'm using the OnSelectedIndexChanged event to trigger a post back and update some other fields in the form. That works fine. But I would like it to do the post back when the drop down list loses focus, rather than on every change. The problem is, when someone selects the list, then types a number, it will change with every key stroke they press, and that will trigger the post back before they get to the value they are trying to type.

Is this possible?

Petey B
  • 11,439
  • 25
  • 81
  • 101

2 Answers2

3

The JavaScript event for something losing focus is called onblur.

Use Attributes.Add() in server-side code to add the function name to be called. You can then trigger the postback from said function.

Widor
  • 13,003
  • 7
  • 42
  • 64
  • I don't want to run javascript, I want to run server side code onblur. Can I use javascript to trigger the post back? – Petey B Aug 04 '11 at 16:34
  • Yes, in fact ASP.NET uses JavaScript to trigger postbacks already, you just don't write it explicitly so it isn't immediately obvious. You can either call it yourself (a bit quick & dirty), or trigger a hidden control to do the postback 'naturally'. Take a look at [this question.](http://stackoverflow.com/questions/5448825/is-it-ok-to-use-dopostback). – Widor Aug 04 '11 at 16:40
3

Try this.

ddlDropDown.Attributes.Add("onblur", "__doPostBack('ddlDropDown','');");

I have not tested it, but I believe it will work - or I hope.

janhartmann
  • 14,713
  • 15
  • 82
  • 138