Postback without a control context - javascript

I have a situation where I want to use several server side controls, which have clients side state. I want to check the state when events occur (like various clicks), and when the state is where i want it, I want to postback to the server and do some processing.
In particular, I need to make sure that at least 3 different controls have selections before I want to do a postback. Since there is no specific control that will initiate the postback, I just want to capture the selection events on the client side, then call __doPostBack() (or something similar) to initiate the processing. So i have disabled all server side events, turned off autopostback, and have wired up some javascript to handle this.
I've got all the client side code written and working, however I cannot seem to get the server-side to recognize the postback. I'm overriding RaisePostBackEvent, and checking the eventArgument for my custom argument. This doesn't work, as RaisePostBackEvent is never called.
This method has worked when I had autopostback enabled (for example, the Telerik Radgrid OnSelectChanged server side event).
Any suggestions on the best way to handle this?
Update:
When asked for sample code, it's exactly as I say above. Not rocket science.
Javascript:
function CheckState(source, eventArgs) {
// logic to test state of controls
__doPostBack("", "DoMyWork:");
}
Then in code behind I have:
protected override void RaisePostBackEvent(IPostBackEventHandler source, string eventArgument)
{
if (eventArgument.IndexOf("DoMyWork") != -1)
{
// do my server side work.
}
}
RESOLUTION:
Because this is somewhat confusing.. I'll just say what my solution was.
I used one of the controls involved in the postback and used it's UniqueID for the control parameter, then my method as the event.

if you call the __doPostBack() without parameters, asp.net cannot figure out which control fired the event and thus it cannot determine which event to fire.
you need to add the name of the control as a parameter to __doPostBack() and an argument which could be null if you dont need one
__doPostBack('textbox1','') //no arguments
__doPostBack('textbox1','12')
you can read the argument you passed in from code behind like this:
Dim arg As String = Request("__EVENTARGUMENT")

Related

how to passthrough a csjs function via property definition

on my application I have a dialog which I re-use across my application. sometimes I call it via ssjs and sometimes csjs. the dialog contains buttons and sometimes I want for them the oncomplete event for an eventhandler to perform a csjs action and sometimes not.
I was wondering how I can pass through csjs code/function to the custom control and use it in the oncomplete event of an eventhandler.
normally I would use the compositeData method to get the value but that is ssjs.
can anyone guide me?
You can provide the CSJS, that should be executed in the oncomplete event, as string property to the custom control and compute the client-side code for the event on the server-side.
If you, for example, name the property csjsOncomplete, the computed value for the client-side event would be #{javascript:return (compositeData.csjsOncomplete || '');} .

Calling C# method (using PageMethod) from LinkButton without postback (MasterPage website)

