Connecting Tech Pros Worldwide Forums | Help | Site Map

User control with javascript events and masterpage

Newbie
 
Join Date: Jul 2007
Posts: 6
#1: Jul 11 '07
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?

kenobewan's Avatar
Moderator
 
Join Date: Dec 2006
Posts: 4,745
#2: Jul 11 '07

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?
Newbie
 
Join Date: Jul 2007
Posts: 6
#3: Jul 13 '07

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 & "')")
Familiar Sight
 
Join Date: Jul 2007
Location: France
Posts: 149
#4: Jul 14 '07

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?
Newbie
 
Join Date: Jul 2007
Posts: 6
#5: Jul 16 '07

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.
Familiar Sight
 
Join Date: Jul 2007
Location: France
Posts: 149
#6: Jul 17 '07

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?
Familiar Sight
 
Join Date: Jul 2007
Location: France
Posts: 149
#7: Jul 17 '07

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.
Newbie
 
Join Date: Jul 2007
Posts: 6
#8: Jul 30 '07

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,
Familiar Sight
 
Join Date: Jul 2007
Location: France
Posts: 149
#9: Jul 31 '07

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:)
Newbie
 
Join Date: Jul 2007
Posts: 6
#10: Aug 8 '07

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 .NET Framework bytes