Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Javascript calling an ASP.Net object.click?

Status
Not open for further replies.

ThatRickGuy

Programmer
Oct 12, 2001
3,841
US
The battle continues...

So I have an image button. in the code behind I add an OnClick attribute to it, the OnClick calls a JavaScript method called "PlaySound". PlaySound then checks to see what state the sound is in (Playing/Stopped/Finished/Paused). If the sound is paused, I need to click on the pause image button. I do this by calling document.getElementById('bsibPause').click();

bsibPause (a glorified image button) also has an OnClick attribute added that calls "PauseSound". There is also a click event that is handled in the Code Behind page.

The JavaScript OnClick and the PauseSound method work correctly, but the Click event handled on the back end is not getting fired. The buttons work correctly when the user clicks on them.

Thoughts?

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Here is a super stripped down sample:

In the code behind page:
Code:
Protected Sub ImageButton1_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ImageButton1.Click
  MsgBox("Button Clicked, backend")
End Sub

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  Me.Button1.Attributes.Add("onclick", "alert('button1 clicked'); document.getElementById('ImageButton1').click;")
End Sub

HTML:
Code:
<form id="Form1" runat="server" >
  <asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="Images/btn_Upload.PNG" />
  <asp:Button ID="Button1" runat="server" Text="Button" />     
</form>

It appears that the JavaScript object.Click() method is not actually causing a postback, it is just calling what ever javascript is associated with the client side click event.

Anyone know how to get around that?

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
If you say you aren't seeing it becuase you don't see a messagebox popup with this code:
Code:
MsgBox("Button Clicked, backend")
Then you are incorrect. The messagebox will only appear on the server, not the client. Use a Response.Write() statment to check it, or show/hide a label if the event runs.
 
Yeah, I'm running it locally. When I click on the ImageButton, the pop-up occurs, when I click on the Button, it does not. I was just using msgbox because it's immediately obvious, my current code does not use any msgboxes, but is still not occuring :(

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
The reason I asked was because if the image doesn't exist, clicking won't actually call the server side event (at least in Firefox anyway) so at first glance I wondered if that was the problem.

After trying out your code, it seems that isn't the problem though and it is something else. I found this which suggests it may be a javascript/HTML issue rather than something you've done in ASP.NET.

After trying it out without ASP.NET, I'd say that's definitely the case as the same happens in a regular HTML page.Try the js forum and see if any of those clever people know how to do it!


____________________________________________________________
Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]

Need help finding an answer? Try the Search Facility or read FAQ222-2244.
 
Bah, easy fix... Just had to pull my head out of Javascript for a while. I have access to the server side events for both objects. So I just added a public .PerformClick method to my custom control and call that from the server side click event from the first control.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
BTW Ca8msm, how did you recreate this without using ASP.Net?
I did it in classic ASP (as I was just trying to rule out whether it was an ASP.NET issue or not) and the form didn't submit correctly.


____________________________________________________________
Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]

Need help finding an answer? Try the Search Facility or read FAQ222-2244.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top