0

Sorry for the basic silly question. But I really dont have any idea on how to solve this. I have ASP controls (One TextBox and One DropDownList). Now I need to access value property of TextBox as well as SelectedIndex property of DropDownList. This should be as simple as:

For TextBox:

document.getElementById("<%= myControl.ClientID >").value

For DropDownList:

document.getElementById("<%= myControl.ClientID %>").selectedindex

But in Visual Studio 2008, while I am writing these codes, it does not work. I can not find any value or selectedindex attribute for the respective control. I am not getting this, because in most forums and tutorials, they suggested in a simple way I mentioned.

HTML Code:

<div> <asp:TextBox ID="myText" runat="server" /> <asp:DropDownList ID="myList" runat="server" /> </div> 

Is there any other way or I am just missing something here?

  • its not about error messages, just I am not getting any suggestion from the list while I write code in tag because there is no such attribute in the list...@jadarnel27 –  Jan 06 '12 at 14:51
  • 1
    You will never get total intellisense from VisualStudio on non compiled languages if you don't properly use the correct `.vsdoc`. Then again, I already wrote how you need to get the value from your dropdown, a little search on Google shows you exactly that. – balexandre Jan 06 '12 at 15:05

3 Answers3

1

first of all, jQuery helps you a lot writting javascript, you shoudl use this path.

To answer your question, you need to use MyControlId.ClientID and not only MyControlId

for example:

var t = document.getElementById('<%= myTextBoxID.ClientID %>').value;
alert(t);  // textbox value

var d = document.getElementById('<%= myDropDownID.ClientID %>'),
    dSelected = d.options[d.selectedIndex].value;
alert(dSelected); // dropdown value

with jQuery this would simply be:

var t = $('#<%= myTextBoxID.ClientID %>').val(),   // for your textbox
    d = $('#<%= myDropDownID.ClientID %>').val();  // for your selectbox 
alert(t ' --> ' + d);

Seams, from the comments that you still have no luck, then the problem must be in the control or the name itself, try this to debug the problem:

let's imagine that you have:

<asp:TextBox runat="server" id="MyTextBox" Text="Hello" />

write right below that line this:

<h2><%= MyTextBox.ClientID %></h2>

and open the inspector of your browser (Firebug or IE Dev tools) and run the javascript to see if you get the Hello string, like this:

Note: I used MyTextBox, please change it to the name that you got inside that <h2> tag

enter image description here

balexandre
  • 73,608
  • 45
  • 233
  • 342
  • 3
    He ninja-edited `.ClientID` into the question, so he is using that. Also...there is absolutely no need for jQuery just to get the ID of a control. Seems a bit excessive =) – Josh Darnell Jan 06 '12 at 14:37
  • @jadarnel27 you will never, ever "JUST" get the value of a control, you will start to do much more.. and if you take the jQuery way since the start, all will be better at the end. And, you do have both plain javascript and jQuery in the answer, just choose what do you prefer, I will **always** stick to jQuery. – balexandre Jan 06 '12 at 14:40
  • @jadarnel27 then he's doing something wrong, or it's not a `` what he's trying to access or using the wrong id, my code works in any `asp:TextBox` and `asp:DropDown` control, tested! – balexandre Jan 06 '12 at 14:47
  • it's easy to to debug @jchoudhury. Do a simple `

    <%= MyTextControlId.ClientID %>

    ` and see what's written, then open the inspector and use that to write the code and retrieve what you want.
    – balexandre Jan 06 '12 at 14:49
  • 1
    Bottom line, you'd better write a mix of pure javascript and jQuery in order to have efficient code. Getting a value does not require building a jQuery object and call .val() if it can be done in one line of js. – Didier Ghys Jan 06 '12 at 15:00
  • @DidierG. He says he has a DropDown as well, do that in one line of code in plain javascript comparing to what you get in jQuery then... – balexandre Jan 06 '12 at 15:02
0

Personally I'd use jQuery instead of standard JavaScript and then have a look at this question for how to find your element with it.

edit: if you haven't used jQuery before then make sure you wrap your code in its ready block otherwise it might not find your element:

$(document).ready(function() {
  // work with your element ID here
});
Community
  • 1
  • 1
akiller
  • 2,462
  • 22
  • 30
0

Try using a single punctuation mark.

document.getElementById('<%= myTextBox.ClientID %>').value 

A couple of answers recommend jQuery but I think you should only use it if you need it. If you're only getting values from elements, then there shouldn't be a need to introduce a new library to your website. If you plan to use quite a bit of Javascript, animations, etc.. then jQuery would make sense.

keyboardP
  • 68,824
  • 13
  • 156
  • 205
  • thanks. But, I have tried both single quote and double quote but still not working. –  Jan 06 '12 at 14:44
  • 1
    The syntax is correct (I used it in this answer: http://stackoverflow.com/questions/8736123/show-message-when-start-typing-inside-textbox-using-javascript/8736188#8736188) - There's probably something else wrong in your Javascript code which is preventing execution. If you replace the link above with an `alert`, does the `alert` get called? Also, could you post more of your markup/JS? – keyboardP Jan 06 '12 at 14:46
  • 1
    @keyboardP. The problem is that if you have your javascript in seperate .js file, `<%...%>` does not get parsed and everything breaks, but I agree with you. – Didier Ghys Jan 06 '12 at 14:47
  • @DidierG. - That's a very good point. OP, are you handling the Javascript from an external file? – keyboardP Jan 06 '12 at 14:49
  • I dont have any separate .js file. –  Jan 06 '12 at 14:55
  • `var theTextBox = document.getElementById('<%= myText.ClientID %>')` should work unless, like Didier G. pointed out, your Javascript is in an external file. Is it? – keyboardP Jan 06 '12 at 14:57
  • Then there's something wrong with your code. Can you show more of your Javascript? (I assume you're using the above code within the ` – keyboardP Jan 06 '12 at 15:10
  • @keyboardP –  Jan 07 '12 at 03:04
  • Replace the line with an alert. Does it get called? – keyboardP Jan 07 '12 at 03:08
  • yes, function is called but I can't get the value of the ASP control (TextBox) like wise –  Jan 07 '12 at 12:50