views:

275

answers:

4

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.

+1  A: 

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" %>
The Menace
i tried it. not working. error: System.NullReferenceException: Object reference not set to an instance of an object. here's what i did: i put <%@ MasterType VirtualPath="~/MyMaster.master" %> on the page (not master page) and ContentPlaceHolder cp = (ContentPlaceHolder) Master.FindControl("ContentPlaceHolder1"); cp.FindControl("Textbox1").Focus(); on the master page (tried it also on the page (not master page)). not working. not even sure WHY i'd need to do something like this?!?
b0x0rz
thnx for trying, i figured out what the problem was (see my own answer).
b0x0rz
A: 

you can write the js code control the textbox in the client event window.onload=.....

don't want to. it SHOULD work from c# code behind.
b0x0rz
A: 

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

i have no idea what you mean, sorry.
b0x0rz
+1  A: 

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...

b0x0rz
Mark this answer as accepted then :)
Daniel S
i can in 15 hours :P so stack overflow tells me...
b0x0rz