Converting Static Code to Dynamic Code
The final step is relatively easy. To make our injected script block totally dynamic, we can pass in the name of the control to focus as an argument to SetFocusControl and insert the control name into the script text. The final revision is shown in Listing 3.
Listing 3 Specifying the Control Name Dynamically.
A parameter was added to the constant and folded into the script text. That's it; we're finished.
The remaining task is figuring out when and where to call SetFocusControl. You have to devise some programmatic logic to determine what control has the focus and call SetFocusControl when the postback handling of the page occurs. Typically, this kind of call will occur when you perform some action that causes a postback. The context of that action should help you reasonably determine what control to focus. For example, if a specific control fails validation, the failing control would be the one to focus.