开发者

Why IE does not refresh the page after a JS call to a click button?

On a ASP.Net page, I am in front of a problem only with IE. Here is the description.

On a page, there is two buttons a开发者_Python百科nd one label. The first button is visible and calls a JS function on the click event. This JS function calls the click function of the second button. The second button has an C€ event handler on the click event. The C# event handler edit the label.

In Firefox : the label is correctly edited after the clicks. In IE (8) : the label is not edited, despite the C€ event handler has been correctly hit.

Also, I observed, in IE, that the Page_Load event is called two times after the JS button click : Page_Load button2_OnClick => change of the Label Text Page_Load => The Label Text is reset :(

In Firefox, the Page_Load is called only once.

My question is : how to make IE refresh correctly the page as Firefox does after a JS button click ?

Below is the sample test code :

1) Page ASPX

<head runat="server">
    <title></title>
    <script type="text/javascript">
        function button1Click(sender, args) {
            var button2 = document.getElementById("button2");
            button2.click();
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button runat="server" ID="button1" Text="Click-me!" OnClientClick="button1Click();" />
        <asp:Button runat="server" ID="button2" Text="Second" OnClick="button2_OnClick" style="display:none" />
        <p />
        <asp:Label runat="server" ID="label1" Text="Init" />
    </div>
    </form>
</body>
</html>

2) C# code-behind :

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void button2_OnClick(object sender, EventArgs e)
    {
        label1.Text = "Changed";
    }
}


The ID of your button will not be button1 or button2 when it's rendered. It will probably be something like ctl001_button1. Therefore your javascript will not work. In ASP.NET 4 you can override this behaviour by using an assigned ClientID.

    <asp:Button runat="server" ID="button1" Text="Click-me!" 
           OnClientClick="button1Click();" ClientIDMode="Static" />
    <asp:Button runat="server" ID="button2" Text="Second" 
           OnClick="button2_OnClick" style="display:none" ClientIDMode="Static" />

As an aside, this alludes to the main problem with ASP.NET Winforms - it tricks developers into thinking that the web is a connected environment.

What actually happens when you click an <asp:Button /> element by default is that a postback is invoked. I.e. Your browser sends a request to the server for a new page. It sends up something called ViewState which is how the server knows what you've done and what to render. There is no "event" handled as such.


I think the problem is with the way you are trying to get the hidden button

var button2 = document.getElementById("button2");

maybe change this to

var button2 = document.getElementById("<%= button2.ClientID %>");

After the buttons are rendered in the browser, the ID is changed by the ASP.Net engine, and not the same as your source.

http://msdn.microsoft.com/en-us/library/system.web.ui.control.clientid.aspx

Hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