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