简体   繁体   中英

Textbox1.Focus(); does not work; as does not Page.SetFocus(Textbox1); why?

as stated both of these do not work (from Page_Load ):

Textbox1.Focus();

or

Page.SetFocus(Textbox1);

any idea why?

this is the control itself:

<asp:TextBox ID="Textbox1" ClientIDMode="Static" CssClass="Textbox1"
                runat="server" MaxLength="80"></asp:TextBox>

it is located on a page, two master pages deep. the form tag is on the second master page, so that is why the focus is not being set there.

not working in: ie, firefox, chrome and safari (all latest versions).

help! thnx

edit: need any more info? i'll do / post anything just tell me what to do to help you help me, i tried EVERYTHING. even google is out of results and is showing my this question.

ok, here is what the ACTUAL problem seems to be.

<asp:Content ID="PlaceHolder1" ContentPlaceHolderID="PlaceHolder1"
    runat="server">

i have no idea how those ID's (ID="PlaceHolder1") got there, but they seem to be the problem.

i figured it out by remaking the project from scratch and seeing if the focus method works in there, then (among other things, so I could post here) i found out this difference.

this works:

<asp:Content ContentPlaceHolderID="PlaceHolder1" runat="server">

hope this helps someone, sometime...

Have you tried this?

<form id="Form1" defaultfocus="Textbox1" runat="server"> 

Or since you mentioned a page in within a MasterPage so it will be in a ContentPlaceholder:

    ContentPlaceHolder cp = (ContentPlaceHolder) Master.FindControl("ContentPlaceHolder1");
    cp.FindControl("Textbox1").Focus();

Make sure you reference your Master in your aspx:

<%@ MasterType VirtualPath="~/MyMaster.master" %>

您可以在客户端事件窗口中编写用于控制文本框的js代码。onload= .....

if your page use the master page , you should find the the textbox firstly ,so then set the textbox focus. and you also should attention the events sequence of the page and master

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