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

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.

Related

Need a method to capture javascript events in WinForm webbrowser control

I have a WinForms app that uses a .NET webbrowser control. What I need to do, is wire up an event on the WinForms side to fire when a value is set (via javascript) in the loaded HTML page. I had success doing this with an onclick event of a button, but I can't seem to get it to work with a custom event. I don't know if this is a limitation in what the browser control can attach to event wise.
So essentially, I need that when a JS function is called in the HTML page and sets a value of a hidden input element (or it could be a regular input that I style to be hidden), I need to know that in WinForms. If it helps, I am using browser flags in this application to emulate IE11 instead of the default IE9 engine. The HTML page loaded is also mine so I can modify it any way needed to make this work properly. I would just use the onclick events of the buttons, but this is a gmaps integration where there can be upwards of 2000 buttons generated (one per marker placed) so it seems like a huge waste of resources to wire up 2000 onclick events when any of those button clicks only modify 4 input fields with the data I care about.
This project happens to be in VB.NET, but C# solutions would be fine as well. They can be transcoded or if the solution uses C# specific features, we can move this to a separate DLL and reference it.
After spending a lot of time on this today, I found a solution. It isn't a direct solution to the problem I posted, but it got me to where I needed to be. I am still interested in an answer to the original problem if anyone has one, but for now, what I found I could do was to create a class in .NET that I could assign to the ScriptingObject of the browser control and then call window.external.myFunctionName, where myFunctionName is a function within the .NET class. This works great for my specific problem, but would not work if I didn't also control the HTML page I was consuming with the browser. That is why I am still interested in alternate solutions if anyone has one. Thanks.

Make a button that saves html page in Chrome (locally)

I prefer Chrome, but I would like it to operate also in other browsers. So, how to make a simple button that will onClick call a function to save html page as html file? Pretty much the same like user presses Right click > Save As.
I have two buttons already, window.print() for printing and location.reload() for refreshing the page, and I must add that this html page is running locally, so there's no web server.
Is it possible with onClick JavaScript as two examples above?
I don't think this is possible, as this would likely be a security violation.
You mention specifically that you are running this locally. Does that mean you are trying to create some sort of application? If so, you might throw it in to something like Brackets Shell. If you put it in there, you can implement your own native function to trigger the "Save As" dialog. It'd be a bit more complicated, but probably one of the only ways to accomplish it if you really needed an on-page Save As button.
This is not currently possible using pure JS, but you can iterate through the document and export the contents to a file manually.
See this similar post: Export DOM

ASP.NET, Java, VB and AJAX

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.

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.

How do I do ajax navigation on pageload?

I have a page with several reports that are produced via ajax calls. I am prototype.js framework on this page for some of the display functions.
The links for each report have anchors/tags like #Report1, #Report2 etc, which are hrefs with onClick functions that do lots of work to create the report via javascript.
I would like to make it so if a user bookmarks a page with a link or navigates directly with a anchor/link in the url for my page to load the report.
So if the user goes to : http://mysite/myPage.jsp#Report2 it should load the page and go to the 2nd report.
Is there anyway in my pageload I can look at the anchor/link and perform the onlcick for that anchor? I was thinking I could create a big case/if statement to figure out what to do, but maybe there was an easier way.
It all depends on how you're Ajax calls are structured really. I do something similar for opening the correct tab within a tab navigation. The code would start off like this, if you let me see how your Ajax events are hooked up then i should be able to show you the rest.
document.observe("dom:loaded", function() {
if(window.location.hash){
var report = window.location.hash.replace("#","");
}
});
EDIT
Looking at your code you would be much better off (imv) switching to an unobtrusive method where you attach events to your elements e.g.
$('ele').observe('click',doStuff.bindAsEventListener($('ele')));
This would enable you to more easily connect the same functionality to a click or a pageload but is also better practice anyway and would prevent code duplication etc. Obviously this is missing large chunks but hopefully you get what i mean

Categories

Resources