Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > trying to use dynamically added jscript for ddlist onchange event

Reply
Thread Tools

trying to use dynamically added jscript for ddlist onchange event

 
 
Zeebra3
Guest
Posts: n/a
 
      08-06-2003
Here goes: I have a web form with several asp:dropdownlists, with
which, when selection is changed I want to fire an event defined in
some clientside js.

The content of the clientside code is dependant on data collected in
the code behind on the server. I have set AutoPostback to false for
the controls and added lines such as
cboMyCombo1.Attributes.Add("onchange", "MyCombos_OnChange('1')");
in the Page_Load event, which is rendered OK when I view the source of
the page.
I wanted to use RegisterClientScriptBlock to get the client js into
the page, but found that it didn't put the code into the <head> tag,
but into the <form> tag. After some research I deemed it necessary to
get the js into the <head> tag and so I added an id and runat=server
to the head tag.
At design time I have some script already declared in the head tag so
I got the InnerHtml property of the head tag in the Page_Load event
and appended some more script within additional <script> tags.

When rendered and looking at the source, everything appears to be
where I want it to be on the page. The onchange attributes are there
and the 'MyCombos_OnChange' js function is within the head tag.
However, you've guessed it, I get the good old message
"Microsoft JScript runtime error: Object expected" suggesting that the
'MyCombos_OnChange' function called from the onchange attribute is not
defined.
Interestingly, if I change the cboMyCombo1.Attributes.Add call in
Page_Load to add the attribute onchange and call a function that is
already hard coded into the page, this function is called OK. I have
also tried swapping round the order in which hard coded client js and
dynamic js appears, even taken out all hard coded client js. I just
can't get my onchange event to call a dynamically created js function
even when it is within the head tag.
I may be overlooking something, but I've exhausted all posts similar
to this problem and am now seeking inspiration.
Thanks for any help with this
 
Reply With Quote
 
 
 
 
Ken Cox [Microsoft MVP]
Guest
Posts: n/a
 
      08-08-2003
Hi Dave,

I played with this for a while and it *seems* to work okay. Not quite sure that
I understand your issue so I may have missed the point.

In the code below, I insert the script into the head tag as a generic control.
I insert the onchange event as an attribute to the dropdown list control. The
script is in place and is being called.

Perhaps you can see if this is what you're after and if not, post some code
that fails for you?

Ken
MVP [ASP.NET]


Public Class jscrpt
Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub
Protected WithEvents cboMyCombo1 As System.Web.UI.WebControls.DropDownList
Protected Head1 As System.Web.UI.HtmlControls.HtmlGenericControl

'NOTE: The following placeholder declaration is required by the Web Form
Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
cboMyCombo1.Attributes.Add("onchange", "MyCombos_OnChange('1')")
Head1.InnerHtml = " <script>function
MyCombos_OnChange(strText){alert(strText);}</script>"
End Sub

End Class


<%@ Page Language="vb" AutoEventWireup="false" Codebehind="jscrpt.aspx.vb"
Inherits="p733workev.jscrpt"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head runat="server" id="head1">
</head>
<body ms_positioning="FlowLayout">
<form id="Form1" method="post" runat="server">
<p>
<asp:dropdownlist id="cboMyCombo1" runat="server">
<asp:listitem value="red">red</asp:listitem>
<asp:listitem value="green">green</asp:listitem>
<asp:listitem value="blue">blue</asp:listitem>
</asp:dropdownlist></p>
<p>
</form>
</P>
</body>
</html>

"Zeebra3" <> wrote in message
news: om...
Here goes: I have a web form with several asp:dropdownlists, with
which, when selection is changed I want to fire an event defined in
some clientside js.

The content of the clientside code is dependant on data collected in
the code behind on the server. I have set AutoPostback to false for
the controls and added lines such as
cboMyCombo1.Attributes.Add("onchange", "MyCombos_OnChange('1')");
in the Page_Load event, which is rendered OK when I view the source of
the page.
I wanted to use RegisterClientScriptBlock to get the client js into
the page, but found that it didn't put the code into the <head> tag,
but into the <form> tag. After some research I deemed it necessary to
get the js into the <head> tag and so I added an id and runat=server
to the head tag.
At design time I have some script already declared in the head tag so
I got the InnerHtml property of the head tag in the Page_Load event
and appended some more script within additional <script> tags.

When rendered and looking at the source, everything appears to be
where I want it to be on the page. The onchange attributes are there
and the 'MyCombos_OnChange' js function is within the head tag.
However, you've guessed it, I get the good old message
"Microsoft JScript runtime error: Object expected" suggesting that the
'MyCombos_OnChange' function called from the onchange attribute is not
defined.
Interestingly, if I change the cboMyCombo1.Attributes.Add call in
Page_Load to add the attribute onchange and call a function that is
already hard coded into the page, this function is called OK. I have
also tried swapping round the order in which hard coded client js and
dynamic js appears, even taken out all hard coded client js. I just
can't get my onchange event to call a dynamically created js function
even when it is within the head tag.
I may be overlooking something, but I've exhausted all posts similar
to this problem and am now seeking inspiration.
Thanks for any help with this


 
Reply With Quote
 
 
 
 
Zeebra3
Guest
Posts: n/a
 
      08-08-2003
Many thanks for your response Ken.
What you have done will indeed work. I have now found out what my
problem was. The block of script that I was trying to add into the
head was something like
<script language="javascript><!--function doThis(arg){window.alert("Hi
There");}--></script>
My problem was the <!-- --> comment tags which I had failed to put
line breaksd around. This was causing problems when it was rendered to
the page.
I realised this when I appended my dynamic javascript to existing
javascript that I hard hardcoded into the page at design time. This
existing javascript had the comment tags on separate lines and so I
didn't need to add in any more, and the function worked OK.
As an addition, once I knew that the comment tags were causing my
problem, I tried using RegisterClientScriptBlock to add my dynamic
function into the form tag on the page again and that worked too. So
the script doesn't need to be put into the Head element after all.
Thanks again for your response

"Ken Cox [Microsoft MVP]" <> wrote in message news:<>...
> Hi Dave,
>
> I played with this for a while and it *seems* to work okay. Not quite sure that
> I understand your issue so I may have missed the point.
>
> In the code below, I insert the script into the head tag as a generic control.
> I insert the onchange event as an attribute to the dropdown list control. The
> script is in place and is being called.
>
> Perhaps you can see if this is what you're after and if not, post some code
> that fails for you?
>
> Ken
> MVP [ASP.NET]
>
>
> Public Class jscrpt
> Inherits System.Web.UI.Page
>
> #Region " Web Form Designer Generated Code "
>
> 'This call is required by the Web Form Designer.
> <System.Diagnostics.DebuggerStepThrough()> Private Sub
> InitializeComponent()
>
> End Sub
> Protected WithEvents cboMyCombo1 As System.Web.UI.WebControls.DropDownList
> Protected Head1 As System.Web.UI.HtmlControls.HtmlGenericControl
>
> 'NOTE: The following placeholder declaration is required by the Web Form
> Designer.
> 'Do not delete or move it.
> Private designerPlaceholderDeclaration As System.Object
>
> Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles MyBase.Init
> 'CODEGEN: This method call is required by the Web Form Designer
> 'Do not modify it using the code editor.
> InitializeComponent()
> End Sub
>
> #End Region
>
> Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles MyBase.Load
> cboMyCombo1.Attributes.Add("onchange", "MyCombos_OnChange('1')")
> Head1.InnerHtml = " <script>function
> MyCombos_OnChange(strText){alert(strText);}</script>"
> End Sub
>
> End Class
>
>
> <%@ Page Language="vb" AutoEventWireup="false" Codebehind="jscrpt.aspx.vb"
> Inherits="p733workev.jscrpt"%>
> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
> <html>
> <head runat="server" id="head1">
> </head>
> <body ms_positioning="FlowLayout">
> <form id="Form1" method="post" runat="server">
> <p>
> <asp:dropdownlist id="cboMyCombo1" runat="server">
> <asp:listitem value="red">red</asp:listitem>
> <asp:listitem value="green">green</asp:listitem>
> <asp:listitem value="blue">blue</asp:listitem>
> </asp:dropdownlist></p>
> <p>
> </form>
> </P>
> </body>
> </html>
>

 
Reply With Quote
 
