PC Review


Reply
Thread Tools Rating: Thread Rating: 4 votes, 1.00 average.

Calling Javascript on a Dropdown list

 
 
=?Utf-8?B?Qw==?=
Guest
Posts: n/a
 
      2nd Feb 2007
I have a dropdown list as below.

I add an onchnage attribute in my codebehind to call some Javascript.

I want to get the selected text from my dropdown. What am I doing wrong below?

<aspropDownList ID="ddlStatus" runat="server" />

In my codebehind

ddlStatus.Attributes["onchange"] = "javascript:SetDateFields();";



<script language="javascript" type="text/javascript">

function SetDateFields()

{

var statusList = document.getElementById("ddlStatus");


var selectedStatus = statusList.options[statusList.selectedIndex].value;


}

</script>

 
Reply With Quote
 
 
 
 
=?UTF-8?B?R8O2cmFuIEFuZGVyc3Nvbg==?=
Guest
Posts: n/a
 
      2nd Feb 2007
C wrote:
> I have a dropdown list as below.
>
> I add an onchnage attribute in my codebehind to call some Javascript.
>
> I want to get the selected text from my dropdown. What am I doing wrong below?


Why do you think that you are doing something wrong? What happens when
you try to use the code? Do you get any error message? Have you enabled
Javascript error messages in the browser?

> <aspropDownList ID="ddlStatus" runat="server" />
>
> In my codebehind
>
> ddlStatus.Attributes["onchange"] = "javascript:SetDateFields();";


The "javascript:" protocol is only used when you put Javascript in an
URL. The only reason that you don't get an error message when you use it
elsewhere, is that it becomes a label instead.

ddlStatus.Attributes["onchange"] = "SetDateFields();";

> <script language="javascript" type="text/javascript">
>
> function SetDateFields()
>
> {
>
> var statusList = document.getElementById("ddlStatus");


Have you checked that this is the actual id that the element gets in the
html code?

> var selectedStatus = statusList.options[statusList.selectedIndex].value;


You are getting the value instead of the text.

> }
>
> </script>
>



--
Göran Andersson
_____
http://www.guffa.com
 
Reply With Quote
 
Laurent Bugnion [MVP]
Guest
Posts: n/a
 
      2nd Feb 2007
Hi,

C wrote:

<snip>

> <script language="javascript" type="text/javascript">


Additionally to Göran's remarks, the "language" attribute is deprecated.
You can safely remove it and use only the "type" attribute.

Greetings,
Laurent
--
Laurent Bugnion [MVP ASP.NET]
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
 
Reply With Quote
 
=?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
Guest
Posts: n/a
 
      2nd Feb 2007
As mentioned on the other forum, aside from the issues adressed by the
others, you are doing anything wrong. Try putting alert(selectedStatus) just
before the closing brace and you will see the value.
Peter

--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net




"C" wrote:

> I have a dropdown list as below.
>
> I add an onchnage attribute in my codebehind to call some Javascript.
>
> I want to get the selected text from my dropdown. What am I doing wrong below?
>
> <aspropDownList ID="ddlStatus" runat="server" />
>
> In my codebehind
>
> ddlStatus.Attributes["onchange"] = "javascript:SetDateFields();";
>
>
>
> <script language="javascript" type="text/javascript">
>
> function SetDateFields()
>
> {
>
> var statusList = document.getElementById("ddlStatus");
>
>
> var selectedStatus = statusList.options[statusList.selectedIndex].value;
>
>
> }
>
> </script>
>

 
Reply With Quote
 
=?Utf-8?B?TWlsb3N6IFNrYWxlY2tpIFtNQ0FEXQ==?=
Guest
Posts: n/a
 
      2nd Feb 2007
Hi there,

In addition to Goran's reply, please find working snippet below:

-- begin aspx code --

<aspropDownList ID="ddlStatus" runat="server"
AutoPostBack="false" OnChange="SetDateFields()">
<asp:ListItem Text="Status1" Value="Value1"/>
<asp:ListItem Text="Status2" Value="Value2"/>
<asp:ListItem Text="Status3" Value="Value3"/>
</aspropDownList>

<script type="text/javascript">
function SetDateFields()
{
var statusList = document.getElementById('<%=ddlStatus.ClientID %>');
var selectedStatus = statusList.options[statusList.selectedIndex].text;

alert('selected status is : ' + selectedStatus);
}
</script>
-- end aspx code --

