Making Things Focus
There are new challenges each and every day, and coming up with a consistent way to set Focus on a given form field is my new one. Of course, we can't leave things that way... so I now have a method to fix this problem.
The Javascript required was easy:
See, rather easy. To make it run in the page all you do is add the onLoad attribute to the body tag like this: onload="setFocus()"
Now that your page is ready, it is time to add the pieces you need to the ASP.Net code behind file. You have a few options:
1. Set focus immediately on page load: formElement = object.ClientId
2. Set focus during an event handler - same as above
3. Set the button to be clicked to put the ClientId value into a hidden text field:
button.Attributes.Add("onmousedown", "document.forms[0].hiddentext.value =
'" + object.ClientID + "';")
Don't forget that objects contained within Web User Controls have "_ctl0_" appended to the
front of them.
I will post a demonstration later, any thoughts until then?
The Javascript required was easy:
function setFocus()
{
//Notice the use of a Public variable
var formElement = "<%=formElement%>";
//We have to check and verify it equals something
if (formElement != "")
{
document.forms[0][formElement].focus();
document.forms[0][formElement].setSelectionRange(0,0);
}
}
See, rather easy. To make it run in the page all you do is add the onLoad attribute to the body tag like this: onload="setFocus()"
Now that your page is ready, it is time to add the pieces you need to the ASP.Net code behind file. You have a few options:
1. Set focus immediately on page load: formElement = object.ClientId
2. Set focus during an event handler - same as above
3. Set the button to be clicked to put the ClientId value into a hidden text field:
button.Attributes.Add("onmousedown", "document.forms[0].hiddentext.value =
'" + object.ClientID + "';")
Don't forget that objects contained within Web User Controls have "_ctl0_" appended to the
front of them.
I will post a demonstration later, any thoughts until then?