Go Back   CodingForums.com > :: Server side development > ASP.NET

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rating: Thread Rating: 45 votes, 3.76 average.
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-22-2005, 04:29 PM   PM User | #1
gzieve
New Coder

 
Join Date: Jan 2003
Location: Philly, PA
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
gzieve is an unknown quantity at this point
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 04:58 PM.. Reason: clarification
gzieve is offline   Reply With Quote
Old 09-22-2005, 05:02 PM   PM User | #2
nikkiH
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
nikkiH is on a distinguished road
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/
nikkiH is offline   Reply With Quote
Old 09-22-2005, 05:30 PM   PM User | #3
gzieve
New Coder

 
Join Date: Jan 2003
Location: Philly, PA
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
gzieve is an unknown quantity at this point
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>
gzieve is offline   Reply With Quote
Old 09-22-2005, 05:38 PM   PM User | #4
nikkiH
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
nikkiH is on a distinguished road
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/
nikkiH is offline   Reply With Quote
Old 09-22-2005, 05:46 PM   PM User | #5
gzieve
New Coder

 
Join Date: Jan 2003
Location: Philly, PA
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
gzieve is an unknown quantity at this point
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!
gzieve is offline   Reply With Quote
Old 09-22-2005, 07:10 PM   PM User | #6
nikkiH
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
nikkiH is on a distinguished road
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/
nikkiH is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


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

Forum Jump


All times are GMT +1. The time now is 07:30 PM.


Advertisement
Log in to turn off these ads.