Simulate postback from JavaScript problem in IE

Last post 06-12-2007 4:12 AM by WolfyUK. 6 replies.

Sort Posts:

  • Confused [*-)] Simulate postback from JavaScript problem in IE

    06-11-2007, 5:58 AM
    • Loading...
    • WolfyUK
    • Joined on 07-15-2005, 5:38 AM
    • Leeds, UK
    • Posts 31

     I have what seems to be a relatively simple problem when attempting to simulate a postback in ASP.NET from JavaScript. My scenario is (and please don't ask me why) that I have an HTML <a>nchor which is generated dynamically using the DOM, and when clicked should force an <asp:Button> to do a postback so that some server-side processing can be done.

    The relevant JavaScript is below: 

        function GetAnchor() {
    theAnchor = document.createElement("a");
    theAnchor.href = "javascript:void(0);";
    theAnchor.onclick = DoClick;
    theAnchor.innerHTML = "Click me";

    ... // add element to parent, amongst other things
    }
    function DoClick() {
    document.getElementById('TheButton').click();
    }
    and the ASP.NET:
        <asp:Button ID="TheButton" runat="server" />
    All very straighforward (although there are some more complicated bits and bobs outside this), and the anchor causes a successful postback in FireFox when it is clicked. However, IE6 seems to refuse to postback when requested. If I add the following to the codebehind to see what's going on:
        TheButton.Attributes.Add("onclick", "alert('Oh dear');" & ClientScript.GetPostBackEventReference(TheButton, ""))
    then I receive the message box but no postback follows. Again, this is fine in FireFox. Clicking the button manually works successfully, but this is not suitable for this solution.

    Any ideas?

    Thanks,

    Marc
    Filed under: , ,
  • Re: Simulate postback from JavaScript problem in IE

    06-11-2007, 9:38 AM
    • Loading...
    • A1ien51
    • Joined on 05-06-2005, 6:46 PM
    • MD USA
    • Posts 2,852

    Do a view source on the page and you will see your asp button does not have the ID="TheButton", but one that has a clientID on it.

     Easiest thing is to use Page.RegisterClientScriptBlock() on the code behind and write out the

    string outputText = "var btnId = '" + TheButton.ClientId + "';";

    Than you would just do

     document.getElementById(btnId).click();

    Eric
     

     

    Coauthor of Ajax In Action [#1 Computer and Internet book on Amazon for 2006]

    If you get an answer to your question, please mark it solved so people don't waste time reading already answered questions!
  • Re: Simulate postback from JavaScript problem in IE

    06-11-2007, 9:52 AM
    • Loading...
    • WolfyUK
    • Joined on 07-15-2005, 5:38 AM
    • Leeds, UK
    • Posts 31

    Hi,

    Thanks for your reply. Unfortunately that is not the case here - 'TheButton' remains the ID of the control after it has been rendered:

        <input type="submit" name="TheButton" value="" onclick="alert('Oh dear');__doPostBack('TheButton','');" id="TheButton" /> 
    The 'Oh dear' message does get displayed, so the browser is not having difficulty in finding the control itself, rather just click()-ing it.

    Marc

  • Re: Simulate postback from JavaScript problem in IE

    06-11-2007, 11:56 AM
    • Loading...
    • A1ien51
    • Joined on 05-06-2005, 6:46 PM
    • MD USA
    • Posts 2,852

    Well there is no reason why you really need to click the button anyway when you can just call __doPostBack('TheButton','');

     Eric
     

     

    Coauthor of Ajax In Action [#1 Computer and Internet book on Amazon for 2006]

    If you get an answer to your question, please mark it solved so people don't waste time reading already answered questions!
  • Re: Simulate postback from JavaScript problem in IE

    06-11-2007, 12:12 PM
    • Loading...
    • WolfyUK
    • Joined on 07-15-2005, 5:38 AM
    • Leeds, UK
    • Posts 31

    A very good point indeed! I modified the JavaScript accordingly, but this still fails in IE6. I have just tested it in IE7 and all is fine, as it is with FireFox. 

    Marc 

  • Re: Simulate postback from JavaScript problem in IE

    06-11-2007, 12:26 PM

    use a submit button instead and add the event on that and it will work all time.

    Hope that helps.

    Please Mark Post that helped you as answer, also include a summary of what solved the problem as it helps others in similar situations
  • Re: Simulate postback from JavaScript problem in IE

    06-12-2007, 4:12 AM
    Answer
    • Loading...
    • WolfyUK
    • Joined on 07-15-2005, 5:38 AM
    • Leeds, UK
    • Posts 31

    I received a suggestion to add:

        return false;

    to the DoClick() method after debugging the JavaScript and confirming that theForm.submit(); was being reached. This seems to have resolved the problem as it prevents the browser from trying to do 'anything else' once the submit request has been processed.

    Thanks for your help!

Page 1 of 1 (7 items)
Microsoft Communities
Page view counter