SEND DATA FROM ASP.NET CODE BEHIND TO A JAVASCRIPT FUNCTION

Samuel Fernandes

Total Post:159

Points:1117
Posted by  Samuel Fernandes
 1170  View(s)
Ratings:
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.

  1. Sumit Kesarwani

    Post:378

    Points:2694
    Re: send data from asp.net code behind to a javascript function

    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

      Modified On Apr-04-2018 06:27:00 AM

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!