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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Invoking button click event from server side

Status
Not open for further replies.

alphajava

Programmer
Mar 30, 2005
17
US

I am trying to programmatically invoke button click event. I used RaisePostBackEvent() and it does execute the OnButtonClick function for that button.
I am associating a javascript Attribute to that button, and this attribute is not getting executed when I call invoke button click using RaisePostBackEvent(). However, when I click the button from the browser GUI, the javascript is executed!
How do I make it behave the same way in both cases?
Thanks.
 
Just call the handler of the button click directly.

OnButtonClick (nothing, nothing)
 
Or place the code from your button in a class/function that makes it accessible to any caller.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 

No, here is my situation:
I am associating a javascript Attribute to a button, so when someone clicks that button the javascript is executed first and this javascript is a YesNo MessageBox. If user clicks No the OnClick function of the button is not executed. If user clicks Yes then the OnClick function of the button is executed.
I can call OnClick directly, but I wont get the javascript MessageBox executed! And that is not what I want. Therefore maybe the only solution is to simulate “user button click event” and I do not know how. I tried using RaisePostBackEvent() which will raise postback event for that button, and it did, and the code in that OnClick was executed but the javascript code was not executed!
I do not know how to make the server thinks that the user clicked a button.
 
If you RaisePostBackEvent, you will simulate a PostBack, and relevant server-side code will run.

The only place you could call "OnClick" directly is in server side code, and the server can't run your client JavScript. How would it?

What you're trying to do isn't possible, because the web is client-server, or request-response.

Scenario 1:

1. user clicks a button
2. client-side script runs
3. user clicks "yes"
4. form submits, generates a Request
5. server processes request, generates a Response

I take it you want to have an alternate Scenario 2:

1. form submits, generates a Request
2. server processes Request
2a. server code runs button OnClick Handler
2b. server (somehow) generates a "mini-Response"
2c. User sees a JavaScript-generated Confirm() box
2d. server enters a "waiting mode"
2e. User/browser clicks "yes", generates a "mini-Request"
2d. server somehow syncrhonizes the two requests
3. server generates a Response



Thomas D. Greer
 
What I am hoping to be able to do is:
1- User enters some value in textbox and clicks button.
2- I do several checks on the value entered, and based on that I might want to prompt a messagebox, and based on the response from the messagebox I’ll decide to process some code or not.
I am writing my web form in c# using asp.net.
Unfortunately all messageboxes are on the client side and this is why I am trying to use javascript.
If someone has solution please let me know. Thanks
 
[link="[URL unfurl="true"]http://www.tgreer.com/aspnet_html_03.html"[/URL]]Integrating client-side scripts with ASP.NET[/url]

Thomas D. Greer
 
I looked at this site the problem with this approach is that once you associate a javascript function with a button, that javascript function is always executed whenever the button is clicked. I want to be able to decide when to call that javascript function, for example if user entered correct stuff, there is no need for an alert messagebox.
I wish that there is away that I can invoke the alert javascript function whenever I want, that will solve my problem.
 
Yes, of course if you associate an event with an event handler, the event handler is called when the event occurs. If that's not what you want to do, then don't do that. ("Doc, it hurts when I lift my arm". "Then, don't lift your arm!")

What you want to do, from what I can tell, has nothing to do with ASP.NET. This is purely a JavaScript question. You might post in the JavaScript forum if things still aren't clear after this.

You create a JavaScript validation function. You assign it to the onsubmit handler of the form. When the user submits the form, the validation function runs.

If the user enters "correct stuff", then "return true". Your form will submit, and ASP.NET can take over.

If the user does not enter "correct stuff", then perform an alert() and "return false". The form will not submit.

The methods to create popup prompts in JavaScript are "alert()" and "confirm()". The term "MessageBox" refers to a Windows Form dialog box.

Thomas D. Greer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top