Thursday, October 11, 2007

Setting focus to control in Asp.NET 2

I'm building a website (as i told in my previous post). In one form i used wizard control. The problem was that the form is in master page, which itself contains navigation bar as header of the site. When page loaded, the control will go to links and controls on the master form first and then to controls in this form.

I googled and found nice snippets of Javascript and even a control class on codeproject. But I tried to go the easier way first. So in page_load event I tried like dropdownlist1.Focus() and voila it worked.

Next was to see on which step the wizard is, which I was already doing to validate the inputs from user, and set the focus to first control on next page of wizard.

There was a postback also, where a dropdown list changes and loads another one. In postback handler (dropdownlist2_selectedindexchanged) at the end, I wrote dropdownlist3.Focus() and it was working here too.

Hope it helps you.

No comments: