Friday, May 19, 2006

怎样解决AJAX交互应用中遇到的浏览器的BACK后退按钮问题

AJAX越来越多的应用到网页交互上,但是用AJAX做交互也有一些问题,比如传统网页的浏览是一个一个页面切换,所以可以用浏览器的后退按钮和前进按钮切换到指定的浏览过的页面,也就是浏览器的history.
但是用AJAX做交互的时候,是通过js+xmlhttp来获取其他页面信息,对于浏览用户来说,是一个隐性的UE,感觉不到浏览器地址栏的URL的变化,对浏览器来说,地址栏的URL没发变化,产生不了HISTORY。
不是说在用AJAX做交互的时候,BACK按钮的问题就解决不了,对于AJAX应用很成熟的网站比如google等,这些都已经解决。有热心的老外已经提供了解决方法,粗粗看了看,原理是用了iframe,有兴趣的自己去研究吧
老外管这个叫:Really Simple History framework,还有专门的AJAX history libraries

原文介绍:点击这里浏览
先看一个演示吧:
http://www.donotremove.co.uk/extra/ajax-nav/index.html

这个演示的下载地址,是PHP的
http://www.contentwithstyle.co.uk/resources/ajax-nav/ajax-nav.zip

另一篇文章介绍:点击这里浏览

这篇文章例子的下载:点击这里下载

Browser Bookmarking Back button
IE6/PC Yes Yes
IE5.5/PC Yes Yes
IE5/PC Yes Yes
IE5/Mac No No
Firefox/PC Yes Yes
Firefox/Mac Yes Yes
Safari1.2/Mac Yes No

Sunday, May 07, 2006

使用第三方SDK开发咱博客园自己的MSN机器人(附示例源码)

更强功能参考我的上篇文章:
用MSN写您的博客
http://overred.cnblogs.com/archive/2006/04/28/387301.html


首先看效果图:(本文程序的功能是你把overred2004@163.com加入您的MSN,他会自动给你显示我的cnblog上的文章列表)



开始:

1.说明:本文使用的第三方SDK是incesoft的BOTPLATFORM机器人平台SDK,他可以支使net或者c++开发人员很轻松的开发出自己的MSN机器人.

2.步骤:

首先在http://sp.incesoft.com/上注册一个sp帐户,进去后您就可以操作(非常简单)如:下载需要的SDK。然后去申请一个机器人用的Passport帐号,在管理页面里添加好,最后点击Bind(绑定)启用机器人,启用后机器人帐号将在十秒内自动登录。

