Showing posts with label pages. Show all posts
Showing posts with label pages. Show all posts

Wednesday, March 28, 2012

PageMethod problems

Hi Everyone -

I have a master page setup, and i would like to use ajax in one of the pages that are to be used as a content page.

In the javascript portion of the detail page, i have some procedures that are marked as webmethods

 [WebMethod()]public void LoadGrid3() {#region LoadGrid3 DataSet ds3 =new DataSet(); DataTable dt3 = ds3.Tables.Add("Customer");

In the javascript side (presentation)

"javascript" type="text/javascript"> function findData() { var request; var AlertDiv = document.getElementById("AlertDiv"); AlertDiv.style.display =""; request = PageMethods.Update(onSearchComplete);} function onSearchComplete(results) { var AlertDiv = document.getElementById("AlertDiv"); AlertDiv.style.display ="none"; alert(results);} function findData_callback(res){ alert("here");}

script language="javascript" type="text/javascript"> function findData() { var request; var AlertDiv = document.getElementById("AlertDiv"); AlertDiv.style.display =""; request = PageMethods.Update(onSearchComplete);} function onSearchComplete(results) { var AlertDiv = document.getElementById("AlertDiv"); AlertDiv.style.display ="none"; alert(results);} function findData_callback(res){ alert("here");} script>

"text/C#" runat="server"> [WebMethod]public void Update() { LoadGrid3(); }

scripttype="text/C#"runat="server">

[WebMethod]

publicvoid Update()

{

LoadGrid3();

}

script>

On the button click event - i have it call the function findData

the PageMethods get popped as an error

Microsoft JScript runtime error: 'PageMethods' is undefined

Am i missing something somewhere??

thanks

tony

Hi,

PageMethods must be declared as public static methods in beta1.

PageMethods and xml-script?

So, I've only seen a couple posts that touch on this, and none had a direct answer. What I'm looking to do is to call one of my pages' WebMethods using the declarative syntax. I'm not sure exactly how to do that.

Using javascript it works fine, so I know that I have the rest of the thing set up right, but for whatever reason using all the components and bindings and all that just.

Conversely, I tried doing it with just a regular web service and binding,a nd that works fine too. Anyone successfully use declarative markup with WebMethods that are served from the aspx page?

hello.

currently, you can only do that if you call web service methods, you can, however, develop your own class which could be used from xml-script (you can adapt the ServiceMethodRequest to do that).


I'm not sure, I guess, what differentiates a webmethod called from the codefile from a webmethod called from a web service, in the eyes of xml-script. What is the body of code that influences the xml-script schema? I assume it's one of the classes in atlas.js, but I'm not sure which one. I'm interested in hearing how you'd do it if it were your task, though.

thanks,

Paul


hello.

take a look at the servicemethodrequest class. you just have to build a similar class but instead of using the servicemethod class internally, you should use teh pagemethod class.


Oh, I see. yeah, I spent the last half hour or so just reading (again) the atlas.js source. I'm not as sharp in javascript as I am in server-side stuff, but I'm starting to get the flow. thanks for the tip, if I end up implementing this I'll post the results.

Wow, ok, that was a LOT easier than I thought it would be. Thanks for the tip. Code follows. I went ahead and put it in my namespace b/c I didn't want to pollute teh Sys.Net namespace, but feel free to take, modify, whatever at liberty. As you suggested, the main thing to do here was to take the existing ServiceMethodRequest class and swap out the ServiceMethod references for PageMethod ones. I also pulled otu some of the member variables that I knew I wouldn't need (e.g. url-related ones) to tidy it up. This is a first draft, so I might find more that I want to do now that I have my teeth in it.

Thanks again!!

Paul

/*

Title: PageMethodExtender.js

Author: Paul Vencill

Summary: Add-on class to Atlas that will allow web methods exposed in the page's codefile to be called from the page's xml-script.

*/

Type.registerNamespace(

'Com');

Type.registerNamespace(

'Com.VelocityDataSolutions');

Com.VelocityDataSolutions.PageMethodRequest =

function() {

Com.VelocityDataSolutions.PageMethodRequest.initializeBase(

this);var _methodName =null;var _parameters =null;var _response =null;var _userContext =null;var _result =null;var _request =null;var _timeoutInterval = 0;var _priority = Sys.Net.WebRequestPriority.Normal;this.get_url =function() {return _url;

}

this.set_url =function(value) {

_url = value;

}

this.get_appUrl =function() {return _appUrl;

}

this.set_appUrl =function(value) {

_appUrl = value;

}

this.get_methodName =function() {return _methodName;

}

this.set_methodName =function(value) {

_methodName = value;

}

this.get_parameters =function() {if (_parameters ==null) {

_parameters = { };

}

return _parameters;

}

this.get_response =function() {return _response;

}

this.get_result =function() {return _result;

}

this.get_timeoutInterval =function() {return _timeoutInterval;

}

this.set_timeoutInterval =function(value) {

_timeoutInterval = value;

}

this.get_priority =function() {return _priority;

}

this.set_priority =function(value) {

_priority = value;

}

this.completed =this.createEvent();this.timeout =this.createEvent();this.error =this.createEvent();this.aborted =this.createEvent();this.invoke =function(userContext) {if (_request !=null) {returnfalse;

}

var _pageMethod =new Sys.Net.PageMethod(_methodName);

_request = _pageMethod.invoke(_parameters, onMethodComplete, onMethodTimeout,

onMethodError, onMethodAborted,

this ,

_timeoutInterval, _priority);

function onMethodComplete(result, response, target ) {

_request =

null;

_userContext = userContext;

_response = response;

_result = result;

target.completed.invoke(target, Sys.EventArgs.Empty);

}

function onMethodError(result, response, target ) {

_request =

null;

_userContext = userContext;

_response = response;

_result = result;

target.error.invoke(target, Sys.EventArgs.Empty);

}

function onMethodTimeout(request, target ) {

_request =

null;

_userContext = userContext;

target.timeout.invoke(request, Sys.EventArgs.Empty);

}

function onMethodAborted(request, target ) {

_request =

null;

_userContext = userContext;

target.aborted.invoke(request, Sys.EventArgs.Empty);

}

returntrue;

}

this.abort =function() {if (_request) {

_request.abort();

}

}

this.getDescriptor =function() {var td = Com.VelocityDataSolutions.PageMethodRequest.callBaseMethod(this,'getDescriptor');

td.addProperty(

'methodName', String);

td.addProperty(

'parameters', Object,true);

td.addProperty(

'response', Sys.Net.WebRequestExecutor,true);

td.addProperty(

'result', Object,true);

td.addProperty(

'timeoutInterval', Number);

td.addProperty(

'priority', Number);

td.addMethod(

'invoke');

td.addMethod(

'abort');

td.addEvent(

'completed',true);

td.addEvent(

'timeout',true);

td.addEvent(

'error',true);

td.addEvent(

'aborted',true);return td;

}

Com.VelocityDataSolutions.PageMethodRequest.registerBaseMethod(

this,'getDescriptor');

}

Com.VelocityDataSolutions.PageMethodRequest.registerClass(

' Com.VelocityDataSolutions.PageMethodRequest', Sys.Component);

Sys.TypeDescriptor.addType(

'script','pageMethod', Com.VelocityDataSolutions.PageMethodRequest);

cool!

that's the spirit :)


Hi,

thanks for posting the code, Paul.

Glad to, I'm very excited about this. I also went ahead and built a Page subclass that inserts a ScriptManager control if there isn't one and pulls in the xml-script from an external file (by default located in the same folder and with the same name as the parent document but with a .ascr extension.). Here's teh code for that subclass as well. It works, but I'm still polishing it to make it a little more robust, add comments, etc etc. I just wanted to see if i could hack my way through it.

using

System;

using

System.Web.UI;

//using System.Xml;

using

System.Xml.XPath;

///

<summary>

///

Summary description for AtlasPage

///

</summary>

public

classAtlasPage : System.Web.UI.Page

{

privatestring _xmlScript;privatebool _hasScriptManager;protectedoverridevoid OnInit(EventArgs e)

{

addScriptManager();

addAtlasScript();

base.OnInit(e);

}

protectedstring _scriptFile;publicstring ScriptFile

{

get {return _scriptFile; }set { _scriptFile =value; }

}

privatevoid getXmlScript()

{

if(String.IsNullOrEmpty(_scriptFile))

{

string url = Request.Url.ToString();

url = url.Replace(

".aspx",".ascr");

_scriptFile = url;

}

try

{

XPathDocument xdoc =newXPathDocument(_scriptFile);XPathNavigator xNav = xdoc.CreateNavigator();

xNav.Select(

"script");

_xmlScript =

String.Format("\n{0}\n",xNav.OuterXml.ToString());

}

catch

{

_xmlScript =

String.Format(@."<!-- File not found: {0} -->","xml-script");

}

}

privatevoid addAtlasScript()

{

getXmlScript();

LiteralControl lit =newLiteralControl(_xmlScript);this.Controls.AddAt(this.Controls.Count - 1, lit);

}

privatevoid addScriptManager()

{

_hasScriptManager =

false;

iterateCollection(Controls);

if (!_hasScriptManager)

{

Microsoft.Web.UI.

ScriptManager scm =new Microsoft.Web.UI.ScriptManager();this.Header.Controls.Add(scm);

}

}

privatevoid iterateCollection(ControlCollection ctrls)

{

foreach (Control ctrlin ctrls)

{

if (ctrl.HasControls())

{

iterateCollection(ctrl.Controls);

}

if (ctrl.ToString().Contains("ScriptManager"))

{ _hasScriptManager =

true; }

}

}

}

Paul Vencill

Velocity Data Solutions. LLC

www.velocitydatasolutions.com

Monday, March 26, 2012

PageRequestManager blocks any other instance of XMLHttpRequest?

I'm trying to implement a progress bar that reports on the contents of session variables during the course of a slow method on one of my pages. The slow method in question populates/updates a datagrid inside an update panel.

I'm aware that it's only possible to have one request at a time using PageRequestManager, so i thought i would use some of my old own ajax libraries, and make a manual call to a webservice which would return the contents of the relevant session variables. However, even using this approach, the call to the webservice does not go through until the slow method has finished, which is obviously not ideal. Does PageRequestManager block any other instances of XmlHttpRequest on the page?

I read an article that suggested what i was attempting to do was possible using PageMethods, but since the latest release, where pagemethods have to not only reside in the aspx file but also have to be static, this is just not feasible.

I'd be grateful if anyone could shed some light on this, cheers

I'm not sure if something is awry with my installation, but now i am unable to request any other page from within the current application whilst the ajax request is in process - it just hangs, and then processes when the current ajax operation is complete. For slow methods that retrieve a lot of data, i'm sure you can see why this is almost ludicrous behaviour. Surely this isn't intended?

hello.

in previous releases, you could only make a partial refresh at a time (ie, you could only start a partial refresh after a previous one ended). in the last version, i think this has been changed and now what happens is that the last request overrides the previous one. this only happens when you're using UpdatePanels (ie, the behavior i've described only happens when you use updatepanels). you can still use xmlhttprequest as you see fit in your pages and you should be able to make several calls.


Thanks for the reply.

Are you aware of any other scenario in which setting a long running method running on one page would block any other requests to other pages within the same application until it is complete? I'm not accessing or modifying session data in either page.

Cheers


hello.

well, even though you're not using session data, it all depends on the way your final page is rendered. for instance, i think that if you don't set the enableSessionstate attribute on the page directive, the page will still use read/write session state. do you think that this might be the problem you're having?


I was unaware i had to specifically set the enableSessionState directive to "false" - even "readonly" still made the page block.

Thanks a lot for the help, appreciated.

PageRequestManagerParserErrorException with the New ASP.Net Ajax framework

I have two simple web pages with two update panels in them. The first one calls the second one using Server.Tranfer or Server.Execute and it gives the following error message:

Sys.WebForms.PageRequestmanagerParsererrorException: The Message received from the Server could not be Parsed. Common caused for this error are when the response is modified by calls to response.Write(), response filters, HttpModules, or server trace is enabled.

Now, this never happened with the previous framework, atlas. I've tried all possible solutions I could think of, but none worked so far.

Try to take a look at the following forums for?reference.
http://forums.asp.net/thread/1467462.aspx
Wish this can help you.

Saturday, March 24, 2012

Pages lifecycle with UpdatePanel.

It seems to me that I read an article says when I use an UpdatePanel not all events are rising.

Help me find the list of the events that are rising with UpdatePanel.

Thank you in advance,

Igor

Hi,

when an UpdatePanel performs a partial postback, the whole page lifecycle is executed, thus all the server-side events are being raised.


Garbin:

when an UpdatePanel performs a partial postback, the whole page lifecycle is executed, thus all the server-side events are being raised.

Thank you.


Garbin:

when an UpdatePanel performs a partial postback, the whole page lifecycle is executed, thus all the server-side events are being raised.

Thank you.


Luckily the ASP.NET team put a funky ittle feature into Atlas which will let you detect if you're in a Partial render or not.

Here's some code I put together to show this in action and how you can use it: -

1<%@. Page Language="C#" AutoEventWireup="true" CodeFile="PostBackType.aspx.cs" Inherits="PostBackType" %>23<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">4<html xmlns="http://www.w3.org/1999/xhtml">5<head runat="server">6 <title>Untitled Page</title>7</head>8<body>9 <form id="form1" runat="server">10 <atlas:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true" />11 <div>12 <asp:Button ID="MyNormalButton" runat="server" Text="Cause Normal Postback" />13 <atlas:UpdatePanel ID="Panel1" runat="server">14 <ContentTemplate>15 <asp:Button ID="MyButton" runat="server" Text="Cause Partial Render Postback" />16 <h1 id="MyMessage" runat="server"></h1>17 </ContentTemplate>18 </atlas:UpdatePanel>19 </div>20 </form>2122 <script type="text/xml-script">23 <page xmlns:script="http://schemas.microsoft.com/xml-script/2005">24 <references>25 </references>26 <components>27 </components>28 </page>29 </script>30</body>31</html>

Then the C#

1using System;2using System.Data;3using System.Configuration;4using System.Web;5using System.Web.Security;6using System.Web.UI;7using System.Web.UI.WebControls;8using System.Web.UI.WebControls.WebParts;9using System.Web.UI.HtmlControls;10using Performance;111213public partialclass PostBackType : System.Web.UI.Page14{15protected void Page_Load(object sender, EventArgs e)16 {17 SetMessage();18 }1920private void SetMessage()21 {22switch (GetPostBackType())23 {24case PostBack.NotPostBack:25 MyMessage.InnerText = ("Not a Postback");26break;27case PostBack.NormalPostBack:28 MyMessage.InnerText = ("Normal Postback");29break;30case PostBack.PartialRenderPostBack:31 MyMessage.InnerText = ("Partial Render");32break;33 }34 }3536private PostBack GetPostBackType()37 {38if (!Page.IsPostBack)39 {40return PostBack.NotPostBack;41 }42else if ((Page.IsPostBack) && (!ScriptManager1.IsInPartialRenderingMode))43 {44return PostBack.NormalPostBack;45 }46else if ((Page.IsPostBack) && (ScriptManager1.IsInPartialRenderingMode))47 {48return PostBack.PartialRenderPostBack;49 }50else51 {52throw new System.NotSupportedException("This kind of Postback is not currently catered for");53 }54 }55}

NICE!!Thanks for the excellent illustration.

and for those of you doing this from a usercontrol, the code would change a little to something like this:

Dim

tempControl2As System.Web.UI.ScriptManager

tempControl2 =

CType(Page.FindControl("ScriptManager1"), System.Web.UI.ScriptManager)IfNot tempControl2.IsInAsyncPostBackThen

Panels and external pages

I have an ajax panel with a grid on it. When clicking on the grid a new window is opened with information from the grid. When finished i want to update the grid when the popup window is closed, how do i do it?

chaim

when opening the new window in your javascript, add the new window to a variable.

var nWindow = window.open("page")

the write a javascript timer to check whether the nWindow is null or not. If it is null then raise a postback to update the grid.

-Alan

Wednesday, March 21, 2012

Partial page refresh on master pages

Hi

I need some help. I'm working on a web site and i'm having problems to avoid a full page refresh when i navigate across the site using a menu control.

I use a master page to define the main aspect of my site. On the master page i placed a menu control and i want to refresh only the content place holder with the content page each time the user click on a menu item. I' ve tried to do this with ajax but until now, no luck.

If someone have a clue on how to do this i would very much appreciate it..Big Smile

Master pages are not frames...each time you click a link, you are on a new page, and the masterpage just gives it the same look. The only way that I have heard of to do that is with iFrames or having one page and load all your pages as user controls, although both methods are rather hacky.


I've alse tried to use iframes inside the master page and set the target property on the menu control to the iframe, but instead of loading the content page inside the iframe it opens a new window.

I placed the iframe within the content place holder on the master page. Maybe its not the right place to put it.


I think that you may need to do it on an aspx page, and all the pages you go to from there would need to not have a master page. Actually if you are doing it that way, you may not even need a master page. You could probably have a page that looks like the master page, but has an iFrame where the content template would be. I am not completely sure if that would work correctly though because we don't worry about that much though because most our pages act as separate applications, so there is not a lot of switching pages. Postbacks can also serve as a way for the user to see that they are going somewhere else, which works the way our site is, but may or may not for you.

partial rendering Post back error

Hi my application works fine in IE with partial rendering when posting back.

But When I use another Browser, some of pages are okay with partial rending, some of them I got the error message as below:

http://localhost

Failed to load viewstate. The control tree into which viewstate is being loaded must match the control tree that was used to save viewstate during previous request. For example, when adding controls dynamically, the controls added during a post-back must match the type and position of the controls added during the initial request.

To me these pages seems no difference when applying partial rendering. All of them use same pattern when using UpdatePanel.

Help please.

Hi,KentZhou

I am afraid we cannot find out the exact root cause without further information captured when the problem occurs.

Why don't you provide us with some source codes of the pages that get error?

To troubleshoot this issue, we really need the source code to reproduce the problem, so that we can investigate the issue in house. It is not necessary that you send out the complete source of your project. We just need a simplest sample to reproduce the problem. You can remove any confidential information or business logic from it.

According to the error message,I guess the problem lies on adding controls dynamically, maybe you can checkDynamicaly loading User Controls into UpdatePanel for solution and more infomation.

Let me know if you need more info.

Hope this helps.

Thank you.


Hi, Thanks for reply.

This message comes out when using Safari to run AJAX application. This application works fine when using IE or Firefox.
The page loads many ascx dynamically and using UpdatePanel with RenderMode="Inline" UpdateMode="Conditional"