1

I want to set Label's text in javascript function, function call and set text to Label and I see it but when I want to read Label's text from code behind the text is previous text (for example if label's text is "5" and I set it to "10" in function but in code behind text is "5")

Javascript function

function PopUpFunction(code) {
  $("#<%= lblStatus.ClientID %>").text(code);
  return false;
}

Set function to click event of LinkButton

lnk.Attributes.Add("onclick", "PopUpFunction(10);");

3 Answers 3

4

You have used text as property but it is a function, Use text() not text

$("#<%= lblStatus.ClientID %>").text(code);

Put an alert to check if your page is refreshed or redirected

function PopUpFunction(code) {
  $("#<%= lblStatus.ClientID %>").text(code);
  alert($("#<%= lblStatus.ClientID %>").text());
}

You can try returning false to stop postback.

lnk.Attributes.Add("onclick", "return PopUpFunction(10);");

function PopUpFunction(code) {
  $("#<%= lblStatus.ClientID %>").text(code);
  alert($("#<%= lblStatus.ClientID %>").text());
  return false;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Did that change not fix the issue?
Assign the value to hidden field and access it in code behind. The html content are not submitted on postback.
Thank you soooooooooooooo much ! I change Label to hidden field and works !
0

Reason for your code doesn't work

It happens because you call the JavaScript Function and it sets the Value but after that the page get reloads... So the Original Value Overwrite the New Value...

If you return False then it doesn't post back the Data and the Page doesn't get reload..

Try it like below... it will help you...

C#

    lnk.Attributes.Add("onclick", "return PopUpFunction(10);");

Javascript :

    <script>
       function PopUpFunction(code) {
           document.getElementById("lblStatus").innerHTML = code
           return false;
       }
   </script>

2 Comments

doesn't find Label that set value to it,remember that Label is server side control
@Sajjjjjjad : @Sajjjjjjad : yes I know the label is server Side control... document.getElementById("lblStatus").innerHTML = code this code set the value to label.... try it first...
0

Are you any script file (Any minimized version) for (**<script type="text/javascript" src="../../Scripts/jquery-ui-1.9.2.min.js"></script>**)is refering in your page head tag ?

Must add any minimized version script in your page . And first run your application in IE browser . let know Check the IE browser is threw any error ??

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.