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

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

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;

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743728017a4496950.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信