2

How can I retrieve value from javascript function in codebehind, on page load .. javascript function like :

<script type="text/javascript">
        function isIFrame() {
            var isInIFrame = (top.location != self.location);
            if (isInIFrame) {
                return "inside";
            }
            else {
                return "outside";
            }
        }
    </script>

and code behind like :

protected void Page_Load(object sender, EventArgs e)
    {
        string resutOfExecuteJavaScript = "";
        // resutOfExecuteJavaScript = isIFrame(); // from javascript

        if (resutOfExecuteJavaScript == "inside")
        {
            // do something
        }
        else
        {
            // do something
        }
    }

thank you.

3 Answers 3

6

You cannot directly call a client side javascript method from server side code . For that first you need to assign the function result to value of some hidden variable and then access it in server side

Suppose you have an hidden field like this

<input type="hidden" runat="server" id="hdnVal"/>

then you can set the value as below

document.getElementById("hdnVal").value=isIFrame();

then at serve side

 string resutOfJavaScriptExecution = hdnVal.Value;
Sign up to request clarification or add additional context in comments.

8 Comments

this is not useful, because Page_Load run before javascript function
Page_load happens in server side , Javascript executes at client side . So whenever the value is postback to server you will have this value in the variable . Give a full picture of the issue in the post
Check this for your referance dotnetodyssey.com/2014/12/18/…
@Mo7ammed - I think the problem is you don't understand how ASP.NET works. server-side code runs on the server... client-side code runs on the client... they can interact only when the server provides information to the client through the rendering of the javascript, and the client-side code can send information back to the server either through post-back or AJAX, etc. The server-side code cannot run a function that is written in a language that is used by the client-side
@Midhun - just a small thing, your answer is a good one, but the OP never mentioned jQuery, so you might want to provide a bit of vanilla code as well with an explanation
|
1

using _doPostBack, you can solve this one

      <script type="text/javascript">
             function isIFrame() {
            var isInIFrame =(top.location != self.location);
            var result;
            if (isInIFrame) { 
                result="inside";
             }
           else
             {
             result ="outside";
             }
           __doPostBack('callPostBack', result);
        </script>
    </head>

In code behind section

protected void Page_Load(object sender, EventArgs e)
{
    this.ClientScript.GetPostBackEventReference(this, "arg");
    if (IsPostBack)
    {
        string eventTarget = this.Request["__EVENTTARGET"];
        string eventArgument = this.Request["__EVENTARGUMENT"];

        if (eventTarget != String.Empty && eventTarget == "callPostBack")
        {
            if (eventArgument == "inside"){   
               //do something
               }
           else if(eventArgument == "outside")
            {
           //do something
           }
       }
    else
    {
       // set the button click
        btnclick.Attributes.Add("onClick", "isIFrame();");
    }
}

Below link will help you out to get more idea.

http://www.dotnetcurry.com/ShowArticle.aspx?ID=203

1 Comment

Please don't provide links as answers. Links can go dead, and if this is the answered accepted by the OP then people won't know what the solution is. Please provide details within your answer, quoting the original page
0

in javascript file or your script add :

function SetHiddenVariable()
     {
        document.getElementById(inpHide).value= "value";
     }

in .aspx add this tag:

    <input id="inpHide" type="hidden" runat="server" />

in aspx.cs (c# file) add :

 anyVariable = inpHide.Value;

Comments

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.