I am trying to call a VB.Net subroutine on my ASP.Net page via a function key. I have managed to detect the key press using a Javascript function and am now trying to call the VB.Net subroutine from Javascript. I have seen 2 methods proposed, but have had no success with either:
Using jQuery
Using Web Methods
1) With jQuery, I get an error message in the web browser that says PageMethods undefined.
I am using a ScriptManager with EnablePageMethods = “True” and have syntax “PageMethods.VBsubroutine() in the Javascript function, but it seems PageMethods is not recognised.
2) I have also declared the WebMethods as follows (for testing purposes):
<System.Web.Services.WebMethod()> _
Public Sub ToggleTestButton()
If btnTest.Text = “Alpha” Then
btnTest.Text = “Omega”
Else
btnTest.Text = “Alpha”
End If
End Sub
Some have suggested that WebMethods should be declared as
<System.Web.Services.WebMethod()> _
Public Shared Function xxx()
But I get an error with the control btnTest.Text being underlined. Something about having to create an instance of objects? The issue is with the Shared. Besides I’m not intending to get any output from the function, hence the use of Sub.
Some have suggested that it must be Public Shared Function. Please advise on why this is so or if there is another way of achieving the same.
The webmethod route has no access to the elements on the page, and unless I'm mistaken should only be used when one requires to execute a sub asynchronously.
The best way to do this in my opinion would be be to place a hidden button on the page, then from the js that detects the function call, click the button.
Do you want to call vb.net function on your asp.net server, when the user presses some key in his browser (client side)?
If yes, then you may try ajax.
Related
I have a caching problem on a button event in Java EE. In JSP, I call an inline JavaScript method in JSP page. This method requests a function in a Java class. Communication between the JSP and the method of java class occurs via structs. The problem is that after I modified this java class, all JSP screen links squeegee when the application calls the same JavaScript method. But this method is only called through a onClink() event is a screen button. Now all JSP buttons are calling this method, even if your event JavaScript onClink is calling another method. If the problem is not the call of JavaScript methods, how do I solve this problem? Refreshing this page does not.
An alternative would be to clear the cache only javascript. And keep the session java application and any other infromação of aplciação java web browser. The problem began to occur when I made an appointment in a java method using as parameter the "ApplicationForm" that takes the form data and use it as research. But for some reason this confunfoiu the browser
I do not want to sound ridiculous, but I can not put the code here.It is copyrighted and owned the company. But it is quite simple. Generic something would be.
JSP Page
The method includes call
// code jsp
<script>
incluirChamada function () {
// Calls the "javascript: incluirChamadaJava" java class via structs
}
the problem is that all the buttons call the incluirChamada () method. even if your onclink are specifying to another class
In the java code ...
javascript: incluirChamadaJava (ApplicationForm form) {
mehod slectUsingHibernate (Filter form) {
}
}
debug your code. You problem have some function that is not closed properly.
I want to get the answer of a window.prompt() alert box through the C# Code Behind file. It's just a one-liner of JavaScript code, so I thought I could even execute it from the Code Behind file. It doesn't really matter to me whether it's a <script> tag in the .aspx file or executed through the .aspx.cs file.
I thought of having the script executed (called from the C# part) and then having the return value assigned to a certain not visible field, but is there any better way to do it?
The obvious way would probably go something like this:
.aspx file:
<script>
function foo() {
document.getElementById('MyFieldID').value = window.prompt('Answer this question:', '');
}
</script>
.aspx.cs file:
////////////////////////////////////////////////
//MyFieldID.Text now contains whatever I want//
//////////////////////////////////////////////
What do you say? Is there any better way?
Better way is always opinion based. What I'd say is you have a few options, all depend on what you're doing. HTTP and ASP.NET provide us a few means of sending data to the server, there are 3 main ones before HTML5:
Query string
Form values
AJAX calls
If you're redirecting the user to a new page after they answer the prompt, you can just send them to yournewurl.aspx?promptAnswer=*whatever*
If you're doing a postback, then you can use a form value (this looks like what you're doing in your example). You can put an <asp:HiddenField> on the page and populate it from JavaScript before submitting the form.
If you need just the prompt response, but are not attempting to reload the page, you can make an AJAX call that sends the variable to the server (this still uses #1 or #2 to send the data, it just does it without reloading the page).
Which of those three options works best depends on your implementation. However, your solution should work just fine. However, since the control you'd likely be using to stuff the value into is a HiddenField it would be in MyFieldID.Value not MyFieldID.Text. The only other thing you have to deal with is if your MyFieldID is nested in some other controls (like a ContentPlaceHolder) such that the ClientID has naming containers pretended to it so it's really something like ContentPlanceHolder1_MyFieldID when accessed from JavaScript.
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
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.
I have this javascript
function myFunction(source) {
window.open(source, "Title", 'width=400, height=400');
}
and in c# code I call it by
HtmlPage.Window.Invoke("myFunction", source);
which opens up a aspx page and goes to its OnLoad function:
protected override void OnLoad(System.EventArgs e)
{
base.OnLoad(e);
My questions are
how do I pass in parameters to the event args in the aspx page? or is that even possible?
and how can I pass these parameters from my c# code to the javascript?
I am still learning javascript so please explain.
Thanks,
Voodoo
You need to add a query string to the URL, such as http://server/path.file.aspx?SomeName=SomeValue.
You can access in in the C# server-side code by checking Request.QueryString["SomeValue"].
PageLoad happens on the serverside before the page has been sent down to the client's compter where the javascript is executed. Have a look at the asp.net page lifecycle It used to be one of our stock interview questions for web devs and it's amazing how many asp.net devs don't know it
SLaks answer is the easiest and most of the time will probably do you well, however you could always use an ajax operation to send some data back to the server and affect the page. This is more complicated but more powerful and leaves your URL looking nice and clean