Home > DeveloperSection > Forums > send data from asp.net code behind to a javascript function
Samuel Fernandes
Samuel Fernandes

Total Post:154

Points:1082
Posted on    August-22-2014 1:41 AM

 ASP.Net ASP.Net 
Ratings:


 1 Reply(s)
 661  View(s)
Rate this:

Hello i have a aspx page 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 i have this code:

ButtonOk.OnClientClick = String.Format("fnClickOK('{0}','{1}')", ButtonOk.UniqueID, "");

protected void ButtonOk_Click(object sender, EventArgs e)

{

}

now i have a class (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 from my codebehind to my javascript function?

thanks.



Sumit Kesarwani

Total Post:378

Points:2694
Posted on    August-22-2014 2:07 AM

Hi Samuel, 

You can have asp.net write arbitrary javascript during rendering of the 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


Don't want to miss updates? Please click the below button!

Follow MindStick