Thursday, April 14, 2011

Problems simulating click of ASP.NET button.

I have some code like this

<asp:Button ID="cancelDummyButt" runat="server" CausesValidation="False" 
    Text="cancel" onclick="cancelDummyButt_Click" Enabled="True" />

(Javascript:)

buttid = '<%=cancelDummyButt.ClientID.ToString%>';
eltoclick = document.getElementById(buttid);
eltoclick.click();

The server-side code is not called. What is wrong? It goes OK until the "click". What is the best way to debug?

(This is to get the right postback from an iframe within an updatepanel)

From stackoverflow
  • First of all, you have a mistake in this line :

    buttid = '<%= cancelDummyButt.ClientID.ToString() %>';
    

    my recommendation is that, place your javascript code to code behind and then register to page like that :

    string script = string.Format("var buttid = '{0}';", cancelDummyButt.ClientID);
    if (!ClientScript.IsClientScriptBlockRegistered("myScript"))
    {
        ClientScript.RegisterClientScriptBlock(typeof(_Default), "myScript", script, true);
    }
    
    Olav : I think ToString without "()" is ok in VB.NET - anyway buttid and eltoclick got assigned correctly, as far as i could see
  • Solved - it was cause by a modal jQuerywindow!

    (Would like some way around that, of course)

    Olav

  • Olav, the trick with the modal windows is to have a hidden ASP:Button outside the modal window. You can have a regular html button inside the modal window that when clicked on, calls the __doPostback('', '') method for the hidden ASP:Button control. This has the same effect as actually clicking the ASP button.

    <!-- start modal dialog div -->
    
       <input type="button" onclick="__doPostBack('<%= btnDoIt.UniqueID %>','');" value="Do It!" />
    
    <!-- end modal dialog div -->
    
    <asp:Button id="btnDoIt" style="display:none;" runat="server"></asp:Button>
    

  • Programmatically invoking the click() event of a control does not fire the event handler function. You'll need to invoke it manually....

    if(eltoclick.onclick())
      eltoclick.click();
    

0 comments:

Post a Comment