简体   繁体   English

asp.net文本框焦点问题

[英]asp.net textbox focus problems

I am trying to set focus on textbox on page load in asp.net as follows 我正在尝试将重点放在asp.net中页面加载的文本框上,如下所示

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. 或我的CSS有点错误。

try this in javascript 试试这个在JavaScript

<script language=javascript>

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

}

</script>

call " fnLoad ()" function on " onLoad " event of body.. 在正文的“ onLoad ”事件上调用“ fnLoad ()”函数。

You need to add this function in body tag : Like 您需要在body标签中添加此功能:

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

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();
    }

I have tried this with updatepanel and textbox inside it. 我已经尝试过使用它里面的updatepanel和textbox了。

CodeBehind 代码背后

代码背后

.aspx .aspx

.aspx

output 输出

输出量

Try this code.. 试试这个代码。

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


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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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