retrieve value from javascript function in codebehind

丶灬走出姿态 提交于 2019-12-22 13:39:08

问题


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.


回答1:


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 resutOfExecuteJavaScript = hdnVal.Value;



回答2:


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




回答3:


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;


来源:https://stackoverflow.com/questions/31136426/retrieve-value-from-javascript-function-in-codebehind

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!