ASP.NET, Java, VB and AJAX - javascript

I have an ASP.NET application with a button that executes VB.NET on the server when clicked.
Specs have changed, I've added a menu of sorts which is to replace the VB button. With some help from S.O., I've managed to manipulate some javascript which does a postback and executes the button's code. I figured I could just make the button invisible and still be able to call it's on_click event from js. How wrong I was!
So now, somehow I'm supposed to call a VB sub from either javascript or (boss says) ajax. I have no idea how to do this.
Could anyone give me a good direction as to how I can call a VB.NET subroutine from ajax on the client? Or javascript?
Thanks in advance,
Jason

It's not obvious that you could just switch to Ajax from a postback scenario, whatever your boss tells you. :-) Lots of stuff might happen in a postback that you don't do in an Ajax call; setting other values serverside, changing visibility and such. It's hard to tell without seeing the actual code, though.
The easiest way for you now would be to be able to "click" that button again. The problem is how you set the invisibility, some types of invisibility makes the button disappear from the form, leaving it impossible to press it even programmatically.
Again, without seeing the code it's hard to tell which way you should go, but to hide it with css will make it possible to "click" it the way you have done. Hiding it with "button.Visible = false" server side won't.

Related

Can I use javascript to detect events of asp.net?

I am now trying to use gridview to create a web application for users to delete some rows from database. However, whenever I refresh the whole page, strange things happen as selectedIndexChanged was called. Is there any way that I can avoid this method being called whenever I refresh the table or is can I use javascript to detect this event? If not, can I generate a yes no dialog for that event like javascript? I am quite new to asp.net and my questions may be quite stupid. Please help!
I think you need to decide if you want to use JS or ASP to handle events on the page.
If you want to use JS, then disable server processing in your ASP tags and handle all the interactions with JS in your page and only process at the server on page submit. ASP can generate the grid for you on page load, but then let JS handle the client side interactions.
If you want to use ASP, then set your grid to process on the server (runat="server") and forget about JS.
Well seems I found out the solution. Actually C# also provides yes no dialog. Seems I could use it to manually stop that event but it seems that I cannot avoid selectedIndexChanged being called when refreshing the page. Thank you every one for your help.

Usage of AJAX in an alert box

I have a javascript alert asking the user to insert a password. When user presses button, I will do an AJAX call to check the password. This could generate another alert with the response (if it was accepted or not), but I wonder if it is possible to have the AJAX response change the text of my first alert? Is it even possible to override the alert button's default not to close itself when clicking "okay"?
I think generally you should try and minimize the use of alerts in your web application. They are very obtrusive and annoying. Generally people just update the DOM to display user information. An alert is for something more unique and important than somebody just mistyping their password.
EDIT:
As other people are pointing out, you should use modal dialogs instead to have behavior as you specify.
I'm quite sure you can't do that since the way the alert is constructed and displayed is different in each browser (just look at mobile browsers). I think you'll safeguard your sanity longer if instead you use modal dialogs.
Being a blocking function, alert() is an abomination in itself, and owing to that would thwart the very thing you are trying to do.

Change value of text box using javascript (Jquery) before post back ASP.NET