I create several LinkButtons dynamically in an ASP page with MasterPage. In a separate JavaScript file, i have a JS function that changes the text of the LinkButton when clicked, i.e. "Yes" to "No" and vice-versa. It's sort of a "Like" button. So it's mandatory the page stays static when people click the LinkButton (would be annoying if not).
A C# method with parameters must be executed when the LinkButton is clicked.
I add this click property to the LinkButton, to use the method that is in a separate C# Class file:
MyLinkButton.Click += (sender, e) => { MyClass.MyClick(par1, par2); };
But the thing is, if i add the following property to prevent postback and keep the page static when LinkButton is clicked:
MyLinkButton.Attributes.Add("href", "javascript:;");
It no longer runs the MyClick method in the MyClass file.
So i tried to call the MyClick method from the JS code that does the "Yes" to "No" change on click using PageMethod.
In MyClass i added: the reference to System.Web.Services and put [WebMethod] above the public static void MyClick() method.
Also added a ScriptManager to my MasterPage file inside body/form and set the attribute EnablePageMethods="true".
The problem is, with or without the ("href", "javascript:;") attribute set on LinkButton, the PageMethod is not calling the MyClick method in the MyClass file. I tried the following two ways:
PageMethods.MyClick(par1, par2)
and
PageMethods.MyClass.MyClick(par1, par2)
And both aren't triggering the method. I appreciate any comments or suggestions.
The way a link button works is, its href points to the __doPostBack JavaScript method. This triggers a message to be sent to the server. Since you've replaced the href with javascript:, essentially "do nothing," that explains the behavior you're seeing.
If you want to send data to the server without reloading the page, you need to use AJAX. There are two main methods, one is the UpdatePanel, most people, including myself, would recommend against this model. Why? Well think of it this way, based on what you're telling me, you have a "like" button. I'd guess there are two pieces of data your server-side method needs, some ID to identify what they are liking and a Boolean to say like or not like. If you use an UpdatePanel, the entire ViewState has to roundtrip to the server. Additionally, the server will respond with the entire HTML contents of everything in the UpdatePanel. There are many good articles on the internet that go into more detail, here is one if you're interested, but I've summarized the points. If you're trying to build a lightweight, fast modern website, it's not the way to go. If you're trying to get something done quickly and don't want to write JavaScript, it might be, however JavaScript is pretty much a requirement for the modern web.
Alternatively, you can use straight AJAX. This requires you to put a method in your code behind decorated with the [WebMethod] attribute. This means it can now be called from JavaScript via AJAX. Using my example you'd have:
[WebMethod]
public static bool DoLike(string id, bool likeOrNotLike)
{
// Your implementation
}
Now, how do you call it. I'd recommend you use a framework such as jQuery to call the AJAX as it simplifies a lot of the syntax for you. With jQuery, you can simply do:
$.ajax({
type: "GET",
url: "MyPage.aspx/DoLike",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: { id: "123", likeOrNotLike: true },
success: function (msg) {
alert("It was called successfully");
},
error: function () {
alert("An error occurred calling it");
}
});
The code above sends two parameters to the DoLike method, "123" (the ID), and true (likeOrNotLike). You're method will be called with these parameters as if you had called it from C#. The framework takes care of all of that.
To add jQuery to your page, simply add a:
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
If you're against jQuery, I can modify my answer to use XMLHTTPRequest directly but it's more cumbersome.
As you can see, AJAX isn't magic though, you can't simply add [WebMethod] to your event handler and all will work. In fact, if you go the straight AJAX route, you're not even using an event handler, you're calling a server side method directly.
You can certainly do the UpdatePanel as some others have suggested here, it will work. It's just, in my, and many other opinions, overkill for AJAX. It was Microsoft's attempt at implementing AJAX without the developer having to do much. Just like with ViewState, it comes with a price. It's up to you if the price is tolerable.
my 2 cents: I think it's because you are trying to run server code without sending the necessary info to the server. Without postback you cant expect the server to process the event.
Not sure if there's a way to do that in ASP. Maybe you could use an AJAX call but I never used it for this specific purpose.
If you want to call the server without a postback, you have to use an UpdatePanel or a native Ajax function. In your case, I believe UpdatePanel is nicest one.
Put your fields and the button inside the UpdatePanel, then everything will work fine, just like magic.
To see how to use UpdatePanel, take a look at
https://msdn.microsoft.com/en-us/library/bb386454.aspx
https://web.archive.org/web/20211020102853/https://www.4guysfromrolla.com/articles/102407-1.aspx
http://ajax.net-tutorials.com/controls/updatepanel-control/

How to bind server side events on client objects and vice versa with meteor

