AGuyCalledGerald AGuyCalledGerald - 1 month ago 12
jQuery Question

How to fill a Label.Text - Property via jQuery

I use ASP.NET and have a label control on my page, which I fill with
the jQuery-Command

$('#<%= myLabel.ClientID %>').html(content);


.val() does not seem to work with this.

Somehow, I have Problems getting the content in code-behind. In the code, the myLabel.Text-Property is still empty.

Answer

If you want to display the value on the client and have it available on the page, you need an input that'll get sent to the code-behind when you POST like this:

$('#<%= myLabel.ClientID %>').html(content);
$('#<%= myInput.ClientID %>').val(content);

<asp:Label Id="myLabel" runat="server" />
<asp:HiddenField ID="myInput" runat="server" />

In the code-behind:

myInput.Value
Comments