0

I am trying to set focus on textbox on page load in asp.net as follows

protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!IsPostBack)
            {
               // fillUnitType();
                //
                fillLastCode();
                txt_Grn_Date.Text = System.DateTime.Now.ToString("dd-MM-yyyy");
                setinitialrow_lvl();
 txt_Po_No.Focus();

            }
        }
        catch (Exception ex)
        {

            lblMessage.Text = ex.Message;
        }
    }

But the textbox is not getting focused. What is that I am missing. I have used update panel is it because of that.? Or my css is slightly faulty.

6
  • too little information Commented Aug 22, 2013 at 4:24
  • Past the code txt_Po_No.Focus(); in out side of! isPostBack Commented Aug 22, 2013 at 4:26
  • @RameshRajendran : not working. Commented Aug 22, 2013 at 4:29
  • try ramesh's suggestion Commented Aug 22, 2013 at 4:29
  • It's due to update panel just place your textbox outside the update panel & see. Commented Aug 22, 2013 at 4:45

4 Answers 4

1

Write following function in your codebehind and for every control call this function

private void Set_Focus(string controlname)
{
string strScript;

strScript = "<script language=javascript> document.all('" + controlname + "').focus() </script>";
RegisterStartupScript("focus", strScript);
}

Set

tapindex = 0
TextBox1.Focus();

or

 textBox1.Select();

or

 protected override void OnShown(EventArgs e)
    {
        textBox1.Focus();
        base.OnShown(e);
    }

or

setTimeout("myFocusFunction()", 500);

    function myFocusFunction(){
        $("#myTextBoxID").focus();
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Its working.Paste this function in code behind.From pageload call this function with Set_Focus(textbox1.text) as parameter..
1

try this in javascript

<script language=javascript>

function fnLoad(){
document.getElementById("<%= txt_Po_No.ClientID %>").focus();

}

</script>

call "fnLoad()" function on "onLoad" event of body..

You need to add this function in body tag : Like

<body onload="fnLoad()">........</body>

Update:

try another way

<script language=javascript>
    $(document).ready(function(){  document.getElementById("<%= txt_Po_No.ClientID %>").focus();}) 
  </script>

or

<script language=javascript>
        $(window).load(function(){  document.getElementById("<%= txt_Po_No.ClientID %>").focus();}) 
      </script>

4 Comments

absolutely . But must call the function in body tag .
When I use ClientScript.RegisterStartupScript(GetType(),"id","callMyJSFunction()",true); this my controls disappera. Page gets blank.
You need to add this function in body tag : Like '<body onload="fnLoad()"></body>'
I have a master page. So there is no body tag.
1

I have tried this with updatepanel and textbox inside it.

CodeBehind

CodeBehind

.aspx

.aspx

output

Output

Comments

0

Try this code..

string jsCode= "<script language=javascript>document.getElementById('<%= TEXTBOX.ClientID%>').focus();</script>";   


ClientScript.RegisterClientScriptBlock(GetType(), "txtbox",jsCode, false);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.