0

Requirement is to set the label text from the tree view when user select any node. While debugging I am getting hospitalName correctly but it is not setting label text.

$("[id$=HospitalTreeViewDiv] span").click(function () {

  var hospitalName = $(this)[0].outerText;

  //this is also not working
  // $("label[for='SelectedHospitalLiteral']").text(hospitalName); 

  $("[id$=SelectedHospitalLiteral]").val("abc");

});

This is aspx snippet

   <div runat="server" id="HospitalTreeViewDiv">
            <asp:TreeView ID="HospitalTreeView" runat="server" ExpandDepth="0">
            </asp:TreeView>
   </div>

   <div class="alignBottom">
        <p> <asp:Label ID="SelectedHospitalLiteral" runat="server" ></asp:Label></p>
   </div>
PawanS
  • 7,033
  • 14
  • 43
  • 71

2 Answers2

1

Use HiddenField for Postbacks

<asp:HiddenField runat="server" ID="labelText" Value=""/>

$("[id$=HospitalTreeViewDiv] span").click(function () {
      $("[id$=labelText]").val("abc")
});

refer to this: How to change the text of a label in jQuery?

Community
  • 1
  • 1
patel.milanb
  • 5,822
  • 15
  • 56
  • 92
  • short time smile... `$html` worked at UI but at code behind still I am getting old values – PawanS Dec 17 '13 at 13:07
  • what do you mean by code behind: once you set the text for label, you will be able to access that value in code behind...# – patel.milanb Dec 17 '13 at 13:09
  • No, I checked that $html is changing the text but when you see label.Text code behind it will give the old value (in my case it was empty) so I am getting this rendered html `

    ...`

    – PawanS Dec 17 '13 at 13:19
  • are you posting back your page, or is it redirected to same page. because when you want to access the text in code-behind, if it is not posted back it will re-render the HTML and re-set the text of label – patel.milanb Dec 17 '13 at 13:26
  • I am new to asp.net, let me analyze your last comment – PawanS Dec 17 '13 at 13:28
  • I think it is yes, its PoastBack... setting the label text is happening on popup screen and on popup screen "Ok" button code behind i am checking labels last updated value... Any other idea! – PawanS Dec 17 '13 at 13:33
  • why dont you use asp:HiddenField rather than changing label value. hiddenfield values will be persisted during postbacks.. – patel.milanb Dec 17 '13 at 13:38
  • Great suggestion... hidden field can work for me. Please edit your answer and suggest for hidden field. – PawanS Dec 17 '13 at 13:46
0

When you are using asp controls label control use ClientID to get the control:

Also to set text use text("yourtext")

Working with your code here:

 $("[id$=HospitalTreeViewDiv] span").click(function () {

      var hospitalName = $(this)[0].outerText; //your forgot to give semicolon here

      $("#<%=SelectedHospitalLiteral.ClientID%>").text("abc"); // and also semicolon  here 

    });
Somnath Kharat
  • 3,570
  • 2
  • 27
  • 51
  • in my case it crashed `$("#<%=HospitalTreeViewDiv.ClientID%>").` itself.. it says `Microsoft JScript runtime error: Syntax error, unrecognized expression: #<%= HospitalTreeViewDiv.ClientID %>` – PawanS Dec 17 '13 at 13:21
  • yes ur r right it will crash.Render ids will be multiple so...modified ans – Somnath Kharat Dec 17 '13 at 13:24