Link to home
Start Free TrialLog in
Avatar of pucko73
pucko73

asked on

Find textbox inside formview in javascript

Can someone help me out how to find a textbox and set the value in JavaScript.

I get it to work on IE 11.0 but it fails on IE 8

var textbox = document.getElementById('<%=formView1.FindControl("tbSomething").ClientID %>');
var comments = textbox.value;


On IE8 textbox is null here.....
Avatar of leakim971
leakim971
Flag of Guadeloupe image

What do you get on the screen with the following :
window.onload = function() {
         var id = '<%=formView1.FindControl("tbSomething").ClientID %>';
         alert("the ID is :" + id); // for testing purpose
         var textbox = document.getElementById(id);
         alert("textbox " + (textbox==null)?"not found":"found" ); // for testing purpose
         var comments = textbox.value;
         alert(comments);
}

Open in new window

Avatar of pucko73
pucko73

ASKER

the id is ct100_ContentPlaceHolder1_formView1_tbSomething
not found
empty dialog
and on IE 11?
could you confirm when you see the first alert you can see the textbox in both cases?
Avatar of pucko73

ASKER

I get the same result from both... It's just that I dont get an exception in the javacode for the script code on IE 11 it seems like.

On 11 i can se the textbox when I get the message with the id.  (but i still get not found)

on 8 I can see the textbox when the dialog with the id is open.
ok, do a right to the page and choose view source on both.
try to locate the textbox and confirm the ID is the right one. Should not...
Avatar of pucko73

ASKER

If i don't put this in the onoad function, just add the alert rows to my function that was called in a onclientclick of a button I get on 11:

id as above
not found
some text  (ie the content of textbox... so I get the value here......


and on 8 I get an exception:

0x800a138f - JavaScript runtime error: Unable to get property 'value' of undefined or null reference....
Validate your page here : http://validator.w3.org/
Avatar of pucko73

ASKER

yes I can se the id  ctl00_ContentPlaceHolder1_formView1_tbSomething in view source on both.
Avatar of pucko73

ASKER

Could it have something to do with it beeing a multiline textobox

I can se that view source show it as <textarea
the problem is when you try to read the data
you muts put the provided script alone in its own script tag (outside of any function) :
<script>
window.onload = function() {
         var id = '<%=formView1.FindControl("tbSomething").ClientID %>';
         alert("the ID is :" + id); // for testing purpose
         var textbox = document.getElementById(id);
         alert("textbox " + (textbox==null)?"not found":"found" ); // for testing purpose
         var comments = textbox.value;
         alert(comments);
}
</script>

Open in new window

Avatar of pucko73

ASKER

Not much output from that.

Some warnings about:
The border attribute is obsolete. Consider specifying img { border: 0; } in CSS instead.

and

An img element must have an alt attribute, except under certain conditions. For details, consult guidance on providing text alternatives for images.

And:
 The cellpadding attribute on the table element is obsolete. Use CSS instead.
Should not affect this.
great. hope you got my previous comment too.
Avatar of pucko73

ASKER

I'm not sure I understand what you mean with your previous  comment?


The strange thing is when I debug it seems like the textbox is assigned  and I can see the properties of it   (running your onload function at least)
and it still say not found.....
any link to see your page?
Avatar of pucko73

ASKER

Unfortunately not.  I don't have any public server to put on :(
What about this (wait 10s before the alerts)  :

<script>
window.onload = function() {

setTimeout(function() {
         var id = '<%=formView1.FindControl("tbSomething").ClientID %>';
         alert("the ID is :" + id); // for testing purpose
         var textbox = document.getElementById(id);
         alert("textbox " + (textbox==null)?"not found":"found" ); // for testing purpose
         var comments = textbox.value;
         alert(comments);
},10000);

}
</script>

Open in new window

Avatar of pucko73

ASKER

still got the same.

id correct
not found
empty
Avatar of pucko73

ASKER

the not found thing is strange. because if there is something in the textbox
I get

id correct
not found
"content of textbox"
Avatar of pucko73

ASKER

So i guess that it works with your code in onload.... but when I have it in my code
it fails.


I call a function on a button OnClientClick

and that function calls anoter function that try to access the textbox
Avatar of pucko73

ASKER

I also notice that on ie8 when I press my button the page don't show the TextBox after I pressed the button....

The first function that I calll shows a dialog where I can select some values... And whan I Close that dialog the function that try to read from textbox are called.
Avatar of pucko73

ASKER

I solved my problem by:

1. Change the function that I call in onClientClick on the button
to fetch the value from the combobox, then I pass that as a parameter to the second function I call.

So I guess that the problem is that the textbox in the page seems to disapear when I
show Another dialog from the javascript
ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pucko73

ASKER

I found the reason for the problem.
Seems like there is some bug with span tag in ie8.

In the dialog that I show I had:
<b>Some Text: </b><span id="myid" />

Open in new window


if I changed that to
<b>Some Text: </b><span id="myid"> </span>

Open in new window


The page behind the dialog that I show did not "disaperar" and I can find the textbox etc.




i
Avatar of pucko73

ASKER

Thanks for all your help.  I've learned a lot from you....