How to c# function using javascript - javascript

I have input HTML button(Not ASP.NET Button) click event in c#
Is there any way i can call c# function using javascript
Thank you very much....

Probably the easiest and most straightforward way will be to create an HTTPHandler on your ASP .NET site and then your JavaScript can make an AJAX call (easily done with jQuery, note the three links to three different methods, and there are likely more) to invoke that handler at its configured URL.
Additionally, does this method need to return anything? From the jQuery side your best bet will be to receive back a JSON object. This can be done pretty easily from the .NET side with the JavaScriptSerializer or even manually.

You need to use the MS Ajax JS libs. These will wire up things like that for you... but it has to be an ASP.NET button. Sounds like you want to make an AJAX Service call. Give us some more details as to how this is setup and how you want it to work. ASP.NET Forms is not very forgiving to simple buttons and JS... you need to think about viewstate.

Related

WPF/ChromiumWebBrowser executing Javascript function (With parameters!)

So I'm working on a WPF app that has a web page self hosting, and in my Javascript I need to use some data from my C#. I'm wondering if there's any way (in WPF/C#) to invoke a Javascript function WITH a parameter, or if the best way to accomplish this would just be to dump the parameters to a text file and use the Javascript to read it in again, which I'd strongly prefer not to do.
Thanks!
Thanks for responding, but the class I'm using is actually the ChromiumWebBrowser, part of CefSharp. I didn't think to google that and i actually found this How to pass inputs to javascript functions with cefsharp
Thanks!

Listen to Response on HTML Form embedded in GWT View?

I have a HTML like the following:
<div>
<form>
<input type="text" />
<button class="sendForm" value="Send form" />
</form>
</div>
<script>
// post the form with Jquery post
// register a callback that handles the response
</script>
I use this type of form a lot with a JavaScript/JQuery overlay that displays the form. That could be handled for example with plugins like FancyBox. I use Fancybox for Ajax content.
I also want to use this form embedded into a GWT view. Lets assume that the for cannot be created on client side because it has some server based markup language inside to set up some model data.
If I want to use this form in GWT I have to do the following. Tell GWT the form request url and use a RequestBuilder to query the html content of this form. Then I can insert it into a div generated by GWT. So far so good.
Problem:
When the user hits the send button the response is handled my the JQuery callback that is inside the script under the form.
Is there a way to access this callback from within GWT?
Is there a way to overwrite the JQuery send action? Since, the code is HTML and comes from the server I cannot place ui-binder UiFields inside to get access to these DOM elements.
I need to get the response if the submitted form accessible to GWT.
Is there a way how I can achieve this with JSNI?
Answers to each question:
1 Is there a way to access this callback from within GWT? actually you cannot modify the callback itself, what you can do from GWT is to call any jquery method, thus you can unbind any previous added handler, and set yours.
//NOTE: not wrapping code in $entry() to make a clearer code.
private static native unbindForm() /*-{
// remove all jQuery handlers added previously to my form
$wnd.$("my_form_selector").off();
// add a new handler for the submit event
$wnd.$("my_form_selector").on("submit", function(event) {
event.preventDefault();
$wnd.$(this).post(url, ...).done(function(data) {
// Use JSNI to call a GWT method
#.com.example.MyClass.handleFormResponse(*)(data);
// NOTE: that you can use a star instead of having to write parameter
// types, when you only have one method signature in your class.
});
}
}-*/
// handle the form response in GWT
public static void handleFormResponse(String data) {
// handle form response in GWT
}
Another thing you can do with GWT, is to wrap your form in a FormPanel and use specific widget methods to set a handler and read the response. But I think this is not what you are asking for.
2 Is there a way to overwrite the JQuery send action Yes, using JSNI to ask jQuery to unbind previously set events. See code in #1.
3 I need to get the response if the submitted form accessible to GWT. You have to include in the jquery.post callback some code to call GWT static methods (you can use-non static as well but this way is easier) this is also JSNI. See code in #1.
4 Is there a way how I can achieve this with JSNI? Of course, JSNI is the way to interact with handwritten javascript from GWT.
Aditional Notes:
GWT is designed to build RIA apps with very optimized js code. I know each one has their reasons for using external libraries, but adding 3party javascript to your app is against the main goals of gwt compiler remove death code and optimize output. If you like jquery like syntax and features for GWT I recomend to use gwtquery, which has been fully written in gwt, hence the compiler will include just the code you use.
Writing JSNI is a source of mistakes difficult to handle in the debugger. So I recommend to use gwt-exporter to populate java methods/classes or gwtquery to call external javascript. See these post I wrote some time ago: Pass value from GWT to Javascript via JSNI and Calling GWT Java function from JavaScript
Javascript => GWT and GWT => javascript values passing both can be done using JSNI . Please have a look here for more information about JSNI

MVC3 Using Javascript to postback similar to how $.post works -- but with an actual postback?

I have an ASP.NET MVC3 app that features a form with a nested-table input
(Ie on each row I can add a sub-table, with no limit on depth)
To handle this for my MVC app, I've created 2 javascript classes(using this term loosely with js:) that mirror my MVC3 model and post the data to an action method. Everything works great...Except that right now the only way that I know how to do this is with jquery $.ajax or $.post --- How can I do a postback in javascript?
I have the URL, and the custom JSON data, and want to do a page postback... Any suggestions? I can't use the normal form submit due to the nested table scenario described above.
Also, I just want to say, that MVC has made this so simple to render! :) For rendering a recursive view did everything without any script required, only on the saving did I need to screw around with json.
Update:I guess another solution would be -- can I change the contents of my form data on submit? My method takes a JSON object, is there any way I can stuff that in my request while my form submit is happening normally?
You can use the XML Http Request to do this. This is eventually what jQuery and other JS libraries use.
But why don't you just stick to jQuery AJAX or POST?
Maybe I'm misunderstanding your question, but it seems like what you want to do is post to the same page you are on, which means if you have the URL (and it sounds like you do), you just need to specific that in the $.ajax method? Maybe you can clarify what you mean a little bit for us.
Edit: Per comment suggested looking at http://jquery.malsup.com/form/
Well, I found that with the MVC3 binding, the table in my form could be normally bound if I named the fields such as Item[0].Children[1].Children[0].FieldA... etc, everything matched up fine without having to convert to javascript objects/json. I changed my code to fix this naming before a form submit, and it binds pretty well without having to do any json calls at all. Less elegant, but I guess it works.

__doPostBack method

I want to know why we use __doPostBack methods. Is it a build-in JavaScript method or a user defined function and what does this code do?
onclick="javascript:setTimeout('__doPostBack(\'SectionA$1\',\'\')', 0)
I am getting an error for this method:
object expected
doPostBack is a javascript function that asp.net uses to submit the main form for executing the server-side code of your application.
Buttons are postbacks after clicking on them, however some other controls does not postback. So they calls the doPostBack javascript for simulate the postback action.
In your case, you might having trouble with your server-side control. This usually happens while having some problem about configuring the controls that uses the mechanism above. So you are interested with the wrong side now I suppose.
It's __doPostBack with two _ characters.

Usercontrol doesnt support Webmethod?

I want to call a Scriptmanager webmethod in usercontrol. But Webusercontrol does not support WebMethod. Can it be achieved by Jquery Ajax or any other way?
You need to put that web method in your page's code behind class of the user control code.
For details take a look at this article https://web.archive.org/web/20211020131232/https://www.4guysfromrolla.com/articles/031704-1.aspx
I would put your web methods in web service.asmx file as it is easier to manage that way.

Categories