Connecting Tech Pros Worldwide Help | Site Map

User control with javascript events and masterpage

  #1  
Old July 11th, 2007, 09:43 AM
Newbie
 
Join Date: Jul 2007
Posts: 6
I have a .ascx file which has a combo box user control - textbox + listbox with a button. when the button is clicked it triggers javascript functions which are registered using registerscript block in vb .net.

The javascript function works fine when master page is not used with hosting page. But when master page is not used it is working fine.

can anybody figured out this problem?
  #2  
Old July 11th, 2007, 10:15 AM
kenobewan's Avatar
Moderator
 
Join Date: Dec 2006
Posts: 4,745

re: User control with javascript events and masterpage


Welcome to TSDN. Do you mean that your hosting page JS won't work when you use a master page?
  #3  
Old July 13th, 2007, 12:35 AM
Newbie
 
Join Date: Jul 2007
Posts: 6

re: User control with javascript events and masterpage


The javascript function itself is in user control which i register in usercontrol itself. SO the JS is not actually in the hosting page. The JS is injected from the user control after the form tag. Here is the scenario:

1) I have a ascx page
2) hosting page - aspx page - register user control
3) Master page


In user control:
In User controls page load method, Javascript functions are in a string in the user control ( combo box list control - listbox and textbox with an image button that trigeers the JS function ). I register the javascript function using register method . Here is the javascript code in user control (.ascx)


Dim sCID As String = Me.UniqueID & "_"



' Javacsript function
Dim sScript As String = vbCrLf _
& "<script type='text/javascript' language='javascript'>" & vbCrLf _
& "function selectList(sCtrlID, sListID, sTextID) {" & vbCrLf _
& " var list = document.getElementById(sCtrlID + sListID);" & vbCrLf _
& " var text = document.getElementById(sCtrlID + sTextID);" & vbCrLf _
& " text.value = list.options[list.selectedIndex].text;" & vbCrLf _
& " if (sListID == 'listbox') openList(sCtrlID);" & vbCrLf _
& "}" & vbCrLf _
& vbCrLf _
& "function openList(sCtrlID) {" & vbCrLf _
& " var list = document.getElementById(sCtrlID + 'dropbox');" & vbCrLf _
& " var btnimg = document.getElementById(sCtrlID + 'dropbtn');" & vbCrLf _
& " if(list.style.display == 'none') { list.style.display = 'block';" & vbCrLf _
& " btnimg.src = document.getElementById(sCtrlID + 'imageup').src;" & vbCrLf _
& " } else {" & vbCrLf _
& " list.style.display = 'none';" & vbCrLf _
& " btnimg.src = document.getElementById(sCtrlID + 'imagedown').src;" & vbCrLf _
& " }" & vbCrLf _
& " return false;" & vbCrLf _
& "}" & vbCrLf _
& "function scrollList(sCtrlID, sListID, sTextID) {" & vbCrLf _
& " var list = document.getElementById(sCtrlID + sListID);" & vbCrLf _
& " var text = document.getElementById(sCtrlID + sTextID);" & vbCrLf _
& " var search = new String(text.value).toLowerCase();" & vbCrLf _
& " list.selectedIndex = -1;" & vbCrLf _
& " var items = list.options;" & vbCrLf _
& " var option = new String();" & vbCrLf _
& " for (i = 0; i < items.length; i++) {" & vbCrLf _
& " option = items[i].text.toLowerCase();" & vbCrLf _
& " if (option.substring(0, search.length) == search ) {" & vbCrLf _
& " list.selectedIndex = i;" & vbCrLf _
& "break;" & vbCrLf _
& "}" & vbCrLf _
& "}" & vbCrLf _
& "}" & vbCrLf _
& "<" & "/script>" & vbCrLf


-- registering the JS

If Not Page.ClientScript.IsClientScriptBlockRegistered("A HHComboBox") Then
Page.ClientScript.RegisterClientScriptBlock(me.Get Type, "AHHComboBox", sScript)

End If



-- adding js events - listbox(id = dropbox) and textbox (id=textbox2)
-- This does not get triggered when master page is used with Hosting page but without master page it does.

dropbox.Attributes.Add("onclick", "selectList('" & sCID _
& "', 'dropbox', 'textbox2')")
textbox2.Attributes.Add("onkeyup", "scrollList('" & sCID _
& "', 'dropbox', 'textbox2')")
dropbtn.Attributes.Add("onclick", "return openList('" _
& sCID & "')")
  #4  
Old July 14th, 2007, 01:21 PM
Familiar Sight
 
Join Date: Jul 2007
Location: France
Posts: 149

re: User control with javascript events and masterpage


I have a similar problem with dynamically embedding a SWFobject.js into a user control. I also have a master page and when I use Page.RegisterClientScriptBlock(... I get an IE error 'null' is null or not a null object.

Do you also get this error in IE?
  #5  
Old July 16th, 2007, 06:11 PM
Newbie
 
Join Date: Jul 2007
Posts: 6

re: User control with javascript events and masterpage


Quote:
Originally Posted by rsdev
I have a similar problem with dynamically embedding a SWFobject.js into a user control. I also have a master page and when I use Page.RegisterClientScriptBlock(... I get an IE error 'null' is null or not a null object.

Do you also get this error in IE?
Actually i do not get any errors. It does a postback to the asp .net code and event does not occur.
  #6  
Old July 17th, 2007, 10:02 AM
Familiar Sight
 
Join Date: Jul 2007
Location: France
Posts: 149

re: User control with javascript events and masterpage


I managed to fix my problem which wasn't related to the Master page. So I now have a scriptblock of dynamic javascript which works when called by the window.onload method.

In the postback how are you making calls to the javascript?
  #7  
Old July 17th, 2007, 10:08 AM
Familiar Sight
 
Join Date: Jul 2007
Location: France
Posts: 149

re: User control with javascript events and masterpage


Sorry I just noticed that you have onclick etc.

If you have an aspx page with a user control, that inherits a master page this should work fine.

I'd double check that the inherits are set up properly.
  #8  
Old July 30th, 2007, 05:32 PM
Newbie
 
Join Date: Jul 2007
Posts: 6

re: User control with javascript events and masterpage


I think it is related to the unique id that i pass. what should be the id sent from the combo box control to the javascript function,
  #9  
Old July 31st, 2007, 03:38 PM
Familiar Sight
 
Join Date: Jul 2007
Location: France
Posts: 149

re: User control with javascript events and masterpage


I code in C# so VB isn't my strong point. You say it's working without the Masterpage so perhaps the problem is the Me indentifier before UniqueID.

In C# with a master page this would become this.Page. So maybe Me.Page is the VB equivalant?

Let me know:)
  #10  
Old August 8th, 2007, 01:00 AM
Newbie
 
Join Date: Jul 2007
Posts: 6

re: User control with javascript events and masterpage


Quote:
Originally Posted by rsdev
I code in C# so VB isn't my strong point. You say it's working without the Masterpage so perhaps the problem is the Me indentifier before UniqueID.

In C# with a master page this would become this.Page. So maybe Me.Page is the VB equivalant?

Let me know:)

I had used Me.unique id in the combo box control and it was not working.
Reply


Similar Threads
Thread Thread Starter Forum Replies Last Post
Postback data problem db007 answers 0 April 10th, 2008 08:23 PM