views:

227

answers:

4

I have a login page. Whenever the page loads, the cursor should blink in the first textbox only that is for entering a userid.

I've written javascript like this, but I am using Master Pages now.

this is my script:

function fcus(x)
{
  x.focus();
}

<asp:TextBox id="textbox1" onload="javascript:fcus(this);">

and where I have to call this method, I write on onload event of textbox but it gives an error.

Can you tell me where I have to call this method?

+1  A: 

Or a bit of jQuery, just dropped into your page:

$(document).ready(function() {

    $(<yourtextboxselector>).focus();

});

http://jquery.com/

Paddy
+1 cause i love to use jquery for such tasks
bastianneu
it is working i write like this in page loadPage.Form.DefaultFocus = txtoldPassword.ClientID ;
Surya sasidhar
A: 

No need to write javascript code, you can try this in your code behind

If your control in Master page, and you need to set focus in login page, then you can find control from master page and call focus method in load event.

((TextBox)Master.FindControl("txtRequiredFocus")).Focus();

OR If you control in login page, that you need to focus on it, In the login page code behind

txtRequiredFocus.focus();

Also you check this thread http://stackoverflow.com/questions/1403498/how-to-make-the-default-focus-in-content-page-from-master-page/1403526#1403526

Muhammad Akhtar
mr.kuhammad akhtar i have to write this code in script tag or below the textbox control can u tell in brief thanku
Surya sasidhar
i think u r little bit confuse about my code mr.Akhtar see i have master page in that i have login page as child page and i want to foucs on that child page textbox if u not get my problem tell me once ok thank u
Surya sasidhar
ok thank u mr.Akthar i write like this and i get iti write in page load Page.Form.DefaultFocus = txtoldPassword.ClientID ;it is working
Surya sasidhar
A: 

You can call the function like below at the end of the textbox.

<asp:TextBox ID="txt23" runat="server" Text="<%$ appSettings:applicationTitle %>" />
<asp:TextBox ID="TextBox1" runat="server" Text="<%$ appSettings:applicationTitle %>" />
<asp:Label ID="Literal1" runat="server" Text="<%$ appSettings:applicationTitle %>" />
     <script language="javascript" type="text/javascript">
     fcus(document.getElementById('TextBox1'));
    </script>  

 <br />
    <asp:HiddenField ID="hidField" runat="server" />


    <asp:Button ID="btnSubmit" runat="server" Text="Create" OnClick="btnSubmit_Click" />
            </div>
        </form>
    </body>
    </html>
Himadri
ok himadri once again this is suryasasidhar. ya i got it i write the code in page load like thisPage.Form.DefaultFocus = txtoldPassword.ClientID ;
Surya sasidhar
+1  A: 

Why don't you use form's DefaultFocus property (ASP.NET 2.0 and above)?

<form defaultfocus=“textbox1”>

You might need to do this programmatically, depending on your scenario.

Look into:

Page.Form.DefaultFocus

To do it programmatically.

RichardOD
ok its working fine. if i have master page and in child page i have the login page then how can i call the method thanku mr.richardod
Surya sasidhar
@Surya- look at this- http://imar.spaanjaars.com/QuickDocId.aspx?QUICKDOC=374
RichardOD
haa! i got it thank u very much mr.RichardOD it is working fine
Surya sasidhar