在 Javascript 中找不到 asp:textbox

发布于 2024-09-04 21:30:04 字数 549 浏览 11 评论 0原文

我正在尝试将 onkeydown 属性添加到 asp:textbox。由于某种原因,我的代码找不到登录视图内的文本框。

我做错了什么吗?

<script type="text/javascript">
window.onload = function() {
    UserName.Attributes.Add("onKeyDown", "KeyDownHandler('" + btn.ClientID + "')");
    Password.Attributes.Add("onKeyDown", "KeyDownHandler('" + btn.ClientID + "')");
}

function KeyDownHandler(btn)
{
    if (event.keyCode == 13)
    {
        event.returnValue=false;    
        event.cancel = true;
        document.getElementById(btn).click();
    }
}
</script>

I am trying to add the onkeydown attribute to an asp:textbox. For some reason my code can't find the textbox that is inside a loginview.

Am I doing something wrong?

<script type="text/javascript">
window.onload = function() {
    UserName.Attributes.Add("onKeyDown", "KeyDownHandler('" + btn.ClientID + "')");
    Password.Attributes.Add("onKeyDown", "KeyDownHandler('" + btn.ClientID + "')");
}

function KeyDownHandler(btn)
{
    if (event.keyCode == 13)
    {
        event.returnValue=false;    
        event.cancel = true;
        document.getElementById(btn).click();
    }
}
</script>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

乙白 2024-09-11 21:30:04

您的代码正在尝试在客户端脚本中添加事件处理程序属性。这需要在服务器端代码块中发生。例如:

<script runat="server"> 
    UserName.Attributes.Add("onKeyDown", "KeyDownHandler('" + btn.ClientID + "')"); 
    Password.Attributes.Add("onKeyDown", "KeyDownHandler('" + btn.ClientID + "')"); 
</script>
<script type="text/javascript">
function KeyDownHandler(btn) 
{ 
    if (event.keyCode == 13) 
    { 
        event.returnValue=false;     
        event.cancel = true; 
        document.getElementById(btn).click(); 
    } 
} 
</script> 

或者,如果您有代码隐藏页面,请将 attribute.Add 调用放入 PreRender 事件中。

Your code is trying to add the event handler attributes in the client script. This needs to happen in a server-side code block. Something like:

<script runat="server"> 
    UserName.Attributes.Add("onKeyDown", "KeyDownHandler('" + btn.ClientID + "')"); 
    Password.Attributes.Add("onKeyDown", "KeyDownHandler('" + btn.ClientID + "')"); 
</script>
<script type="text/javascript">
function KeyDownHandler(btn) 
{ 
    if (event.keyCode == 13) 
    { 
        event.returnValue=false;     
        event.cancel = true; 
        document.getElementById(btn).click(); 
    } 
} 
</script> 

Alternatively, if you have a code-behind page, put the attribute.Add calls in the PreRender event.

春风十里 2024-09-11 21:30:04

在您的 aspx 文件中,添加服务器脚本,该脚本将现有的用户名和密码文本框绑定到名为 KeyDownHandler 的客户端事件处理程序:

<script runat="server">
   protected void Page_Load(object sender, EventArgs e)
   {
      TextBox userNameControl = FindControl("UserName") as TextBox;
      TextBox passwordControl = FindControl("Password") as TextBox;

      if (userNameControl != null)
         userNameControl.Attributes.Add("onKeyDown", "KeyDownHandler(this)"); 

      if (passwordControl != null)
         passwordControl.Attributes.Add("onKeyDown", "KeyDownHandler(this)"); 
   }
</script>

然后声明事件处理程序的客户端脚本:

<script type="text/javascript">
function KeyDownHandler(domButton) 
{ 
    if (event.keyCode == 13) 
    { 
        event.returnValue=false;     
        event.cancel = true; 
        domButton.click(); 
    } 
} 
</script>

In your aspx file, add the server script that will bind existing UserName and Password textboxes to a client event handler called KeyDownHandler:

<script runat="server">
   protected void Page_Load(object sender, EventArgs e)
   {
      TextBox userNameControl = FindControl("UserName") as TextBox;
      TextBox passwordControl = FindControl("Password") as TextBox;

      if (userNameControl != null)
         userNameControl.Attributes.Add("onKeyDown", "KeyDownHandler(this)"); 

      if (passwordControl != null)
         passwordControl.Attributes.Add("onKeyDown", "KeyDownHandler(this)"); 
   }
</script>

Then declare the event handler's client script:

<script type="text/javascript">
function KeyDownHandler(domButton) 
{ 
    if (event.keyCode == 13) 
    { 
        event.returnValue=false;     
        event.cancel = true; 
        domButton.click(); 
    } 
} 
</script>
绮烟 2024-09-11 21:30:04

尝试以这种方式连接事件处理程序参数:

<script type="text/javascript">
    window.onload = function() {
    UserName.Attributes.Add("onKeyDown", "KeyDownHandler(this)");
    Password.Attributes.Add("onKeyDown", "KeyDownHandler(this)");
}

function KeyDownHandler(domButton)
{
    if (event.keyCode == 13)
    {
        event.returnValue=false;    
        event.cancel = true;
        domButton.click();
    }
}
</script>

Try wiring the event handler parameter this way:

<script type="text/javascript">
    window.onload = function() {
    UserName.Attributes.Add("onKeyDown", "KeyDownHandler(this)");
    Password.Attributes.Add("onKeyDown", "KeyDownHandler(this)");
}

function KeyDownHandler(domButton)
{
    if (event.keyCode == 13)
    {
        event.returnValue=false;    
        event.cancel = true;
        domButton.click();
    }
}
</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文