在不同的浏览器不一致的LinkBut​​ton PageMethod的行为 [英] Inconsistent LinkButton PageMethod behavior in different browsers

查看:205
本文介绍了在不同的浏览器不一致的LinkBut​​ton PageMethod的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网页,执行后回上一个LinkBut​​ton,但也有一个的OnClientClick 事件。我们的想法是从客户端的数据(不要问)。

I have a LinkButton on a page that performs a post-back, but also has an onClientClick event. The idea is to set some session variables in the background from client-side data (don't ask).

我已经放在一个破发点中的Web方法步骤通过我们的code,我们正在经历什么是依赖于浏览器中,PageMethods可能会返回一个成功的消息,故障信息,或都没有消息的。此外,幅材的方法可以或可以不被调用,无论PageMethods结果

I've placed a break point in the web method to step through our code, and what we're experiencing is that depending on the browser, the PageMethods may return a success message, failure message, or no message at all. Additionally, the web method may or may not get called, regardless of the PageMethods result.

下面是我们的研究结果一个方便的小图:

Here's a handy little chart of our results:

Browser          PageMethods    WebMethod
--------------   -------------  --------------------
IE 8, 9, 10      Success        Called successfully
Safari 5.1.7     Failure        *Never called*
Firefox 25.0.1   *Neither*      Called successfully
Chrome v31       Failure        Called successfully

这是四个不同的浏览器和四个不同的结果。

That's four different browsers, and four different results.

我已经试过产生在两个服务器端和客户端的code具有同等效力的链接按钮,甚至没有设置在WebMethod的会话变量,具有相同的结果。

I've tried generating the link button in both server-side and client-side code with the same effect, and without even setting the session variables in the WebMethod, with the same results.

在code可以进行复制,以下简单code:

The code can be reproduced with the following simple code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script type="text/javascript">

    function doStuff() {
        var a = 'a';
        var b = 'b';

        PageMethods.doStuffWebMethod(a, b, doStuffSuccess, doStuffFail);
    }

    function doStuffSuccess() {
        alert(Success!');
    }
    function doStuffFail() {
        alert(Failure!');
    }

</script>

<html>
    <body style="background-color:#f3f4f6;" >

        <form runat="server" name="mainForm" id="mainForm" action="Test.aspx">

            <asp:ScriptManager ID="ScriptManager1"  EnablePageMethods="true" runat="server"></asp:ScriptManager>

            <asp:LinkButton runat="server" CausesValidation="false" OnClientClick="doStuff();">Do stuff!</asp:LinkButton>

        </form>
    </body>
</html>

protected void Page_Load(object sender, EventArgs e)
{
    LinkButton lbAdd = new LinkButton();
    lbAdd.Text = "Web method test";
    lbAdd.CausesValidation = false;
    lbAdd.OnClientClick = "doStuff();";
    mainForm.Controls.Add(lbAdd);
}

[WebMethod]
public static void doStuffWebMethod(string a, string b)
{
    try
    {
        //System.Web.HttpContext.Current.Session["a"] = a;
        //System.Web.HttpContext.Current.Session["b"] = b;
        string x = a + b;
    }
    catch (Exception ex)
    {
        //
    }
}

的问题:

为什么我的Web方法未能在Safari,并让我在其他三种浏览器三种不同的返回消息吗?

Why is my web method failing in Safari, and giving me one of three different return messages in three other browsers?

我怎样才能改变这种code把它提到的浏览器工作?

How can I change this code to get it to work in the browsers mentioned?

推荐答案

调用您的Web方法失败的原因是因为 你不取消的回发的的LinkBut​​ton 。 你必须从事件的OnClientClick返回false取消 回发。在code以下应该修复它:

The reason the call to your web method is failing is because your are not canceling the postback of your LinkButton. You have to return false from the OnClientClick event to cancel the postback. The code below should fix it:

function doStuff() {
  var a = 'a';
  var b = 'b';

  PageMethods.doStuffWebMethod(a, b, doStuffSuccess, doStuffFail);
  return false; // Cancel postback.
}

function doStuffSuccess() {
  alert('Success!');
}

function doStuffFail() {
  alert('Failure!');
}

<asp:LinkButton ID="mybutton"  runat="server" CausesValidation="false" OnClientClick="return doStuff();">Do stuff!</asp:LinkButton>

有关取消浏览器(回传)的默认行为更复杂的解决方案,请对下面的<一看href="http://stackoverflow.com/questions/128923/whats-the-effect-of-adding-return-false-to-an-onclick-event">stackoverflow问答。

For a more sophisticated solution for canceling the default behaviour of a browser (postback) please have a look on the following stackoverflow question and answer.

你得到的不同结果不同的浏览器的原因也许是由于浏览器厂商不同的实现。但我不知道这件事。

The reason you are getting those different results for the different browsers is maybe due to different implementations of the browser vendors. But I am not sure about this.

您还可以通过创建一个网络协议跟踪验证此行为 (在谷歌Chrome浏览器和切换到网络选项卡preSS F12)。

You can also verify this behaviour by creating a network protocol trace (press F12 in Google Chrome browser and switch to the network tab).

如果该协议不从doStuff返回false()方法:

The protocol in case you do not return false from the doStuff() method:

  1. 在该页面的方法 doStuffWebMethod 被调用。然后你得到的JavaScript消息框。 ( HTTP POST 的)
  2. 您WebForm.aspx要求。 ( HTTP POST 的)
  3. 然后WebResource.axd的和的ScriptResource.axd要求。 ( HTTP GET 的)
  1. The page method doStuffWebMethod is called. Then you get the JavaScript message box. (HTTP POST)
  2. Your WebForm.aspx is requested. (HTTP POST)
  3. Then WebResource.axd and ScriptResource.axd is requested. (HTTP GET)

2号和3显示了一个回发是您的页面方法的请求后执行。

Number 2. and 3. shows that a postback is executed after the request of your page method.

如果该协议你从doStuff返回false()方法:

The protocol in case you do return false from the doStuff() method:

  1. 只有网页的方法 doStuffWebMethod 被调用。 ( HTTP POST 的)
  1. Only the page method doStuffWebMethod is called. (HTTP POST)

第二个跟踪清楚地表明,没有执行回发

The second trace clearly shows that there is no postback executed.

如果你想回发的情况发生你的LinkBut​​ton的,那么你可以手动触发使用页面方法后__doPostBack()方法中的JavaScript成功处理回传回来:

If you want the postback to happen for your LinkButton then you could manually trigger the postback in the JavaScript success handler using the __doPostBack() method after the page method comes back:

function doStuffSuccess() {
  alert('Success!');

  __doPostBack('<%= mybutton.UniqueID %>', '');  // trigger the postback.
}

这篇关于在不同的浏览器不一致的LinkBut​​ton PageMethod的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