2
<script language="javascript" type="text/javascript">
    function myjavascriptfn() 
      {
        //debugger;
        var strValue= "test";
        return strValue
      }

How do I call this javascript function in my code behind and proceed appropriately with respective of return values.

4
  • Are you asking how to parse the HTML, extract the JS and execute it server side … or how to get the browser to execute the JS and pass the result back to the server? Commented Feb 17, 2012 at 10:35
  • — That was an either/or question. Which is it? Commented Feb 17, 2012 at 11:18
  • 1
    1) How to parse the HTML, extract the JS and execute it server side … OR 2) how to get the browser to execute the JS and pass the result back to the server? @Shanker : Please clarify your question. Commented Feb 17, 2012 at 11:23
  • i need to do both the operation:( Commented Feb 17, 2012 at 14:40

1 Answer 1

3

You can easily declare JavaScript to be run on the Client using

 ScriptManager.RegisterStartupScript(this, this.GetType(), "launchpage", "
     function javascriptfn() {
       var strValue= 'test';
       return strValue;
     }
     document.getElementById('"+HiddenField1.ClientID+"').value = javascriptfn();
     document.getElementById('"+saveProgressButton.ClientID+"').click();
  ", true);

note: I have divided out the JavaScript out onto multiple lines to make it easier to read but it should all be on one line.

Your problem comes with the second part of the question, sending the data back, you will most likely need a postback (partial or full or handle it with AJAX.

I would add an updatepanel with a asp hiddenfield and a hidden button to trigger it, populate the value of the hidden field with whatever this function is for had have some code in your code behind to capture the event.

<asp:UpdatePanel ID="responcetable" runat="server" UpdateMode="Conditional">
    <ContentTemplate>  
        <asp:HiddenField ID="HiddenField1" runat="server" />   
        <asp:Button ID="saveProgressButton" runat="server" Text="Button" CssClass="displaynone" /> 
    </ContentTemplate>        
    <Triggers><asp:AsyncPostBackTrigger ControlID="saveProgressButton" EventName="theeventtodealwiththis" /></Triggers>
</asp:UpdatePanel>

and on the serverside

    protected void theeventtodealwiththis(object sender, EventArgs e)
    {
         // some logic to handle the value returned
    }
Sign up to request clarification or add additional context in comments.

3 Comments

thanks for your reply.. in my javascript i will be calling an flashobject so by declaring this way its not working... i'm expecting it should resemble like a method which returns a string! how?
This solution should answer part 2 of @sanguine comment, will have a look around and see if it can answer part 1. When "saveProgressButton" is clicked it will send the "HiddenField1" back to the server which should contain the string from javascriptfn() which you can access from HiddenField1.Text. Are you wanting the flash object to communicate with a c# method in code behind? or is it simply replying a string in JavaScript. Or are there multiple JavaScript methods which would cause this to break?
thank you let me chk out and give yo reply...thanks once again :)

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.