473,378 Members | 1,403 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

javascript handling of selectedItemIndexChanged?

34
If the user of my site changes their selection in a dropdownlist, I would like to handle that client side and based on what they chose it would populate a text box with different text.

I can figure out how to do this server side no problem, but how do I get to do this in javascript.

I have something like:
Expand|Select|Wrap|Line Numbers
  1.     <asp:DropDownList ID="StatusDropDownList" runat="server"  >
  2.         <asp:ListItem Selected="True" Text="Released" Value="Released">Released</asp:ListItem>
  3.         <asp:ListItem Text="Down" Value="Down">Down</asp:ListItem>
  4.         <asp:ListItem Text="Blocked" Value="Blocked">Blocked</asp:ListItem>
  5.  
  6.     </asp:DropDownList>
I have an text box that I want to populate based on what the user selects and I would like to do this in javascript...I cannot seem to get that function to work.

any suggestions?
thanks.
Aug 7 '07 #1
12 1398
pbmods
5,821 Expert 4TB
Heya, JLC.

Please use CODE tags when posting source code. See the REPLY GUIDELINES on the right side of the page next time you post.
Aug 7 '07 #2
JLC
34
I see that now...okay will do...thanks.
Aug 7 '07 #3
pbmods
5,821 Expert 4TB
Heya, JLC.

To get the client and the server talking like that, you'll need to use AJAX.

How familiar are you with AJAX?
Aug 7 '07 #4
JLC
34
Hi...

I am not familiar at all with ajax.

I can't write a javascript that changes the text in a text box based on the selection of a dropdownlist?

Seems like something I might do alot...but I am still learning so maybe that seems ridiculous! :)

It was easier just dealing with it in C# but I am told for this project I am not allowed to do it that way.

is AJAX difficult? do I really need to learn AJAX to accomplish this task?
thanks
Aug 7 '07 #5
pbmods
5,821 Expert 4TB
Heya, JLC.

I suppose AJAX is not the only tool for the job. It's probably the best in the long term, but let's try this instead:
  • On the server side, generate the code to create a JavaScript variable for each drop down element. I don't know how you would do that using ASP, but here's how you might do it in PHP:
    Expand|Select|Wrap|Line Numbers
    1. echo '
    2. <script type="text/javascript">
    3.     var $dropdown = {
    4.         "Down": "' . $whatShouldGoInTheTextBoxWhenTheUserPicksDown . '",
    5.         "Blocked": "' . $whatShouldGoInTheTextBoxWhenTheUserPicksBlocked . '",
    6.         etc.
    7.     };
    8. </script>';
    9.  
  • When your script executes, this is an example of what gets sent to the browser:
    Expand|Select|Wrap|Line Numbers
    1. <script type="text/javascript">
    2.     var $dropdown = {
    3.         "Down": "Some down stuff here!",
    4.         "Blocked": "Some blocked stuff here!"
    5.     };
    6. </script>
    7.  
  • The browser will parse this and create the JavaScript variable called $dropdown. It is important to remember that PHP (or ASP) does NOT directly set the value of the JavaScript variable! This is a VERY common misconception, and anybody who is reading this should be aware of that!
  • Then include the JavaScript on the page that looks for that variable when the value of the select changes:
    Expand|Select|Wrap|Line Numbers
    1. <select ... onchange="document.getElementById('theTextBoxID').value = $dropdown[this.options[this.selectedIndex].value];">
    2.  

For example, if the User were to pick the 'Down' option, then the Javascript would attempt to set the value of the text box to $dropdown['Down'], or "Some down stuff here!".
Aug 7 '07 #6
JLC
34
Ok pbmods...I couldn't quite get it to work from what you were saying. I tried some other things here...but still not working. Can you take a look at this and see if you see anything horribly wrong?
Thanks.

Expand|Select|Wrap|Line Numbers
  1. <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
  2.  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4.  
  5. <html xmlns="http://www.w3.org/1999/xhtml" >
  6. <head runat="server">
  7.     <title>Untitled Page</title>
  8.  
  9.     <script type="text/javascript">
  10.         function EmailChangedText(ddid)
  11.         {
  12.             var selected = ddid.options[ddid.selectedIndex].value;
  13.             Text1.Value = selected;
  14.  
  15.  
  16.         }
  17.     </script>
  18. </head>
  19. <body>
  20.     <form id="form1" runat="server">
  21.     <div>
  22.         <asp:DropDownList ID="DropDownList1" runat="server">
  23.              <asp:ListItem Selected="true" Text="Down" Value="Down">Down</asp:ListItem>
  24.              <asp:ListItem Text="Deploying" Value="Deploying">Deploying</asp:ListItem>
  25.              <asp:ListItem Text="Running" Value="Running">Running</asp:ListItem>
  26.         </asp:DropDownList>
  27.         <input id="Text1" type="text" style="width: 490px; height: 268px" />
  28.  
  29.  
  30.         </div>
  31.     </form>
  32. </body>
  33. </html>
  34.  
  35.  

and then in my .cs file

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Web;
  5. using System.Web.Security;
  6. using System.Web.UI;
  7. using System.Web.UI.WebControls;
  8. using System.Web.UI.WebControls.WebParts;
  9. using System.Web.UI.HtmlControls;
  10.  
  11. public partial class _Default : System.Web.UI.Page 
  12. {
  13.     protected void Page_Load(object sender, EventArgs e)
  14.     {
  15.         DropDownList1.Attributes.Add("OnChange", "javascript: EmailChangedText(" + DropDownList1.ClientID + ")");
  16.  
  17.     }
  18. }
  19.  
  20. why wouldn't this just populate my text box with what the selection is in the drop down?
  21.  
  22.  
  23.  
  24.  
  25.  


