Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Jan 2003
    Location
    Philly, PA
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    asp:dropdownlist getting value with javascript?

    Is it possible to reference the .NET asp dropdownlist value and text using javascript on the html page?

    I was trying all sorts of variations of things like this w/o luck. Thanks.

    var strUser = document.getElementById("dropdown1").value;
    var strUser = document.all(dropdown1).children[0].text;
    Last edited by gzieve; 09-22-2005 at 05:58 PM. Reason: clarification

  • #2
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts
    It's a select, not a textbox. Yours would have done okay with a textbox, hidden, or textarea. For select value, you need to use

    var e = document.getElementById("dropdown1"); // select element
    var strUser = e.options[e.selectedIndex].value;

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/

  • #3
    New Coder
    Join Date
    Jan 2003
    Location
    Philly, PA
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks...but it's still not getting it. I placed an alert after the first line, and it returns null. Than it bombs out on the bottom line. It's a databound listboxe, don't know if that makes a difference? It's bound on pageload.

    var e = document.getElementById("lstUser"); // select element
    alert(e);
    var strUser = e.options[e.selectedIndex].value;

    This is the listbox:
    <asp:dropdownlist id="lstUser" runat="server" Height="16px" Width="216px" BackColor="#C0C0FF"></asp:dropdownlist>

  • #4
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts
    Ah, yeah, .net changes control names for some things. I'm still learning about that.
    You need to register a script block server side and use control.ClientID.

    Here's what I use in my datagrid_edit command handler to set focus.
    Code:
    		// set focus to textbox so people don't have to scroll
    			TextBox txtBox = (TextBox) this.dgRptSpecs.Items[e.Item.ItemIndex].Cells[1].FindControl("rptNameTxt");
    			RegisterStartupScript("focus", "<script type=\"text/javascript\">\n"+
    				"Form1." + txtBox.ClientID + ".focus();\n" +
    				"<" + "/script>");

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/

  • #5
    New Coder
    Join Date
    Jan 2003
    Location
    Philly, PA
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ahhh.....I remember this from before now. .NET changes the client ID. I did vew source on the web page and see that the ID has been changed to "_ctl0_lstUser". When I plugged that in, it works fine.

    Thanks for jogging my memory!

  • #6
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts
    No problem, but be careful.
    It may be dynamically assigned an a per run basis. I'm not sure. I'd check into that before counting on it.

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •