I'm trying to display a panel to the user when an asynchronous call is made, but only if that happend from a specific call.
using the normal "get control" script I have mine like:
function pageLoad() {
try {
var manager = Sys.WebForms.PageRequestManager.getInstance();
manager.add_endRequest(OnEndRequest);
manager.add_beginRequest(OnBeginRequest);
}
catch (err) { }
}
function OnBeginRequest(sender, args) {
//alert('Start\n\n' + sender + '\n\n' + args);
var p = document.getElementById('ajaxLoadingPanel');
p.style.visibility = 'visible';
p.style.display = 'inline';
}
function OnEndRequest(sender, args) {
//alert('End\n\n' + sender + '\n\n' + args);
var p = document.getElementById('ajaxLoadingPanel');
p.style.visibility = 'hidden';
p.style.display = 'none';
}
but my question is How do I know the methods of sender and args?
I went through the MSDN and they talk nothing about the methods we can use, and there is no intellisence in VS2008 for this part...
any ideas? I want to get a list of methods and properties for both sender and args that I can use of this javascript API.
This documentation is helpful:
http://msdn.microsoft.com/en-us/library/bb398976.aspx
It has a table of all the events on PageRequestManager and what their event args are. Then the event args documents their properties, etc. The sender is always the PageRequestManager.
Debug in ScriptDebugger and find out the contents of sender and args
you can identify that which control has caused the postback
To know which element caused postback, you can use args.get_postBackElement().id.
Related
I'm using DevExpress 13.1 to develop my web application. My page has two controls: A Gridview which contains some item and a FileManager control (is children of a callbackpanel) which contain files information of item which is focused on that Gridview. I'm using Gridview's FocusRowChange client event to get data and send it back to server through an callback (of callback panel) to set new RootFolder value, but it not works. Tell me where's my wrong?
Thanks in advance.
My code:
ASPX file:
function myGridView_FocusRowChanged(s, e) {
var index = s.GetFocusedRowIndex();
var soCT = s.GetRowValues(index,"SoChungThu;SoHopDong",LoadFileList);
}
function LoadFileList(values) {
myCallbackPanel.PerformCallback("CHANGE_ROOT_FOLDER" + "," + values);
}
CS file:
protected void myCallbackPanel_Callback(object sender, DevExpress.Web.ASPxClasses.CallbackEventArgsBase e)
{
var vals = e.Parameter.Split(',');
if (vals[0].ToUpper() == "CHANGE_ROOT_FOLDER")
{
var path = vals[0] + "/" + vals[1];
myFileManager.Settings.RootFolder = path;
}
}
}
I found a solution.
Store folder path in Session, and assign it to RootFolder in Page_Load event.
Thank for reading.
I have a click event bound to the following ko function:
self.select = function (entity, event) {
var ctrlPressed = false;
if (event.ctrlKey) { ctrlPressed = true; }
if (!ctrlPressed) {
manager.deselectAll();
this.selected(true);
} else {
this.selected() ? this.selected(false) : this.selected(true);
}
}
It is bound like so:
data-bind="click: select, event: { dblclick: function(){alert('test');}}"
This currently works except that it fires "select" twice when you double click, which I do not want. I tried following the advice in this SO question, but when I create the singleClick() function, I get an error that "ctrlKey is not a function of undefined". So it's not passing the event properly. Further more, the doubleClick() function in the other answer there doesn't work at all. It gives an error on the "handler.call" part saying handler is not defined.
So, how can I successfully call my ko select function on singleClick but NOT on doubleclick?
I don't think this is really a knockout issue. You have at least these two options:
1. Implement some custom logic that prevents processing if a single click has started processing already
2. Prevent the double-click function altogether. JQuery has this handy handler:
$(selector).on("dblclick", function(e){
e.preventDefault(); //cancel system double-click event
});
So I technically got it to work. Here is my new singleClick function
ko.bindingHandlers.singleClick = {
init: function (element, valueAccessor, c, viewModel) {
var handler = valueAccessor(),
delay = 400,
clickTimeout = false;
$(element).click(function (event) {
if (clickTimeout !== false) {
clearTimeout(clickTimeout);
clickTimeout = false;
} else {
clickTimeout = setTimeout(function () {
clickTimeout = false;
handler(viewModel, event);
}, delay);
}
});
}
};
This passes the viewModel and event to the handler so I can still modify observables and capture ctrlKey pressed.
The binding:
data-bind="singleClick: select, event: { dblclick: function(){alert('test');}}"
The problem is that now, obviously, single clicking an item has a delay while it waits to see if it's a double click. This is an inherent and unsolvable issue, I believe, so though this technically answers my question, I will consider a completely different route (ie, no double-clicking at all in my interface)
I have this button:
<s:Button includeIn="MeniuPrincipal" label="Descarcare Date" click="downloadLmData(event)"/>
and this click event handler:
protected function downloadLmData(event:MouseEvent):void
{
downloadData('competenta', 'competente');
downloadData('localitate', 'localitati');
}
the downloadData function looks like this:
private function downloadData(item:String, items:String):void
{
try {
var colVar:String = 'col' + cappitalize(items);
this.status = "Descarcare date in curs...";
this[colVar] = null;
var service:HTTPService = new HTTPService();
service.url = serverUrl + items + '/xml';
service.resultFormat = "xml";
service.method = "GET";
service.addEventListener(ResultEvent.RESULT, addArguments(downloadDataComplete, [item, items]));
service.send();
} catch (error:Error) {
errorHandler.defaultErrorHandler(error);
}
}
The problem is, all calls are ignored, except for the first one. Is there any "queuing" mechanism which would allow all calls to be made?
Thank you.
You need to chain your asynchronous calls. See these 2 blog posts for implementations :
http://kuwamoto.org/2006/05/16/dealing-with-asynchronous-events-part-1/
http://kuwamoto.org/2006/05/16/dealing-with-asynchronous-events-part-2/
I will rather use observer pattern. The easiest way.
I need to run a javascript function when the update panel is loaded completely(I want to scroll), and not on initial page load.
Please suggest.
Thanks
This is the way to get the end Event after the update.
<script type="text/javascript">
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_endRequest(EndRequest);
function EndRequest(sender, args) {
}
</script>
Untested
<script type="text/javascript">
var app = Sys.Application;
app.add_init(ApplicationInit);
function ApplicationInit(sender) {
var prm = Sys.WebForms.PageRequestManager.getInstance();
if (!prm.get_isInAsyncPostBack())
{
prm.add_pageLoaded(PageLoaded);
}
}
function PageLoaded(sender, args) {
//Do something
}
</script>
If you are using AJAX then the only way i have found yet to give an alert to a user on return to the Asynchronous post back is to add an “end request” handler to the PageRequestManager.
In this way you can tell the request manager to run a javascript function on returning from a Asynchronous post back event of AJAX.
Code for doing this is :
function load()
{
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
}
where “EndRequestHandler” will be the name of your javascript function you want to call.
Call the above function in Onload event of tag:
<body onload=”load()”>
function EndRequestHandler()
{
alert(“You record has been saved successfully”);
}
Now If you want to give a different message based on your logic in server side code (code behind) then you can use a server side Hidden Field:
<input id=”hdnValue” type=”hidden” runat=”server” value=”" />
Set its value in server side code on Asychronous Post Back:
Protected Sub btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCreateSample.Click
If condition Then
hdnValue.value = “do this”
Else
hdnValue.value = “do that”
End If
End Sub
Now you can check the value of this Hidden Field in your Client Side EndRequestHandler function and give a different alert to user based on its value:
function EndRequestHandler()
{
if (document.getElementById(‘<%= hdnValue.ClientID %>’).value == “do this”)
{
alert(“You record has been saved successfully”);
}
else
{
alert(“There is an error”);
}
}
you can use below code with if jquery is used
This is to show saved message and hide that message after 5 seconds after update panel is updated
function pageLoad() {
window.Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
}
function EndRequestHandler()
{
window.setTimeout(function () {
var label = window.$get('<%= lblMsg.ClientID%>');
if (label != null) { label.style.display = 'none'; }
}, 5000);
}
So I need to scroll to the top of the page after an async post back in an asp.net update panel.
The code I used was this:
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestEventHandler);
function EndRequestEventHandler(sender, args)
{
scrollTo(0,0);
}
However, I only want this to be run when I click on a certain button which causes the async postback.
How do I wire this event up in my code behind button event?
Any help would be appreacited, thanks!
My quest for a solution is finally over. This question was part of the help, and the rest I found here.
Had to override ASP.NET Ajax's behaviour of memorizing the scroll position:
<script type="text/javascript">
var manager = Sys.WebForms.PageRequestManager.getInstance();
manager.add_beginRequest(beginRequest);
function beginRequest()
{
manager._scrollPosition = null;
}
</script>
And then use the bit of code in the answer here, on the codebehind of the page I wanted to scroll to the top:
ScriptManager.RegisterStartupScript(this, typeof(MyControl), "someText", "window.scrollTo(0, 0)", true);
I used Farinha's answer (thanks!) and changed it slightly so I could just call the method any place I wanted to scroll to the top, but maintain the scroll position otherwise.
public static void ScrollToTop(int intPosY = 0)
{
string strScript = #"var manager = Sys.WebForms.PageRequestManager.getInstance();
manager.add_beginRequest(beginRequest);
function beginRequest()
{
manager._scrollPosition = null;
}
window.scroll(0," + intPosY.ToString() + ");";
Page pagCurrent = GetCurrentPage();
ScriptManager.RegisterStartupScript(pagCurrent, pagCurrent.GetType(), string.Empty, strScript, true);
return;
}
public static Page GetCurrentPage()
{
return (HttpContext.Current.Handler as Page);
}
Try this:
protected void myButon_Click(object sender, EventArgs e)
{
ScriptManager.RegisterStartupScript(this, typeof(MyControl), "someText", "alert('!');", true);
}
Improving on the answers of #Farinha and #Bradford Scott, the code can be simplified to this:
var script =
"Sys.WebForms.PageRequestManager.getInstance()._scrollPosition = null; " +
"window.scrollTo(0, 0);"
ScriptManager.RegisterStartupScript(this, GetType(), "key", script, true);
I'm actually not even sure why their scripts work since they add the reset of the Sys.WebForms.PageRequestManager's _scrollPosition as an add_beginRequest handler, and here we are actually returning from the request.
Anyway, resetting the _scrollPosition right before making your own scrollTo() call definitely works.