Is it possible to directly bind server side events to client side objects in meteor?
I would like to update a view for example when a server side event triggers. On the other hand I'd like to fire a server side method when a user clicks a view item.
I could use Meteor#methods for all the events but that seems odd.
Or can I specify an eventhandler for example using EventEmitter outside the client- and server-scope so that it is available on both sides and trigger/bind events ob that very object?
Some confused about that I am thankful for hints into the right direction.
Regards
Felix
Update:
Using Meteor#methods works great in case user events should be mapped to server side actions. The other way around is still unclear. Asynchronous actions on serverside could persist their results in a collection which is pub/sub'ed to the client, which in turn could update some view due to the reactive context. But thats odd, cause persisting that kind of info is slow, wasted space and time. Any suggestions?
I believe you can use the Collection.observe on the server side to 'observe' events on the Collection as clients are inserting, updating, removing, etc... That might be a start if you are focused on Collections alone. I used it like a sort of server side event loop to watch for collection changes.
When a user clicks on something in a view try binding a Template Event to the View css selector and then calling a Meteor method which will notify the server of the event. See the examples of binding a key handler and/or button handlers to a Template.entry.event which then call a Meteor method notifying the server that something happened.
What about storing the progress in Session? You could do something like this:
Template.progress.value = function() {
return Session.get('progress');
}
Then, whenever you update the Session on the server, the client template will automatically get those changes.
Out of curiosity, how exactly are you performing asynchronous actions on the server? I'm still trying to figure that out.

What is the a4j event sequence?

What is the event sequence of a a4j:commandLink or a4j:commandButton?
Is it right: onclick->actionListner->oncomplete->reRender?
Is reRender comes before oncomplete?
When action happens?
The onclick JavaScript is called the first when the enduser clicks the generated HTML element. When the JavaScript code does not return false, then all associated ActionListener implementations in the JSF side will be invoked, in the order of their association with the component. If the listeners haven't thrown any exception, then the real action will be invoked. When the action returns a successful response, then the components specified in reRender will be updated in the client side. Finally the oncomplete JavaScript will be called.
It's pretty easy to track yourself if you have a debugger in both the client and server side and know how to use it. I strongly recommend Firebug for the client side and Eclipse for the server side. You can of course also always do poor man's debugging using alert()s or System.out.println()s.

What does "javascript:_doPostBack('Gridview','Edit$0')" mean, full post back still occurs?

JavaScript is used where one want to do something on the client side purely, or wants to send something to the server in a manner that postback does not handle.
But in Visual Studio 2008 controls ASP.NET C# I have seen that when the page is displayed in the browser the controls, namely GridView, FormView, and LINKBUTTON (!) all show this javascript:thing when the cursor is hovered on them. Why?
Post back still occurs. Even the linkbutton has this JavaScript thing and whenever you click on it, full post back occurs.
Changing label.text, etc. too is on the pageload event!
so why the JavaScript? Why not a simple button? Why linkbutton?
In this case JavaScript calls could be used to send additional data to the server, e.g. save some client data for the grid (like the width of resized columns or something like that).
Server-side frameworks use this approach to allow server-side guys to generate all the client-side code. It's a kind of quick'n'dirty solutions (comparing with well-organized unobtrusive JavaScript).
ASP.NET is stateless. That is, every time a page is requested, the server actually constructs the entire page and its controls & state and then responds to the request. It then renders the appropriate HTML markup as response to the request.
For any control, if there is the autopostback property set to true then a page is postback to the server if the control causes a postback (like clicking on a link button).
How does ASP.NET post back the page ?
It does it using a javascript function called _doPostBack(). The function is -
function __doPostBack(eventTarget, eventArgument) {
if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
theForm.__EVENTTARGET.value = eventTarget;
theForm.__EVENTARGUMENT.value = eventArgument;
theForm.submit();
}
This function is used to submit the form back to the server. _doPostBack accepts arguments - event target and event arguments by using hidden variables __EVENTTARGET and __EVENTARGUMENT. This tells the server which control caused the postback and also passes appropriate arguments to the server.
if you have this code in your aspx page -
<asp:LinkButton ID="lnkButton" runat="server">LinkButton</asp:LinkButton>
The corresponding generated markup will be -
<a id="LinkButton1" href="javascript:__doPostBack('lnkButton','')">LinkButton</a>
So, say you click on a link button, the page is postback by the __doPostBack() function. Then, the page is recreated at server with the respective control state on the page. To get the state of each control on the page mechanisms like viewstate are used. Once the page is loaded, the server computes and renders the response markup.

Categories

Resources