469,323 Members | 1,614 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,323 developers. It's quick & easy.

User control with javascript events and masterpage

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?
Jul 11 '07 #1
9 6650
kenobewan
4,871 Expert 4TB
Welcome to TSDN. Do you mean that your hosting page JS won't work when you use a master page?
Jul 11 '07 #2
mumbai
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 & "')")
Jul 12 '07 #3
rsdev
149 100+
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?
Jul 14 '07 #4
mumbai
6
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.
Jul 16 '07 #5
rsdev
149 100+
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?
Jul 17 '07 #6
rsdev
149 100+
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.
Jul 17 '07 #7
mumbai
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,
Jul 30 '07 #8
rsdev
149 100+
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:)
Jul 31 '07 #9
mumbai
6
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.
Aug 8 '07 #10

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Ahmed | last post: by
2 posts views Thread by Scott | last post: by
reply views Thread by benjamin.soulier | last post: by
9 posts views Thread by J055 | last post: by
10 posts views Thread by Cirene | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by Gurmeet2796 | last post: by
reply views Thread by listenups61195 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.