User control with javascript events and masterpage 
July 11th, 2007, 08:43 AM
| | Newbie | | Join Date: Jul 2007
Posts: 6
| | User control with javascript events and masterpage
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?
| 
July 11th, 2007, 09:15 AM
|  | Moderator | | Join Date: Dec 2006
Posts: 4,745
| |
Welcome to TSDN. Do you mean that your hosting page JS won't work when you use a master page?
| 
July 12th, 2007, 11:35 PM
| | Newbie | | Join Date: Jul 2007
Posts: 6
| |
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 & "')")
| 
July 14th, 2007, 12:21 PM
| | Familiar Sight | | Join Date: Jul 2007 Location: France
Posts: 149
| |
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?
| 
July 16th, 2007, 05:11 PM
| | Newbie | | Join Date: Jul 2007
Posts: 6
| | 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.
| 
July 17th, 2007, 09:02 AM
| | Familiar Sight | | Join Date: Jul 2007 Location: France
Posts: 149
| |
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?
| 
July 17th, 2007, 09:08 AM
| | Familiar Sight | | Join Date: Jul 2007 Location: France
Posts: 149
| |
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.
| 
July 30th, 2007, 04:32 PM
| | Newbie | | Join Date: Jul 2007
Posts: 6
| |
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,
| 
July 31st, 2007, 02:38 PM
| | Familiar Sight | | Join Date: Jul 2007 Location: France
Posts: 149
| |
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:)
| 
August 8th, 2007, 12:00 AM
| | Newbie | | Join Date: Jul 2007
Posts: 6
| | 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.
|  | | Thread Tools | Search this Thread | | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 220,662 network members.
|