473,406 Members | 2,390 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Programmatically click asp.net button via javascript?

Hi

I have a server-side (asp.net) method that can be called by clicking a
<asp:button> on my webpage. This method should also be called every n
seconds, so I've set up a javascript timer which fires every n seconds.
The problem is that I then don't know how to call the server-side
method.

I figured that since I already have a button calling that method, I
could just emulate the button click, but I've tried two methods and
neither seem to work. document.AllocationsForm.LoadButton.click()
complains that document.AllocationsFrom.LoadButton is null or doesn't
exist, and __doPostBack('LoadButton', '') does a postback but doesn't
actually execute the method.

Nov 19 '05 #1
6 4975
In situations like yours I usually introduce a hidden input control for
passing an action code from client to server side. It is much simpler than
trying to mimic exact client-side button click handling.

Eliyahu

"Cathryn Johns" <cj****@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
Hi

I have a server-side (asp.net) method that can be called by clicking a
<asp:button> on my webpage. This method should also be called every n
seconds, so I've set up a javascript timer which fires every n seconds.
The problem is that I then don't know how to call the server-side
method.

I figured that since I already have a button calling that method, I
could just emulate the button click, but I've tried two methods and
neither seem to work. document.AllocationsForm.LoadButton.click()
complains that document.AllocationsFrom.LoadButton is null or doesn't
exist, and __doPostBack('LoadButton', '') does a postback but doesn't
actually execute the method.

Nov 19 '05 #2
Try:

var btn = document.getElementById(btnName);
if (btn) btn.click();

as I did in this sample:

http://www.societopia.net/Samples/asppage_buttons.aspx
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"Cathryn Johns" wrote:
Hi

I have a server-side (asp.net) method that can be called by clicking a
<asp:button> on my webpage. This method should also be called every n
seconds, so I've set up a javascript timer which fires every n seconds.
The problem is that I then don't know how to call the server-side
method.

I figured that since I already have a button calling that method, I
could just emulate the button click, but I've tried two methods and
neither seem to work. document.AllocationsForm.LoadButton.click()
complains that document.AllocationsFrom.LoadButton is null or doesn't
exist, and __doPostBack('LoadButton', '') does a postback but doesn't
actually execute the method.

Nov 19 '05 #3
On Tue, 01 Nov 2005 04:40:41 -0800, Cathryn Johns wrote:
Hi

I have a server-side (asp.net) method that can be called by clicking a
<asp:button> on my webpage. This method should also be called every n
seconds, so I've set up a javascript timer which fires every n seconds.
The problem is that I then don't know how to call the server-side
method.

I figured that since I already have a button calling that method, I
could just emulate the button click, but I've tried two methods and
neither seem to work. document.AllocationsForm.LoadButton.click()
complains that document.AllocationsFrom.LoadButton is null or doesn't
exist, and __doPostBack('LoadButton', '') does a postback but doesn't
actually execute the method.


Candidate for Ajax?

Nov 19 '05 #4
Nope, that doesn't work either - if (btn) just returns false. I assume
that this is because in your sample, the button is a regular html
<input type="button"...>, whereas mine is an actual asp.net button
(<asp:button ....>).

Any ideas on how to click an asp.net button?

Phillip Williams wrote:
Try:

var btn = document.getElementById(btnName);
if (btn) btn.click();

as I did in this sample:

http://www.societopia.net/Samples/asppage_buttons.aspx
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"Cathryn Johns" wrote:
Hi

I have a server-side (asp.net) method that can be called by clicking a
<asp:button> on my webpage. This method should also be called every n
seconds, so I've set up a javascript timer which fires every n seconds.
The problem is that I then don't know how to call the server-side
method.

I figured that since I already have a button calling that method, I
could just emulate the button click, but I've tried two methods and
neither seem to work. document.AllocationsForm.LoadButton.click()
complains that document.AllocationsFrom.LoadButton is null or doesn't
exist, and __doPostBack('LoadButton', '') does a postback but doesn't
actually execute the method.


Nov 19 '05 #5
Nope :-)

The buttons in my demo are <asp:button> and they do work. Another demo is
the City of North Vancouver's website (which I developed) where the search
button is also an asp:button and working using btn.Click() too.
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"Cathryn Johns" wrote:
Nope, that doesn't work either - if (btn) just returns false. I assume
that this is because in your sample, the button is a regular html
<input type="button"...>, whereas mine is an actual asp.net button
(<asp:button ....>).

Any ideas on how to click an asp.net button?

Phillip Williams wrote:
Try:

var btn = document.getElementById(btnName);
if (btn) btn.click();

as I did in this sample:

http://www.societopia.net/Samples/asppage_buttons.aspx
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"Cathryn Johns" wrote:
Hi

I have a server-side (asp.net) method that can be called by clicking a
<asp:button> on my webpage. This method should also be called every n
seconds, so I've set up a javascript timer which fires every n seconds.
The problem is that I then don't know how to call the server-side
method.

I figured that since I already have a button calling that method, I
could just emulate the button click, but I've tried two methods and
neither seem to work. document.AllocationsForm.LoadButton.click()
complains that document.AllocationsFrom.LoadButton is null or doesn't
exist, and __doPostBack('LoadButton', '') does a postback but doesn't
actually execute the method.


Nov 19 '05 #6
CJ
Found the problem, and you're right, it had nothing to do with asp.net
buttons :-)

The button is in a .ascx user control which is sitting in a .aspx page,
and I had to prefix the button name with the name of the user control
WebUI element. Once I figured that out, it all worked smoothly.

Nov 19 '05 #7

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

Similar topics

3
by: madforplaid | last post by:
Can anyone help me to figure out a strange span/radio button/JavaScript behavior? When clicking on the radio button for WEEK 2 below all works okay, but when clicking on the label for the same...
0
by: [Yosi] | last post by:
I search solution for a little bit strange problem , I working on an application which includes a button. Click on this button will create number of threads each one execute deferent executable...
5
by: Steve | last post by:
Hi, Is it possible to make hitting the enter key in an ASP textbox run the code behind an ASP button on a form? I have a search page which users tend to type in the query then just hit enter...
3
by: greg | last post by:
Hi I need to programmatically click imagebutton from javascript so I write in a link navigate url document.forms.nextBtn.fireEvent('onclick'); !!!!! it says nextBtn undefined it is a web...
1
by: dinotius | last post by:
Hi, Can anyone please tell me how to respond to an event in VB when a programmatically created button is clicked? I can get it to work if the addhandler event is in the pages Page_Load sub but...
4
by: Bob | last post by:
Hi, I'm working with VWD and i defined programmatically a button (in code-behind) with an ID. So I expect to see beside "Page Events" and "Form1" my button "b1" in order to use the Click event....
0
by: radders | last post by:
Hi guys, What I'd like to do is place three edit boxes with three static text labels on a click button. This would let the user input RGB values to the three edit boxes and then click the...
3
by: eddie69 | last post by:
Hi, Newbie question here: I am creating an application in VB Studio 2005 which reads the status of an USB PIR Motion sensor. So far, I have managed to get the application to read the motion...
12
by: Thammarat charoenchai. | last post by:
Hi, I'm try to learn vb.net. in delphi have .click method for click button by coding. Can I do that with vb.net Thank you very much.
5
by: laziers | last post by:
Hi, anyone know how to write a single-cilick button? I use this: <asp:button id="ButtonAdd" runat="server" text="Anuluj" onclick="Click_ButtonAdd" /> if ( !isPostBack){...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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...

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.