Note you have to use control.ClientID to get the valid ID (ClientID contains
all the parent controls ids - if i didnt use it and moved dropdown list to a
Panel control it'd stop work). You may be wondering why i put OnChange
attribute to drop down list declaration. This is allowed (even if you get a
warning from schema validation) and it's called 'expando'.

Hope it's clear now


Milosz


"C" wrote:

> I have a dropdown list as below.
>
> I add an onchnage attribute in my codebehind to call some Javascript.
>
> I want to get the selected text from my dropdown. What am I doing wrong below?
>
> <aspropDownList ID="ddlStatus" runat="server" />
>
> In my codebehind
>
> ddlStatus.Attributes["onchange"] = "javascript:SetDateFields();";
>
>
>
> <script language="javascript" type="text/javascript">
>
> function SetDateFields()
>
> {
>
> var statusList = document.getElementById("ddlStatus");
>
>
> var selectedStatus = statusList.options[statusList.selectedIndex].value;
>
>
> }
>
> </script>
>

 
Reply With Quote
 
=?Utf-8?B?TWlsb3N6IFNrYWxlY2tpIFtNQ0FEXQ==?=
Guest
Posts: n/a
 
      2nd Feb 2007
Hi there,

In addition to Goran's reply, please find working snippet below:

-- begin aspx code --

<aspropDownList ID="ddlStatus" runat="server"
AutoPostBack="false" OnChange="SetDateFields()">
<asp:ListItem Text="Status1" Value="Value1"/>
<asp:ListItem Text="Status2" Value="Value2"/>
<asp:ListItem Text="Status3" Value="Value3"/>
</aspropDownList>

<script type="text/javascript">
function SetDateFields()
{
var statusList = document.getElementById('<%=ddlStatus.ClientID %>');
var selectedStatus = statusList.options[statusList.selectedIndex].text;

alert('selected status is : ' + selectedStatus);
}
</script>
-- end aspx code --

Note you have to use control.ClientID to get the valid ID (ClientID contains
all the parent controls ids - if i didnt use it and moved dropdown list to a
Panel control it'd stop work). You may be wondering why i put OnChange
attribute to drop down list declaration. This is allowed (even if you get a
warning from schema validation) and it's called 'expando'.

Hope it's clear now


Milosz


"C" wrote:

> I have a dropdown list as below.
>
> I add an onchnage attribute in my codebehind to call some Javascript.
>
> I want to get the selected text from my dropdown. What am I doing wrong below?
>
> <aspropDownList ID="ddlStatus" runat="server" />
>
> In my codebehind
>
> ddlStatus.Attributes["onchange"] = "javascript:SetDateFields();";
>
>
>
> <script language="javascript" type="text/javascript">
>
> function SetDateFields()
>
> {
>
> var statusList = document.getElementById("ddlStatus");
>
>
> var selectedStatus = statusList.options[statusList.selectedIndex].value;
>
>
> }
>
> </script>
>

 
Reply With Quote
 
Bhaskardeep Khaund
Guest
Posts: n/a
 
      3rd Feb 2007
Hi,

U can acheive that by writing

ddlStatus.Attributes.Add("onchange","javascript:SetDateFields()")


Bhaskar

"C" <(E-Mail Removed)> wrote in message
news:59EC5CBB-DCB5-478E-A8CD-(E-Mail Removed)...
>I have a dropdown list as below.
>
> I add an onchnage attribute in my codebehind to call some Javascript.
>
> I want to get the selected text from my dropdown. What am I doing wrong
> below?
>
> <aspropDownList ID="ddlStatus" runat="server" />
>
> In my codebehind
>
> ddlStatus.Attributes["onchange"] = "javascript:SetDateFields();";
>
>
>
> <script language="javascript" type="text/javascript">
>
> function SetDateFields()
>
> {
>
> var statusList = document.getElementById("ddlStatus");
>
>
> var selectedStatus = statusList.options[statusList.selectedIndex].value;
>
>
> }
>
> </script>
>



 
Reply With Quote
 
Laurent Bugnion [MVP]
Guest
Posts: n/a
 
      3rd Feb 2007
Hi,

Bhaskardeep Khaund wrote:
> Hi,
>
> U can acheive that by writing
>
> ddlStatus.Attributes.Add("onchange","javascript:SetDateFields()")


This is the same as what the OP did

ddlStatus.Attributes["onchange"] = "javascript:SetDateFields();";

Additionally, had you read previous answers, you would have seen that
you should never use "javascript:", except in very very specific cases.

HTH,
Laurent
--
Laurent Bugnion [MVP ASP.NET]
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

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
Re: I need a date dropdown list and a time dropdown list Ken Slovak - [MVP - Outlook] Microsoft Outlook VBA Programming 0 25th Feb 2009 02:56 PM
Get Text from dropdown list using javascript moondaddy Microsoft ASP .NET 2 24th Nov 2008 07:41 PM
result of selecting from the dropdown list should be a dropdown list No News Microsoft Excel Worksheet Functions 1 1st Jul 2006 09:20 AM
how to make dropdown list items filled by Javascript persisting on a page postback? lucy Microsoft ASP .NET 0 26th Apr 2006 05:50 PM
Help a newbie: Populating dropdown lists on a form using data from anothr dropdown list DAVID Microsoft Access 1 14th Jun 2005 12:21 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:18 PM.