其次程序参照SDK里的Demo写就可以了,很简单。但由于SDK没有集成用户状态管理的功能,如果要实现比较复杂的功能(如http://www.bloghome.cn/help/msn.html 里所描述的功能),那就得自己下些功夫喽。

最后当把以上的都设置完毕后,您就可以把我提供的哪个DEMO 设置成您自己信息:
//username and password you applied from http://sp.incesoft.com
me.user = "SP000145";//您的spid
me.password
= "******";//您的sp密码
编译ok后,执行debug下哪个exe文件,连接和登陆成功后您就可以体验一下您的机器人拉!

更多使用方法请看他的提供的文档(在下载后的SDK里)
参考网上的资料和他提供的DEMO我提供一个示例源码:
下载:
http://www.cnblogs.com/Files/overred/CnBlogRobot.rar

Wednesday, May 03, 2006

The ASP.NET Page Life Cycle


Introduction

When a page request is sent to the Web server, whether through a submission or location change, the page is run through a series of events during its creation and disposal. When we try to build ASP.NET pages and this execution cycle is not taken into account, we can cause a lot of headaches for ourselves. However, when used and manipulated correctly, a page's execution cycle can be an effective and powerful tool. Many developers are realizing that understanding what happens and when it happens is crucial to effectively writing ASP.NET pages or user controls. So let's examine in detail the ten events of an ASP.NET page, from creation to disposal. We will also see how to tap into these events to implant our own custom code.

I'll set the stage with a simple submission form written in ASP.NET with C#. The page is loaded for the first time and has several server-side Web controls on it. When the Web server receives a request for the page, it will process our Web controls and we will eventually get rendered HTML. The first step in processing our page is object initialization.

1. Object Initialization

A page's controls (and the page itself) are first initialized in their raw form. By declaring your objects within the constructor of your C# code-behind file (see Figure 1), the page knows what types of objects and how many to create. Once you have declared your objects within your constructor, you may then access them from any sub class, method, event, or property. However, if any of your objects are controls specified within your ASPX file, at this point the controls have no attributes or properties. It is dangerous to access them through code, as there is no guarantee of what order the control instances will be created (if they are created at all). The initialization event can be overridden using the OnInit method.

2. Load Viewstate Data

After the Init event, controls can be referenced using their IDs only (no DOM is established yet for relative references). At LoadViewState event, the initialized controls receive their first properties: viewstate information that was persisted back to the server on the last submission. The page viewstate is managed by ASP.NET and is used to persist information over a page roundtrip to the server. Viewstate information is saved as a string of name/value pairs and contains information such as control text or value. The viewstate is held in the value property of a hidden control that is passed from page request to page request. As you can see, this is a giant leap forward from the old ASP 3.0 techniques of maintaining state. This event can be overridden using the LoadViewState method and is commonly used to customize the data received by the control at the time it is populated

3. LoadPostData Processes Postback Data

During this phase of the page creation, form data that was posted to the server (termed postback data in ASP.NET) is processed against each control that requires it. When a page submits a form, the framework will implement the IPostBackDataHandler interface on each control that submitted data. The page then fires the LoadPostData event and parses through the page to find each control that implements this interface and updates the control state with the correct postback data. ASP.NET updates the correct control by matching the control's unique ID with the name/value pair in the NameValueCollection. This is one reason that ASP.NET requires unique IDs for each control on any given page. Extra steps are taken by the framework to ensure each ID is unique in situations, such as several custom user controls existing on a single page. After the LoadPostData event triggers, the RaisePostDataChanged event is free to execute (see below).

4. Object Load

Objects take true form during the Load event. All object are first arranged in the page DOM (called the Control Tree in ASP.NET) and can be referenced easily through code or relative position (crawling the DOM). Objects are then free to retrieve the client-side properties set in the HTML, such as width, value, or visibility. During Load, coded logic, such as arithmetic, setting control properties programmatically, and using the StringBuilder to assemble a string for output, is also executed. This stage is where the majority of work happens. The Load event can be overridden by calling OnLoad

5. Raise PostBack Change Events

As stated earlier, this occurs after all controls that implement the IPostBackDataHandler interface have been updated with the correct postback data. During this operation, each control is flagged with a Boolean on whether its data was actually changed or remains the same since the previous submit. ASP.NET then sweeps through the page looking for flags indicating that any object's data has been updated and fires RaisePostDataChanged. The RaisePostDataChanged event does not fire until all controls are updated and after the Load event has occurred. This ensures data in another control is not manually altered during the RaisePostDataChanged event before it is updated with postback data.

6. Process Client-Side PostBack Event

After the server-side events fire on data that was changed due to postback updates, the object which caused the postback is handled at the RaisePostBackEvent event. The offending object is usually a control that posted the page back to the server due to a state change (with autopostback enabled) or a form submit button that was clicked. There is often code that will execute in this event, as this is an ideal location to handle event-driven logic. The RaisePostBackEvent event fires last in the series of postback events due to the accuracy of the data that is rendered to the browser.

Controls that are changed during postback should not be updated after the executing function is called due to the consistency factor. That is, data that is changed by an anticipated event should always be reflected in the resulting page. The RaisePostBackEvent can be trapped by catching RaisePostBackEvent.

7. Prerender the Objects

The point at which the objects are prerendered is the last time changes to the objects can be saved or persisted to viewstate. This makes the PreRender step a good place to make final modifications, such as changing properties of controls or changing Control Tree structure, without having to worry about ASP.NET making changes to objects based off of database calls or viewstate updates. After the PreRender phase those changes to objects are locked in and can no longer be saved to the page viewstate. The PreRender step can be overridden using OnPreRender

8. ViewState Saved

The viewstate is saved after all changes to the page objects have occurred. Object state data is persisted in the hidden object and this is also where object state data is prepared to be rendered to HTML. At the SaveViewState event, values can be saved to the ViewState object, but changes to page controls are not. You can override this step by using SaveViewState

9. Render To HTML

The Render event commences the building of the page by assembling the HTML for output to the browser. During the Render event, the page calls on the objects to render themselves into HTML. The page then collects the HTML for delivery. When the Render event is overridden, the developer can write custom HTML to the browser that nullifies all the HTML the page has created thus far. The Render method takes an HtmlTextWriter object as a parameter and uses that to output HTML to be streamed to the browser. Changes can still be made at this point, but they are reflected to the client only. The Render event can be overridden, as shown in Figure 6 (below).

10. Disposal

After the page's HTML is rendered, the objects are disposed of. During the Dispose event, you should destroy any objects or references you have created in building the page. At this point, all processing has occurred and it is safe to dispose of any remaining objects, including the Page object. You can override Dispose, as shown in Figure 6.

Conclusion

Each time we request an ASP.NET page, we run through the same process from initialization to disposal. By understanding the inner workings of the ASP.NET page process, writing and debugging our code will be much easier and effective (not to mention less frustrating).