Reputation: 31
I'm trying to read a value from a Label(asp) with Ajax. but i'm allways get Undefind :|
My Code is:
function NIS2USD() {
var from = document.getElementById("NIS").value;
var to = document.getElementById("USD").value;
var amount = document.getElementById("totalAmountLabel").value;
request = new XMLHttpRequest();
request.onreadystatechange = ProcessResponse;
request.open("GET", "Convert.aspx?from=" + num1 + "&to=" + num2 + "&amount=" + amount, true);
request.send();
}
function USD2NIS() {
var from = document.getElementById("USD").value;
var to = document.getElementById("NIS").value;
var amount = document.getElementById("totalAmountLabel").value;
request = new XMLHttpRequest();
request.onreadystatechange = ProcessResponse;
request.open("GET", "Convert.aspx?from=" + num1 + "&to=" + num2 + "&amount="+amount, true);
request.send();
}
function ProcessResponse() {
if (request.readyState == 4 && request.status == 200) {
document.getElementById("totalAmountLabel").innerHTML = request.responseText;
}
}
and My def for the label is:
<asp:Label ID="totalAmountLabel" runat="server" Text="Label"></asp:Label>
Why I'm always getting undef?
Upvotes: 0
Views: 991
Reputation: 1058
it is because controls get renamed when inside an update panel. try to use this if you have jQuery reference
$('#<%=totalAmountLabel.ClientID%>')
Upvotes: 1
Reputation: 50728
Either change the javascript to (the javascript code must be defined in page for this to work, not in a JS file because of the <%= %>
):
document.getElementById("<%= totalAmountLabel.ClientID %>").innerHTML = request.responseText;
Or leave the javascript as is and change the label to (for 4.0):
<asp:Label ID="totalAmountLabel" runat="server" Text="Label" ClientIDMode="static"></asp:Label>
Client ID mode of static ensures on the client the ID is actually totalAmountLabel
Upvotes: 0
Reputation: 227240
I assume <asp:Label>
is generating an HTML <label>
tag. If so, it doesn't have a .value
attribute. Try .innerHTML
instead.
var amount = document.getElementById("totalAmountLabel").value;
should be
var amount = document.getElementById("totalAmountLabel").innerHTML;
Upvotes: 0
Reputation: 16144
Try this to get the client ID for the label.
var amount = document.getElementById("<%=totalAmountLabel.ClientID%>").innerHTML;
Upvotes: 1