使用ASP.NET中的Javascript参数调用服务器端函数

时间:2022-04-09 15:59:09

I have an ASP.NET application in VB.NET. I have a Javascript function in mypage.aspx and another one in my callback.aspx page. I need this scripts to render and submit an IFrame on mypage.aspx.

我在VB.NET中有一个ASP.NET应用程序。我在mypage.aspx中有一个Javascript函数,在callback.aspx页面中有另一个函数。我需要这个脚本来渲染和提交mypage.aspx上的IFrame。

When I click on the submit button:

当我点击提交按钮时:

<asp:Button ID="subbtn" Name="Submit" OnClientClick="onsubmit_action();" runat="server" />

this script is executed, where iframeId is the id of the IFrame:

执行此脚本,其中iframeId是IFrame的id:

function onsubmit_action() {

            submitPage('iframeId');           
        }

The response of the IFrame (validation or success) is submitted to the callback.aspx file. I guess this happens through cross-site scripting that calls the callback function in the callback.aspx file;

IFrame的响应(验证或成功)将提交给callback.aspx文件。我想这是通过调用callback.aspx文件中的回调函数的跨站点脚本来实现的;

function callback()
{
     parent.pagecallback_success('<%=Request.QueryString("Id")%>');
}

that calls the function in mypage.aspx

调用mypage.aspx中的函数

function pagecallback_success(ref_id) {
        var Url = "mypage.aspx?";
        Url += "id=" + id;
        window.location.href = Url;
    }

The script works as expected. Now, I would like to call a server function

该脚本按预期工作。现在,我想调用一个服务器功能

Protected Function Store(ByVal id As String) As Boolean

in mypage.aspx.vb and pass the variable id:

在mypage.aspx.vb中传递变量id:

function hostedpagecallback_success(id) {
            var Url = "mypage.aspx?";
            Url += "id=" + id;
            window.location.href = Url;
            "<%= Store(id) %>"
        }

The problem is that the compiler considers id as a server side function and gives a compile error. However if I use a sub (without parameters) instead of a function, the sub is executed 3 times, on page_load, when the IFrame is received and another time (connection is https cannot debug Javascript efficiently).

问题是编译器将id视为服务器端函数并给出编译错误。但是,如果我使用sub(不带参数)而不是函数,则在收到IFrame时,在page_load上执行sub次3次(连接是https无法有效地调试Javascript)。

I am not good in cross-site scripting and code nuggets, probably it is really trivial but I do not know how to solve this problem. Anybody?

我不擅长跨站点脚本和代码块,可能它真的很小但我不知道如何解决这个问题。任何人?

1 个解决方案

#1


0  

I think you are looking for page methods a feature of MS-Ajax. Put the [WebMethod] on the method you want to call (server side) and call it client side with something like PageMethods.GetData(f, s, OnRequestComplete, OnRequestError);

我认为您正在寻找页面方法MS-Ajax的一个功能。将[WebMethod]放在要调用的方法(服务器端)上,并使用类似PageMethods.GetData(f,s,OnRequestComplete,OnRequestError)的方法调用客户端;

The whole pattern takes an article to describe. http://www.dotnetfunda.com/articles/article454-using-pagemethods-and-json-in-aspnet-ajax.aspx

整个模式需要一篇文章来描述。 http://www.dotnetfunda.com/articles/article454-using-pagemethods-and-json-in-aspnet-ajax.aspx

#1


0  

I think you are looking for page methods a feature of MS-Ajax. Put the [WebMethod] on the method you want to call (server side) and call it client side with something like PageMethods.GetData(f, s, OnRequestComplete, OnRequestError);

我认为您正在寻找页面方法MS-Ajax的一个功能。将[WebMethod]放在要调用的方法(服务器端)上,并使用类似PageMethods.GetData(f,s,OnRequestComplete,OnRequestError)的方法调用客户端;

The whole pattern takes an article to describe. http://www.dotnetfunda.com/articles/article454-using-pagemethods-and-json-in-aspnet-ajax.aspx

整个模式需要一篇文章来描述。 http://www.dotnetfunda.com/articles/article454-using-pagemethods-and-json-in-aspnet-ajax.aspx