Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

up vote 0 down vote accepted

I've found that the easiest way to send data back and forth between client and codebehind is to use jquery ajax functions and page methods.

Here's a good getting started article :

http://encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-methods/

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.