Click here to Skip to main content
15,893,368 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
.aspx
C#
<asp:HyperLink ID="leftMenu" runat="server">HyperLink</asp:HyperLink>

.aspx.cs

1.

C#
protected void Page_Load(object sender, EventArgs e)
{

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


2.
C#
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
Posted
Updated 10-Jun-13 15:53pm
v2
Comments
Sergey Alexandrovich Kryukov 10-Jun-13 21:12pm    
ASP.NET? Tag it!
—SA

1 solution

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
 
Share this answer
 
v2
Comments
melvintcs 10-Jun-13 21:53pm    
ID='leftMenu' is exists, the javascript is working at aspx, but not at code behind.
Sergey Alexandrovich Kryukov 10-Jun-13 22:09pm    
What are you even talking about?
—SA
Sergey Alexandrovich Kryukov 10-Jun-13 22:10pm    
Where does it exist? Did you check HTML, not ASP.NET? JavaScript works with HTML, not ASP.NET.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900