473,466 Members | 1,391 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Simulate postback from JavaScript problem in IE

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:Buttonto 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
Jun 11 '07 #1
9 3809
On Jun 11, 11:56 am, Marc Woolfson
<MarcWoolf...@discussions.microsoft.comwrote:
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:Buttonto 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
Because it cannot find 'TheButton'. If you open the source of the page
(View Source) you will see that <asp:Button ... runat=serverhas been
converted to HTML input field with ASP.NET id. To find the control you
should call ASP.NET contol's property:

document.getElementById('<%=TheButton.ClientId%>') .click();

Jun 11 '07 #2
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

"Alexey Smirnov" wrote:
Because it cannot find 'TheButton'. If you open the source of the page
(View Source) you will see that <asp:Button ... runat=serverhas been
converted to HTML input field with ASP.NET id. To find the control you
should call ASP.NET contol's property:

document.getElementById('<%=TheButton.ClientId%>') .click();
Jun 11 '07 #3
On Jun 11, 1:55 pm, Marc Woolfson
<MarcWoolf...@discussions.microsoft.comwrote:
Hi,

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

I tried to test your code and it works for me in IE (v.7)

<asp:button id="btnLogin" runat="server" text="Login"
OnClick="btnLogin_Click"></asp:button>

<script>
theAnchor = document.createElement("a");
theAnchor.href = "javascript:void(0);";
theAnchor.onclick = DoClick;
theAnchor.innerHTML = "Click me";
f = document.getElementById("form1");
f.appendChild(theAnchor);
function DoClick() {
document.getElementById('btnLogin').click();
}
</script>

A code for the form and button:

<form name="form1" method="post" action="...." id="form1">
.....
<input type="submit" name="btnLogin" value="Login" id="btnLogin" />

Jun 11 '07 #4
use the javascript debugger, and step into __doPostBack to see whats
canceling its call to form.submit(). for example, a validation routine
may be getting a script error, which would cause this behavior.

-- bruce (sqlwork.com)

Marc Woolfson wrote:
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

"Alexey Smirnov" wrote:
>Because it cannot find 'TheButton'. If you open the source of the page
(View Source) you will see that <asp:Button ... runat=serverhas been
converted to HTML input field with ASP.NET id. To find the control you
should call ASP.NET contol's property:

document.getElementById('<%=TheButton.ClientId%>' ).click();
Jun 11 '07 #5
I have just tested this in IE7 like yourself, and it works properly.
Unfortunately I need the backwards-compatibility with IE6.

Marc

"Alexey Smirnov" wrote:
Okay, I see, it's not the case.

I tried to test your code and it works for me in IE (v.7)

<asp:button id="btnLogin" runat="server" text="Login"
OnClick="btnLogin_Click"></asp:button>

<script>
theAnchor = document.createElement("a");
theAnchor.href = "javascript:void(0);";
theAnchor.onclick = DoClick;
theAnchor.innerHTML = "Click me";
f = document.getElementById("form1");
f.appendChild(theAnchor);
function DoClick() {
document.getElementById('btnLogin').click();
}
</script>

A code for the form and button:

<form name="form1" method="post" action="...." id="form1">
.....
<input type="submit" name="btnLogin" value="Login" id="btnLogin" />

Jun 11 '07 #6
Hi Bruce,

I have just tried what you have suggested and it seems to step right through
to theForm.submit() without a problem. It just does not want to postback!

I also tried adding a spin wait to confirm that it wasn't just posting back
'too quickly' and this backed up my theory above (in FF/IE7 it spin waits
correctly when the item is clicked).

Marc

"bruce barker" wrote:
use the javascript debugger, and step into __doPostBack to see whats
canceling its call to form.submit(). for example, a validation routine
may be getting a script error, which would cause this behavior.

-- bruce (sqlwork.com)
Jun 11 '07 #7
On Jun 11, 6:28 pm, Marc Woolfson
<MarcWoolf...@discussions.microsoft.comwrote:
Hi Bruce,

I have just tried what you have suggested and it seems to step right through
to theForm.submit() without a problem. It just does not want to postback!

I also tried adding a spin wait to confirm that it wasn't just posting back
'too quickly' and this backed up my theory above (in FF/IE7 it spin waits
correctly when the item is clicked).

Marc

"bruce barker" wrote:
use the javascript debugger, and step into __doPostBack to see whats
canceling its call to form.submit(). for example, a validation routine
may be getting a script error, which would cause this behavior.
-- bruce (sqlwork.com)- Hide quoted text -

- Show quoted text -
add return false

function DoClick() {
document.getElementById('TheButton').click(); return false;
}

Jun 11 '07 #8
Excellent! Thanks very much.

Marc

"Alexey Smirnov" wrote:
>
add return false

function DoClick() {
document.getElementById('TheButton').click(); return false;
}
Jun 12 '07 #9
On Jun 12, 10:10 am, Marc Woolfson
<MarcWoolf...@discussions.microsoft.comwrote:
Excellent! Thanks very much.

Marc
Hej... great that it's working now :-)

Return false is a must, otherwise the normal behavior
"javascript:void(0);" is executed. I didn't recognized it at the first
glance, because of the function and IE7 :-))

The question is why did they change it for IE7?

Jun 12 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Earl Teigrob | last post by:
I did a ton of searching to try and find a simple solution to this issue and finally wrote my own, which I am sharing with everyone. In my searching, I did find a very complete and robust solution at...
21
by: Martin Eyles | last post by:
I am trying to get javascript to cause a page to post back. I have tried calling _doPostBack from my script, but generates an error "object expected". I think this is because the page's script...
5
by: Psych971 | last post by:
Hi, I'm wondering how I can generate a postback using javascript on a page that does not have any controls with the auto-postback property set to true. I know I can just use the submit() function...
1
by: Steve M. | last post by:
Hello all. I have a dropdown list: <select name="select" class="mapMenu" onChange="JumpToIt(this)"> <option selected value="none">Select a Tool --</option> <option...
7
by: Tony Girgenti | last post by:
Hello. I'm trying to undetrstand ASP.NET 2.0 and javascript. When i have a button and i click on it and i see the web broswer progress bar at the bottom do something, does that mean that there...
4
by: Harry | last post by:
Hi, This aspx page (let's call it thispage.aspx) fetches data from a sqldatasource, then performs several things (in code-behind) and, to simplify, passes data from code-behind via a hiddenfield...
4
by: Peter | last post by:
ASP.NET I have an application which use ASP.NET Autocomplete extender which works great. But I have a question how to update all the fields on the screen using Ajax. Users starts typing in a...
4
by: c0mm3nt | last post by:
Hi Guys. I'm writing a web crawler (web spider) that crawl all links in a website. My application is a Win32 App, written in C# with .Net framework 3.5. Now I'm using HttpWebRequest an...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.