Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
83
rated 0 times [  85] [ 2]  / answers: 1 / hits: 129610  / 12 Years ago, thu, april 12, 2012, 12:00:00

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.


More From » asp.net

 Answers
4

Place HiddenField Control in your Form.



<asp:HiddenField ID=hidden runat=server />


Create a Property in the Form



protected String LabelProperty
{
get
{
return hidden.Value;
}
set
{
hidden.Value = value;
}
}


Update the Hidden Field value from JavaScript



<script>
function UpdateControl() {
document.getElementById('<%=hidden.ClientID %>').value = '12';
}
</script>


Now you can access the Property directly across the Postback. The Label Control updated value will be Lost across PostBack in case it is being used directly in code behind .


[#86276] Wednesday, April 11, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
juliettec

Total Points: 327
Total Questions: 127
Total Answers: 102

Location: Bangladesh
Member since Sat, Jan 23, 2021
3 Years ago
;