Ken Cox [Microsoft MVP]
Guest
Posts: n/a
 
      08-08-2003
Glad to hear you got things going!

Ken

"Zeebra3" <> wrote in message
news: m...
Many thanks for your response Ken.
What you have done will indeed work. I have now found out what my
problem was. The block of script that I was trying to add into the
head was something like
<script language="javascript><!--function doThis(arg){window.alert("Hi
There");}--></script>
My problem was the <!-- --> comment tags which I had failed to put
line breaksd around. This was causing problems when it was rendered to
the page.
I realised this when I appended my dynamic javascript to existing
javascript that I hard hardcoded into the page at design time. This
existing javascript had the comment tags on separate lines and so I
didn't need to add in any more, and the function worked OK.
As an addition, once I knew that the comment tags were causing my
problem, I tried using RegisterClientScriptBlock to add my dynamic
function into the form tag on the page again and that worked too. So
the script doesn't need to be put into the Head element after all.
Thanks again for your response

"Ken Cox [Microsoft MVP]" <> wrote in message
news:<>...
> Hi Dave,
>
> I played with this for a while and it *seems* to work okay. Not quite sure

that
> I understand your issue so I may have missed the point.
>
> In the code below, I insert the script into the head tag as a generic

control.
> I insert the onchange event as an attribute to the dropdown list control. The
> script is in place and is being called.
>
> Perhaps you can see if this is what you're after and if not, post some code
> that fails for you?
>
> Ken
> MVP [ASP.NET]
>
>
> Public Class jscrpt
> Inherits System.Web.UI.Page
>
> #Region " Web Form Designer Generated Code "
>
> 'This call is required by the Web Form Designer.
> <System.Diagnostics.DebuggerStepThrough()> Private Sub
> InitializeComponent()
>
> End Sub
> Protected WithEvents cboMyCombo1 As System.Web.UI.WebControls.DropDownList
> Protected Head1 As System.Web.UI.HtmlControls.HtmlGenericControl
>
> 'NOTE: The following placeholder declaration is required by the Web Form
> Designer.
> 'Do not delete or move it.
> Private designerPlaceholderDeclaration As System.Object
>
> Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles MyBase.Init
> 'CODEGEN: This method call is required by the Web Form Designer
> 'Do not modify it using the code editor.
> InitializeComponent()
> End Sub
>
> #End Region
>
> Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles MyBase.Load
> cboMyCombo1.Attributes.Add("onchange", "MyCombos_OnChange('1')")
> Head1.InnerHtml = " <script>function
> MyCombos_OnChange(strText){alert(strText);}</script>"
> End Sub
>
> End Class
>
>
> <%@ Page Language="vb" AutoEventWireup="false" Codebehind="jscrpt.aspx.vb"
> Inherits="p733workev.jscrpt"%>
> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
> <html>
> <head runat="server" id="head1">
> </head>
> <body ms_positioning="FlowLayout">
> <form id="Form1" method="post" runat="server">
> <p>
> <asp:dropdownlist id="cboMyCombo1" runat="server">
> <asp:listitem value="red">red</asp:listitem>
> <asp:listitem value="green">green</asp:listitem>
> <asp:listitem value="blue">blue</asp:listitem>
> </asp:dropdownlist></p>
> <p>
> </form>
> </P>
> </body>
> </html>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What is Server-Side Jscript (not Jscript.NET)? Maxwell2006 ASP .Net 5 03-07-2006 05:28 AM
DropDownList(ddlist) and TextBox(tbox) deepak ASP .Net Web Controls 1 12-12-2005 05:58 PM
simple question(ddlist and javascript) deepak kumar ASP .Net Web Controls 1 10-30-2004 05:13 AM
Porting from JScript to JScript.Net - compiler error Jon Maz ASP .Net 4 09-09-2004 10:24 AM
Which to Learn: Javascript, Jscript, JScript.NET, ECMA 262 (3rd ed)? John Bentley Javascript 14 02-05-2004 06:14 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57