Custom Control Javascript Issues
-
I have a custom control that renders a tree view. Each node on the tree has a hyperlink. The issue is that I cannot get a handle to the hyperlink object to set the onclick attribute for firing my javascript function. I can, however, set the URL, so what I am ending up with is setting the url to point to a javascript funcion like this:
<a href="javascript:PostIt(4)">Send Request</a>
(I put a colon in between javascript and PostIt, but it's not showing up on this page properly, btw). and a script that looks like this:
function PostIt(folderID)
{
___ctl0__ctl0__ctl0_mainTreeView_State.value=folderID;
__doPostBack('_ctl0:_ctl0:_ctl0:mainTreeView','___ctl0__ctl0__ctl0_mainTreeView_State.value');
}The value '___ctl0__ctl0__ctl0_mainTreeView_State' was created by this call in my C# code:
Page.RegisterHiddenField(HelperID, selectedFolderID );
The error message I get, though is, "Error: PostIt is not defined Source File: javascript:PostIt(4) Line: 1". Any ideas as to why it would be saying that this function is not defined (the script block was added to the page through Page.RegisterClientScriptBlock)? Thanks in advance. -Matt ------------------------------------------ The 3 great virtues of a programmer: Laziness, Impatience, and Hubris. --Larry Wall
-
I have a custom control that renders a tree view. Each node on the tree has a hyperlink. The issue is that I cannot get a handle to the hyperlink object to set the onclick attribute for firing my javascript function. I can, however, set the URL, so what I am ending up with is setting the url to point to a javascript funcion like this:
<a href="javascript:PostIt(4)">Send Request</a>
(I put a colon in between javascript and PostIt, but it's not showing up on this page properly, btw). and a script that looks like this:
function PostIt(folderID)
{
___ctl0__ctl0__ctl0_mainTreeView_State.value=folderID;
__doPostBack('_ctl0:_ctl0:_ctl0:mainTreeView','___ctl0__ctl0__ctl0_mainTreeView_State.value');
}The value '___ctl0__ctl0__ctl0_mainTreeView_State' was created by this call in my C# code:
Page.RegisterHiddenField(HelperID, selectedFolderID );
The error message I get, though is, "Error: PostIt is not defined Source File: javascript:PostIt(4) Line: 1". Any ideas as to why it would be saying that this function is not defined (the script block was added to the page through Page.RegisterClientScriptBlock)? Thanks in advance. -Matt ------------------------------------------ The 3 great virtues of a programmer: Laziness, Impatience, and Hubris. --Larry Wall
I suspect that the error happens due to the following code line:
___ctl0__ctl0__ctl0_mainTreeView_State.value=folderID;
Here, you are trying to access the hidden element via its name, and it may cause the 'undefined' script error. You can try again to get the value of the hidden emelent via its id as below:
window.document.getElementById('___ctl0__ctl0__ctl0_mainTreeView_State').value=folderID;
-
I suspect that the error happens due to the following code line:
___ctl0__ctl0__ctl0_mainTreeView_State.value=folderID;
Here, you are trying to access the hidden element via its name, and it may cause the 'undefined' script error. You can try again to get the value of the hidden emelent via its id as below:
window.document.getElementById('___ctl0__ctl0__ctl0_mainTreeView_State').value=folderID;
This was a good suggestion. It led me to discover that you cannot access a hidden element using getElementById unless you've given that element an ID. It won't work using the name unless you specify the whole hierarchy (document.form.hiddenfieldname). So instead of registering my hidden field with Page.RegisterHiddenField, I just manually add the hidden input type with an ID field. This actually works. Thanks a bunch. -Matt ------------------------------------------ The 3 great virtues of a programmer: Laziness, Impatience, and Hubris. --Larry Wall
-
This was a good suggestion. It led me to discover that you cannot access a hidden element using getElementById unless you've given that element an ID. It won't work using the name unless you specify the whole hierarchy (document.form.hiddenfieldname). So instead of registering my hidden field with Page.RegisterHiddenField, I just manually add the hidden input type with an ID field. This actually works. Thanks a bunch. -Matt ------------------------------------------ The 3 great virtues of a programmer: Laziness, Impatience, and Hubris. --Larry Wall
Hi Matt, In fact, you can keep using the Page.RegisterHiddenField method to register your hidden field as there are a couple of alternative ways to access a hidden element, for example: the document.getElementsByName method or the document.all collection. You can take a quick look at the document object here in MSDN http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/reference/objects/obj_document.asp[^]