Heya, JLC.

I suppose AJAX is not the only tool for the job. It's probably the best in the long term, but let's try this instead:
  • On the server side, generate the code to create a JavaScript variable for each drop down element. I don't know how you would do that using ASP, but here's how you might do it in PHP:
    Expand|Select|Wrap|Line Numbers
    1. echo '
    2. <script type="text/javascript">
    3.     var $dropdown = {
    4.         "Down": "' . $whatShouldGoInTheTextBoxWhenTheUserPicksDown . '",
    5.         "Blocked": "' . $whatShouldGoInTheTextBoxWhenTheUserPicksBlocked . '",
    6.         etc.
    7.     };
    8. </script>';
    9.  
  • When your script executes, this is an example of what gets sent to the browser:
    Expand|Select|Wrap|Line Numbers
    1. <script type="text/javascript">
    2.     var $dropdown = {
    3.         "Down": "Some down stuff here!",
    4.         "Blocked": "Some blocked stuff here!"
    5.     };
    6. </script>
    7.  
  • The browser will parse this and create the JavaScript variable called $dropdown. It is important to remember that PHP (or ASP) does NOT directly set the value of the JavaScript variable! This is a VERY common misconception, and anybody who is reading this should be aware of that!
  • Then include the JavaScript on the page that looks for that variable when the value of the select changes:
    Expand|Select|Wrap|Line Numbers
    1. <select ... onchange="document.getElementById('theTextBoxID').value = $dropdown[this.options[this.selectedIndex].value];">
    2.  

For example, if the User were to pick the 'Down' option, then the Javascript would attempt to set the value of the text box to $dropdown['Down'], or "Some down stuff here!".
Aug 8 '07 #7
pbmods
5,821 Expert 4TB
Heya, JLC.

Horribly wrong? You mean ASIDE from the fact that you're programming in d-flat? Just kidding :P

The idea is to output JavaScript that stores the values that you want to put in the textbox into a variable.
Aug 8 '07 #8
JLC
34
OK, well I got it working but only in this way

when I choose an item from the drop down list, it will display in an alert.

What I want to do is instead of displaying in an alert, I want it to display in my asp:textbox.

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2.     function EmailChangedText(ddid)
  3.     {
  4.         var selected = ddid.options[ddid.selectedIndex].value;
  5.  
  6.         alert(selected);   <--this works
  7.         EmailTextBox.Value = selected; <--this does not but I want it to! :)
  8.     }
  9.  
  10. </script>
  11.  
Heya, JLC.

Horribly wrong? You mean ASIDE from the fact that you're programming in d-flat? Just kidding :P

The idea is to output JavaScript that stores the values that you want to put in the textbox into a variable.
Aug 8 '07 #9
pbmods
5,821 Expert 4TB
Heya, JLC.

I'm going to go ahead and move this thread to the ASP forum, where our resident Experts will be better able to help you out.
Aug 8 '07 #10
JLC
34
ok thanks for all the help
I appreciate it...

Heya, JLC.

I'm going to go ahead and move this thread to the ASP forum, where our resident Experts will be better able to help you out.
Aug 8 '07 #11
jhardman
3,406 Expert 2GB
sorry, I'm passing the buck too. This belongs in .NET

Jared, moderator
asp forum
Aug 8 '07 #12
Frinavale
9,735 Expert Mod 8TB
sorry, I'm passing the buck too. This belongs in .NET

Jared, moderator
asp forum

This problem has been solved in this thread

Cheers!

-Frinny
Aug 9 '07 #13

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

Similar topics

6
by: Yvan J. Gagnon | last post by:
I am currenly developing a web site using Macromedia fireworks, and am trying to figure out a way (through hand-coding) of attaching a javascript function (onClick="doit=false") to each of the...
72
by: Stephen Poley | last post by:
I have quite often (as have probably many of you) come across HTML forms with irritating bits of Javascript attached. The last straw on this particular camel's back was a large form I was asked to...
25
by: delerious | last post by:
I see some code examples like this: <DIV onmouseover="this.style.background='blue'"> and other code examples like this: <DIV onmouseover="javascript:this.style.background='blue'"> Which...
34
by: electrician | last post by:
Perl has it, Basic has it, Fortran has it. What is so difficult about creating a goto command for JavaScript. Just set up a label and say go to it.
2
by: xander.zone.3x | last post by:
How can we download data in tables in a html page into a csv file using client side Javascript. I should click a button and a table's data should be downloaded into a csv in local drive. The os...
12
by: pantagruel | last post by:
Hi, I'm thinking of making a WScript based JavaScript library, I can think of some specific non-browser specific scripting examples that should probably make it in, like Crockford's little...
19
by: maya | last post by:
hi, so what is "modern" javascript?? the same as "DOM-scripting"? i.e., editing content (or changing appearance of content) dynamically by massaging javascript objects, html elements, etc? ...
1
Frinavale
by: Frinavale | last post by:
Introduction I've seen many questions asked about how to disable the browser's back button and in the past I've replied with "it's simply not possible". It's not a good idea to disable the back...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.