0

I have can I make an Image button have focus while loading the page. I have set the Image button's TabIndex value to "-1" in script side, while pressing tab key the tab index is changing for all the controls exactly inclding Image button, but I want the Image button to be focused while loading the page.

Is there any difference between Image button and other controls? What property or function i've to use to set focus?

I am using the below line to set focus, but it's not working:

ScriptManager1.SetFocus(imgSearch1);

Thanks in advance.

Joel Etherton
  • 37,325
  • 10
  • 89
  • 104
Lakshmitha
  • 675
  • 3
  • 11
  • 17
  • Have you tried these options... http://stackoverflow.com/questions/45827/how-do-you-automatically-set-the-focus-to-a-textbox-when-a-web-page-loads – Lloyd Nov 08 '11 at 11:36
  • imgSearch1 is the button you want to focus? I'm not so familiar with ASP.NET, but there should be a Focus() method that can be called directly on the control you and to focus. – Tudor Nov 08 '11 at 11:59
  • ya i have function, function imgSetFocus() { var ele = document.getElementById('TabContainer1_tabCountry_imgSearch1'); ele.tabIndex = -1; ele.focus(); } but its showing error message as "htmlfile: Can't move focus to the control because it is invisible, not enabled, or of a type that does not accept the focus." that image button properties are Enabled and Visible also – Lakshmitha Nov 08 '11 at 12:11

2 Answers2

1

Simple way is create one javascript function and call it onLoad in body tag.

Bhargav Mistri
  • 944
  • 8
  • 20
  • hi, i have called one javascript function as u said, function imgSetFocus() { var ele = document.getElementById('TabContainer1_tabCountry_imgSearch1'); ele.tabIndex = -1; ele.focus(); } but its showing error message as "htmlfile: Can't move focus to the control because it is invisible, not enabled, or of a type that does not accept the focus." that image button properties are Enabled and Visible also. then why its showing this error? – Lakshmitha Nov 08 '11 at 11:52
  • use document.getElementById('<% imgSearch1.clientId %>').focus(); – Bhargav Mistri Nov 08 '11 at 12:13
  • its showing error "; expected" I think your code format is correct only, with out syntax error. I am not getting why this error is coming. I've checked everything. – Lakshmitha Nov 08 '11 at 12:31
  • function setload() { document.getElementById('<%= ImageButton1.clientId %>').focus(); } – Bhargav Mistri Nov 08 '11 at 12:54
  • please use this code : function setload() { document.getElementById('<%= imgSearch1.clientId %>').focus(); } – Bhargav Mistri Nov 08 '11 at 12:56
1

Try this

imgSearch1.Focus();

or setting the focus first before setting its tabIndex:

var ele;
function imgSetFocus() {
   ele = document.getElementById('<%= imgSearch1.ClientID %>');
   ele.focus(); 
   setTimeout(function(){ ele.tabIndex = -1 }, 500);
}
Alvin
  • 985
  • 5
  • 13
  • Again its showing run time error "htmlfile: Can't move focus to the control because it is invisible, not enabled, or of a type that does not accept the focus." – Lakshmitha Nov 09 '11 at 06:34
  • I think youre putting these JS codes in the head arent you? put it below the page.\ – Alvin Nov 09 '11 at 08:51
  • javascript will naturally not able to find a control if your codes are executed right before the control is being created/rendered. just what ive said, put it AFTER the control in your markup, or better below the page. (before the end of the body tag

    )

    – Alvin Nov 09 '11 at 08:54