dynamically generate script block and attach to event

N

Nikhil Patel

Hi all,
Is it possible to add two lines of javascript code from code behind at
runtime and link it to a DropDown control's SelectedIndexChanged event?

Thanks.
-Nikhil
 
G

Guest

Hi Nikhil,

You can insert the client-side code from the code-behind and add an
attribute to the DDL. Here's some code, let us know if it helps?

Ken
Microsoft MVP [ASP.NET]
Toronto

<%@ Page Language="VB" %>
<script runat="server">

Sub Page_Load
Page.RegisterClientScriptBlock("hndlr","<scr" + "ipt
language=JavaScript>function change_handler(){alert('Got a change!');return
false;}</sc" + "ript>")
DropDownList1.attributes.add("onchange","change_handler()")
end sub
</script>
<html>
<head>
</head>
<body>
<form runat="server">
<asp:DropDownList id="DropDownList1" 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>
</form>
</body>
</html>
 
N

Nikhil Patel

Hi Ken,
Thanks for your reply. Will I have to add this scriptblock in the
postback of the page also or just when the page is loading for the first
time?
Thanks again.

Ken Cox said:
Hi Nikhil,

You can insert the client-side code from the code-behind and add an
attribute to the DDL. Here's some code, let us know if it helps?

Ken
Microsoft MVP [ASP.NET]
Toronto

<%@ Page Language="VB" %>
<script runat="server">

Sub Page_Load
Page.RegisterClientScriptBlock("hndlr","<scr" + "ipt
language=JavaScript>function change_handler(){alert('Got a
change!');return
false;}</sc" + "ript>")
DropDownList1.attributes.add("onchange","change_handler()")
end sub
</script>
<html>
<head>
</head>
<body>
<form runat="server">
<asp:DropDownList id="DropDownList1" 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>
</form>
</body>
</html>


Nikhil Patel said:
Hi all,
Is it possible to add two lines of javascript code from code behind at
runtime and link it to a DropDown control's SelectedIndexChanged event?

Thanks.
-Nikhil
 
K

Ken Cox [Microsoft MVP]

Hi Nikhil,

Each time unless you do something different with it like adding it into a
Literal control.

Nikhil Patel said:
Hi Ken,
Thanks for your reply. Will I have to add this scriptblock in the
postback of the page also or just when the page is loading for the first
time?
Thanks again.

Ken Cox said:
Hi Nikhil,

You can insert the client-side code from the code-behind and add an
attribute to the DDL. Here's some code, let us know if it helps?

Ken
Microsoft MVP [ASP.NET]
Toronto

<%@ Page Language="VB" %>
<script runat="server">

Sub Page_Load
Page.RegisterClientScriptBlock("hndlr","<scr" + "ipt
language=JavaScript>function change_handler(){alert('Got a
change!');return
false;}</sc" + "ript>")
DropDownList1.attributes.add("onchange","change_handler()")
end sub
</script>
<html>
<head>
</head>
<body>
<form runat="server">
<asp:DropDownList id="DropDownList1" 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>
</form>
</body>
</html>


Nikhil Patel said:
Hi all,
Is it possible to add two lines of javascript code from code behind
at
runtime and link it to a DropDown control's SelectedIndexChanged event?

Thanks.
-Nikhil
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top