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?

2
  • its not about error messages, just I am not getting any suggestion from the list while I write code in <script></script> tag because there is no such attribute in the list...@jadarnel27 Commented Jan 6, 2012 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. Commented Jan 6, 2012 at 15:05

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

6 Comments

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 =)
@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.
@jadarnel27 then he's doing something wrong, or it's not a <input type="text"> what he's trying to access or using the wrong id, my code works in any asp:TextBox and asp:DropDown control, tested!
it's easy to to debug @jchoudhury. Do a simple <h2><%= MyTextControlId.ClientID %></h2> and see what's written, then open the inspector and use that to write the code and retrieve what you want.
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.
|
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
});

Comments

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.

10 Comments

thanks. But, I have tried both single quote and double quote but still not working.
The syntax is correct (I used it in this answer: stackoverflow.com/questions/8736123/…) - 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. 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.
@DidierG. - That's a very good point. OP, are you handling the Javascript from an external file?
<div> <asp:TextBox ID="myText" runat="server" /> <asp:DropDownList ID="myList" runat="server" /> </div> I dont have any separate .js file.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.