I am trying to change the value of a set of text boxes (that hold greyed out suggestions in them) when the user presses the submit form button. This button runs a server side method using OnClientClick that submits the data and does a whole slew of other things.
Now my problem is that i can't either: fit in a javascript function that will change the values before the server gets hit, OR call the server side method in the javascript instead of the button OnClientClick event.
Ive tried:
$(this.form).submit(function(){
//Change value here
});
//using OnClientClick to call function
and
$("#"+"<%=submitBtn.ClientID %>").click(function(){
//Change values here
__doPostBack("<%=submitBtn.ClientID %>");
});
//not using OnClientClick to call server method
(pretty sure that won't work)
and
$("#"+"<%=submitBtn.ClientID %>").click(function(){
//Change values here
});
//using OnClientClick still to call function
Im stumped
Edit
Right I obviously didn't give enough info,
What happens when I use submit events is the server event fires before the JavaScript event, therefore when do a server side validation before I send the values away, I have the wrong values, there isn't any point in changing to client side validation because i will still have the same problem when I send the form data back to the db.
Update
So i still have a problem (both with this and mentally because of this).
Because of the idiots who worked on this before me (now i have to fix it) they removed the submit behaviour (asp.net) from the button at the bottom, because they use some server side trickery to figure out if some validators should be on or off (when really it should be client side that does that), hence they had to turn it off because it would fire validators if it didn't.
ANYWAY... So I'm still having trouble, the on click function for the button doesn't seem to fire in time or the scripts run simultaneously. I tested this by adding an alert and a breakpoint on the code behind, the breakpoint fires and the alert fires too. sooo..... yeah.
Is there any way i could maybe circumvent this by removing the "onclientclick" from the button and calling the function it calls in the CB?
Any ideas? (Please?)
Small update:
Still can't figure it out :(. Is anyone confused by the question?
Yup, this function should work
$(this.form).submit(function(){
//Change value here
});
But one culprit might be the commented part: "// Change values here." If you're using one of these,
$('#target').text('my new info');
$('#target').html('my new info');
...you will have trouble. You need to use .val()
$('#target').val('my new info');
The form is submitted before JS fires all events.
Have your button's onClientClick event change the values. You can call functions in sequence if you need to.
jQuery does some funky things with events and you can't be sure what order they will fire after they are attached. You must explicitly specify the functions to call.
<button id="some_button" onclick="SetValues(); SubmitForm();" />
Just stumbled across this. It's been a month since you first posted the question so you may have fixed it already. However, thought I'd add my thoughts anyway.
First, it's make sense your code is being ignored. When you bind to the submit and onclick events you functions are added to the list of events handles. Events handlers are processed from the first added to the last added. So the postback is started before your jquery code is called.
To solve this you need to change the code in the function that OnClientClick calls or write a wrapper function that calls your code then calls what OnClientClick called and change OnClientClick to call your wrapper function.
this function should do the trick:
$(this.form).submit(function(){
//Change value here
});
just make sure you enable the text fields again before you send them to the server, otherwise the server won't pick them up
I have used id of submit button to check for a "click"
http://jsfiddle.net/sreeprasad/7urvg/

Execute JavaScript on AJAX Panel Load

I want to run a JavaScript function to initialize some controls.
My problem is that the controls are on a Telerik control's form template that is displayed using AJAX.
Is there a way to specify the JavaScript function executes whenever this template is displayed?
It appears the answer is no. While there are some convoluted ways to trigger JavaScript on an AJAX postback, trying them stopped the Telerik controls from working. I guess they rely on that technique already. Anyway, I've moved on to approach the problem from a different angle.

Detecting Javascript html page modification in VB.NET Webbrowser control

My VB.NET code is supposed to execute third party Javascript code in an attempt to fill in and submit a form. This process consists of five steps, and I have been able to submit the form when all the steps are kept separate (i.e. behind 5 separate consecutive button clicks). Now, what I'd like to have is one button to handle all the five steps.
The problem is that the form originally only appears after calling "webbrowser.Navigate" command, which apparently modifies the page's HTML code. I seem to be unable to detect when Javascript has finished loading the new HTML in order to fill and submit the form. I have tried a timer control to wait for a certain HTML element ID to appear, but in vain.
This question has been asked before in different forms, but at least I could not find much help from the earlier answers:
InvalidCastException with WebBrowser.IsBusy or ReadyState (VB .NET)
Detect when AJAX changes HTML in a DIV in WebBrowser
http://www.techtalkz.com/vb-net/374234-vb-net-webbrowser-control-how-capture-javascript-events-statusbar-changed-mouseclick-etc.html
Please help me.
Well, this is the way it normally works in the software industry: Only after you've explained the problem to others, are you in a position to sufficiently understand it - and solve it yourself.
The problem was that I had not been using System.Windows.Forms.Timer() but another (less suitable) timer class for tracking changes in the HTML code. This was the reason why Application.DoEvents() did not work. With System.Windows.Forms.Timer() I was able to create a Timer.Tick event that keeps track of the phase of the form submittal (1-5 in my example) and attempts to execute the required Javascript commands in a Try-Catch construction. If an exception is caught, Application.DoEvents() is executed instead, and the timer ensures that the same commands are attempted to be executed shortly again.
This seems to work for me.

Categories

Resources