---
title: "send data from asp.net code behind to a javascript function"  
description: "send data from asp.net code behind to a javascript function"  
author: "Samuel Fernandes"  
published: 2014-08-22  
updated: 2014-08-22  
canonical: https://www.mindstick.com/forum/2090/send-data-from-asp-dot-net-code-behind-to-a-javascript-function  
category: "asp.net"  
tags: ["asp.net"]  
reading_time: 1 minute  

---

# send data from asp.net code behind to a javascript function

Hello i have a [aspx page](https://www.mindstick.com/forum/218/how-do-i-create-an-aspx-page-that-periodically-refreshes-itself) and have this code on it:

```
<asp:Button ID="ButtonOk" runat="server" Text="Ok" OnClick="ButtonOk_Click" /><script type="text/javascript">    function fnClickOK(sender, e)     {        __doPostBack(sender, e);     }</script>
```

and on my [code behind](https://www.mindstick.com/forum/2199/call-javascript-s-function-from-code-behind) i have this code:

ButtonOk.OnClientClick = [String.Format](https://www.mindstick.com/forum/1763/string-format-with-null-values-c-sharp)("fnClickOK('{0}','{1}')", ButtonOk.UniqueID, "");

```
protected void ButtonOk_Click(object sender, EventArgs e){}
```

now i have a [class](https://www.mindstick.com/blog/165/generic-class-in-c-sharp) (class someclass = new closs()) that is been Created in the code behind and i want to use it in the ButtonOk_Click function.

so I could do somthing like this:

```
protected void ButtonOk_Click(object sender, EventArgs e){   string name = someclass.getname;}
```

so how can i send [data](https://www.mindstick.com/articles/13050/salesforce-aiming-to-dominate-predictive-analytics-with-data-science) from my codebehind to my [javascript function](https://www.mindstick.com/forum/2091/javascript-function-on-data-bind)?

thanks.

## Replies

### Reply by Sumit Kesarwani

Hi Samuel,

You can have asp.net write arbitrary [javascript](https://www.mindstick.com/articles/874/how-to-create-watermark-text-for-textbox-by-using-javascript) during rendering of the [page](https://www.mindstick.com/articles/13031/why-to-make-a-wikipedia-page).

```
Page.ClientScript.RegisterStartupScript(    typeof(Button),
"okclick", string.Format("var MyData = '{0}';", data),
true);
```

after the page loads, you will have access to MyData in the global javascript scope. You can reference it from within fnClickOK


---

Original Source: https://www.mindstick.com/forum/2090/send-data-from-asp-dot-net-code-behind-to-a-javascript-function

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
