.click代码背后 [英] .click at code behind

查看:70
本文介绍了.click代码背后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

.aspx

< asp:HyperLink ID =   leftMenu runat =   server> HyperLink < /   asp:HyperLink  >  



.aspx.cs



1.



  protected   void  Page_Load( object  sender,EventArgs e)
{

ScriptManager.RegisterClientScriptBlock( this this .GetType(), document.getElementById('leftMenu')。click(); true );
}





2.

  protected   void  Page_Load( object  sender,EventArgs e)
{
Response.Write( < script> document.getElementById('leftMenu')。click ();< /脚本>中);
}



错误:未捕获TypeError:无法调用方法'click'of null



I尝试了很多例子,同样的错误信息。请指教,我错过了什么?如果我把脚本放在.aspx javascript

解决方案

问题是: document.getElementById('leftMenu')为null(未定义)。显然,你只是没有 id =leftMenu的元素。检查并确保你拥有它。



此外,这种方法模拟了点击,这可能不是在页面加载和页面加载上具有相同功能的最佳方式在菜单(按钮,或其他任何)上单击。开发一些你从两个地方简单调用的函数是不是更容易:表单加载时和控件的事件处理程序?如果你做了这样的事情,你就不会失去任何东西,但可以避免一些讨厌的裁员。







问题出在这里:你的身份证是什么?在asp元素中,您应该使用 ClientID 。请了解其用法:

http: //msdn.microsoft.com/en-us/library/system.web.ui.control.clientid.aspx [ ^ ]。



-SA

.aspx

<asp:HyperLink ID="leftMenu" runat="server">HyperLink</asp:HyperLink>


.aspx.cs

1.

protected void Page_Load(object sender, EventArgs e)
{

   ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "", "document.getElementById('leftMenu').click();", true);
}



2.

protected void Page_Load(object sender, EventArgs e)
        {
            Response.Write("<script>document.getElementById('leftMenu').click();</script>");
        }


Error: Uncaught TypeError: Cannot call method 'click' of null

I tried a lot of example, same error message. Please advice, what i missed? it's working if i put the script under .aspx javascript

解决方案

The problem is: document.getElementById('leftMenu') is null (undefined). Apparently, you simply don't have an element with id="leftMenu". Check it up and make sure you have it.

Also, this method simulates the click, which is perhaps not the best way of having the same functionality on page load and on menu (button, or whatever else) click. Isn't it much easier to develop some function which you simply call from two places: when the form is loaded and from the control's event handler? If you do such thing, you won't loose anything at all but can save yourself from some annoying redundancies.

[EDIT]

Here is the problem: what is your "ID"? In your "asp" element, you should use ClientID instead. Please learn about its usage:
http://msdn.microsoft.com/en-us/library/system.web.ui.control.clientid.aspx[^].

—SA


这篇关于.click代码背后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