Click here to Skip to main content
15,897,718 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have multiple labels and assigning value to then using jQuery OnChange event... but while getting value of those labels in vb.net on submit button then getting NULL OR Empty String .. Any one can tell how may i get those labels value in vb.net or c#

My Code:

function labelValue(){
var amount = jQuery("#<%=lblAmount.ClientID %>");
amount.value= jQuery("#<%=txtUserName.ClientID %>").val()
}
<asp:TextBox ID="txtUserName" runat="server" Text="10000" />
<asp:Label ID="lblAmount" runat="server" />
VB.Net Code: On Submit Button:

Dim lbl As String = lblAmount.Text
I am getting here zero or Null
Posted
Comments
Sergey Alexandrovich Kryukov 3-Feb-16 12:04pm    
Are you sure you are talking about labels? Labels are designed to be primarily static text elements used to label input control and providing access key for setting keyboard focus on them. What "value"? Why would you need to modify labels?
jQuery .val() reads or assigns the value of an input control, it does not change label text and not applicable to non-input elements.
—SA
Member 10711621 3-Feb-16 13:47pm    
Instead of label what control should i use ?

asp:Label controls are rendered as span elements on the page, and things in a span are not submitted with a page postback so your .net code has no idea you've updated the content of the span via javascript (.net stores info like this in the ViewState, and reads from the ViewState to recreate the control).

You should put a hidden field on the page and update the value of the hidden field as well as the label's text, and on postback your .net code can query the value of that hidden field to find out what you updated the text to.
 
Share this answer
 
Please see my comment to the question.

You badly misuse labels, but this is not the worst thing. In JavaScript, you actually can add any non-existing property to any element, which you actually do with your amount.value = line. As to jQuery .val(), it also operates the property value. But the problem is: it has nothing to do with the text of the element you are trying to get. With label, the content is not even text, this is any HTML markup (innerHTML in JavaScript). Moreover, no matter what you do in your JavaScript, the changes cannot reach the server side unless you send an HTTP request, which can be done by sending it via Ajax, posting a Web form, navigation, etc. But don't try to do anything like that right away. Rather, you have to redesign your whole approach, and for that you need to understand better how Web works, what happens on the server side and client side, in what order, how HTTP works, and so on.

Your question can make more sense only if you forget labels and even text input elements for now and explain just the behavior you want to achieve.

—SA
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900