Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

passing DropDownListValue 2

Status
Not open for further replies.

AT76

Technical User
Apr 14, 2005
460
0
0
US
Hi,

I have an asp DropDownList and I'm trying to pass its selected value to javascript.

Like this:

Code:
this.DropDownList.Attributes.Add("onclick", "showDiv(this)");

I then have a js file that tries to get that value selected:

Code:
var i = null;

function showDiv(obj)
{
    i = obj.options[obj.selectedIndex].value;
}

But the value selected is not getting passed. Could someone help me figure out why?

Thank you!
 
your dropdownlist will require a value for this to work. if you only supply text it won't work. you could also try using the onchange event

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Thank you both.

Jmeckley, I'm currently only supplying text to the DropDownList. I will try the 'onchange' event.

Question:

Is this OK?
Code:
<asp:DropDownList ID="DropDownList" runat="server" BackColor="White" AutoPostBack="true" />

Or do I need to add an "OnTextChanged" or "OnSelectedIndexChanged" event?

I'm having a hard time understanding what gets passed as I can't currently step through the JS code. Does the index (0,1,2) gets passed, or does the selected text from the DropDownList?

Currently when I run this code I get the following error from IE: "'Obj' is undefined".

Does this make sense?? Thanks for your help Jmeckley!

 
I'm after opening a specific popup window (.htm) depending on the item selected in the DropDownList.
 
Try this:
Code:
    <script type="text/javascript">
    function showDiv(obj)
        {
            i = obj.options[obj.selectedIndex].value;
            window.open(i+'.html');
        }
    </script>
Code:
        <asp:DropDownList ID="DropDownList1" runat="server" onchange="showDiv(this);">
            <asp:ListItem Value="1" Text="1"></asp:ListItem>
            <asp:ListItem Value="2" Text="2"></asp:ListItem>
            <asp:ListItem Value="3" Text="3"></asp:ListItem>                        
        </asp:DropDownList>


____________________________________________________________
Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]

Need help finding an answer? Try the Search Facility or read FAQ222-2244.
 
Thank you ca8msm. Your example help me understand how it works. I'm now able to open the desired htm based on the selection chosen in the DropDownList. However, I'm unable to launch the code on DropDownList1_SelectedIndexChanged event. This fires but only when I click a submit button.

Here's how I have my DropDownList1 setup:
Code:
<asp:DropDownList ID="DropDownList1" runat="server"  BackColor="White" Visible="False" AutoPostBack="True" OnSelectedIndexChanged="DropDownList3_SelectedIndexChanged" OnTextChanged="DropDownList3_SelectedIndexChanged"/>

I have read online that AutoPostBack must be set to true. I did that but the code is not launching when I select an item in my DropDownList. Any ideas as to why this is?

 
If you want server side code to fire as well, you will have to set the AutoPostBack property like you have done. You can then add the event to your code behind page and use the handles method to tell the event when it should fire e.g.
Code:
    Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) [b]Handles DropDownList1.SelectedIndexChanged[/b]

    End Sub


____________________________________________________________
Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]

Need help finding an answer? Try the Search Facility or read FAQ222-2244.
 
Hi ca8msm, I did that. The problem is that I'm able to hit code in the DropDownList1_SelectedIndexChanged event but only when I hit the submit button. If I just switch items the event wont fire.
 
Did you use the name for the sub that you speicified in the HTML DropDownList3_SelectedIndexChanged
Or did you just cut and paste Mark's code which will not work if you used the name above. You will have to change it to
Code:
OnSelectedIndexChanged="DropDownList[b]1[/b]_SelectedIndexChanged"
 
Hi Jim,

I actually have it set up for: DropDownList3_SelectedIndexChanged

But it doesn't fire. Just when I click submit.
 
I understand my issue now. I had a JS funtion that was overriding my call. Thanks to all!!!!

You guys are awesome!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top