I have a series of textboxes on a form. When the user inserts numbers into these textboxes, calculations are made and <asp:Label>
controls are updated via JavaScript to reflect these calculations:
document.getElementById('<%=TotalLoans.ClientID %>').innerHTML = TotalLoans;
This correctly updates the UI. However, when I try to access the value in the codebehind, the Text
property is empty. This makes sense I guess, since I was updating the innerHTML
property via the JavaScript.
//TotalLoans.Text will always be equal to "" in this scenario
double bTotalLoans = string.IsNullOrEmpty(TotalLoans.Text)
? 0.00
: Convert.ToDouble(TotalLoans.Text);
How do I update the Text
property of the <asp:Label>
via JavaScript in such a way that I can read the property in the codebehind?
Update
This is a small problem on a large form that contains 41 labels, each of which displays the results of some calculation for the user. Taking the advice of FishBasketGordo I converted my <asp:Label>
to a disabled <asp:TextBox>
. I'm setting the value of the new textbox as such:
document.getElementById('<%=TotalLoans.ClientID %>').value = TotalLoans;
Again, in the codebehind, the value of TotalLoans.Text
is always equal to "".
I don't mind changing how I approach this, but here's the crux of the matter.
I am using JavaScript to manipulate the property values of some controls. I need to be able to access these manipulated values from the code behind when 'Submit' is clicked.
Any advice how I can go about this?
Update 2
Regarding the answer by @James Johnson, I am not able to retrieve the value using .innerText
property as suggested. I have EnableViewState
set to true on the <asp:Label>
. Is there something else I am missing?
I don't understand why, when I type in a textbox and submit the form, I can access the value in the codebehind, but when I programmatically change the text of a textbox or label by way of JavaScript, I cannot access the new value.
asp:Label
isn't a type of<input>
. I feel like I've run into this before and had to use hidden inputs for the server to recognize it. – Henandchickens<asp:Label>
controls aren't really the right choice for what you're doing. I would consider using a read-only textbox or something more semantically appropriate. – Respecting