简体   繁体   中英

HOW to set Focus on a hidden Textbox

In my page_load event I have this code :

myTextbox.focus().

So when I set my textbox to visible=false my code doesn't work.

Hidden controls are not focusable. Set the Opacity to 0 instead.

You cannot. If something isn't rendered, it cannot be interacted with, so you cannot set the focus to it.

Focus means that user input is focused to the control, that means if the control is a text-box, the text input cursor will be placed in the control or if it is a checkbox, the checkbox will be focused and may be selected by pressing space, you can't put a text input cursor in a hidden control and it cannot be used for any user input.

If you still want to set focus for some reason, try setting its height and width to Zero. Like style="height:0px; width:0px" and use Page.SetFocus(yourControl); to set focus

When you set the Control.Visible property to false it doesn't just hide the control on the page. It omits that control from being rendered on the client's browser entirely, but "remembers" everything about that control on the server for future postbacks.

If you actually do a client side hide (ie set the CSS Style display: none; then it will still exist on the page, but just be hidden. At that point you can focus it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM