Search code examples
jqueryasp.netlabelcode-behind

Change asp:label with jQuery then access in codebehind


I know, crazy right.

So I have an asp label and I want to fill it with jQuery. Later, in the codebehind I want to access this text for something else.

What I have is:

<asp:Label ID="myLabel" runat="server" />
<asp:Button ID="clickMe" runat="server" Text="Click!" OnClientClick="runMe();" 
   OnClick="clickMe_Click()" />

<script language="javascript" type="text/javascript">

   function runMe () {
      $("#<%=myLabel.ClientID").text("here");}
</script>

protected void clickMe_Click(object sender, EventArgs e)
{
   string isIt = myLabel.Text;
}

So if I put a breakpoint on whatever code follows 'isIt' definition and look at it, isIt="". Is there a way to make this work? I'm guessing since jQuery's acting on the html it is actually altering the not the actual ASP.NET label.


Solution

  • Try

    $('#<%= myLabel.ClientID %>').html("here"); 
    (This should work in all the browsers)
    

    When I want to pass the value to the code behind page, I generally use a HiddenField and populate it using .val() in Jquery. I can then access its value in the code behind using HiddenField1.Value