Mako
Mako

Reputation: 1515

Unable To Access Label Text in Code Behind file when setting it through Javascript

I have a label control in my Page

<asp:Label ID="EmpType" runat="server" Text=" " ></asp:Label>

I am setting its value through Javascript using the following code

var lblEmpType = document.getElementById('<%=EmpType.ClientID %>');
 lblEmpType.innerText = "Hi";

The value is getting displayed in the Page correctly but when i try to access "EmpType.Text" from code behind file, the value is " " ... I want to access the value of the label which i have set through javascript..

Upvotes: 2

Views: 1913

Answers (1)

patmortech
patmortech

Reputation: 10219

If want to know the label's new value, you will have to use a hidden field on your page that gets its value set to the same thing as the label in your javascript code, and then you can look at the hidden field's value on postback.

ASP.NET Label controls are rendered as a span tag in html, which does not have any value to postback natively (that only works with actual input fields like textboxes and select lists). That's why there is nothing changed when you check again in your code behind after posting the form.

Upvotes: 4

Related Questions