最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

c# - retrieve value from javascript function in codebehind - Stack Overflow

matteradmin4PV0评论

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.

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.

Share Improve this question edited Jun 30, 2015 at 11:38 Uwe Allner 3,4679 gold badges37 silver badges51 bronze badges asked Jun 30, 2015 at 10:59 Mo7ammedMo7ammed 511 gold badge3 silver badges9 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 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;

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./ShowArticle.aspx?ID=203

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;
Post a comment

comment list (0)

  1. No comments so far