0

I am trying fetch c# value in to JavaScript any body can help me to resolve this.

ASP.NET CODE

 <asp:Button runat="server" Text="Submit"   class="btn btn-success btn-lg" ID="btnsubmit" OnClick="btnsubmit_Click"/>

C# code

protected void btnsubmit_Click(object sender, EventArgs e)
{
   string abc="xyz";
   // From here i need to Call JavaScript Function 
   // suppose code is like myfunction(abc)<-Java script function
   myfunction(abc);
}

Javascript

<script type="javascript/text">

  function myfunction(abc){
    var val = abc
    console.log(val);
    // here i want to get abc value is this possible
  }

</script>

1 Answer 1

2

You can use ClientScriptManager.RegisterStartupScript method.

Registers the startup script with the Page object using a type, a key, a script literal.

Code

 protected void btnsubmit_Click(object sender, EventArgs e)
 {
      string abc="xyz";       

      //If the string can contain apostrophes or backslashes, you would need to escape them
      string value = abc.Replace("\\", "\\\\").Replace("'", "\\'");
      Page.ClientScript.RegisterStartupScript(this.GetType(),"CallMyFunction","myfunction('" +value + "')",true);
 }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.