Turn: no new page navigation

Source: Internet
Author: User
Document directory
  • Question
  • Answer
Question

With ASP. NET Ajax, we can implement asynchronous sending and receiving without refreshing in the page. However, it is impossible for us to place all functions in one page. This design is inconvenient and the logic for dynamically loading controls is complicated. Therefore, we still need to navigate between multiple pages. Can the navigation between pages be refreshed?

Answer

It is unavoidable to refresh the inter-page navigation, but we cannot leave the logic that originally belongs to multiple pages in one page, so we need to find a way to separate these logics. Here, we use usercontrol as the basic view unit, instead of page. In this way, changing the loaded usercontrol changes the view, which is equivalent to implementing navigation, however, the page does not change, so you can skip the new page.

But how to dynamically load usercontrol? Page. loadcontrol? Isn't that troublesome? If you do not handle it yourself, it may cause problems such as not triggering the control event. Therefore, I made a viewpanel control that can dynamically load usercontrol. You can use it directly.

This control has only one attribute to be concerned with, that is, viewpanel. virtualpath "~ /Usercontrols/sample. ascx ") after it is set, it will automatically help you load the usercontrol. During the running process, if you modify this attribute, the control saves the value through viewstate, that is, the value is permanently valid for one modification. No matter how many replies are sent, until you modify the value again next time.

It is best to implement a no-refreshing navigation between views. You only need to place a viewpanel in updatepanel, and then use the usercontrol Design for different views (in fact, the design is similar to the use of page ), by dynamically loading these usercontrols through viewpanel, you can achieve a non-Refresh navigation between views.

 

 

 

 using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace CatChen.Cocollab.Web.UI.Controls
{
public class ViewPanel : System.Web.UI.Control, System.Web.UI.INamingContainer
{
private string _virtualPath;
private Control _view;

public string VirtualPath
{
get { return this._virtualPath; }
set
{
string oldValue = this._virtualPath;
this._virtualPath = value;
if (this.Page != null && value != oldValue)
{
this.ChildControlsCreated = false;
this.EnsureChildControls();
}
}
}
public Control View
{
get { return this._view; }
}

protected override void OnInit(EventArgs e)
{
base.OnInit(e);
if (!string.IsNullOrEmpty(this.VirtualPath))
{
this.EnsureChildControls();
}
}

protected override void LoadViewState(object savedState)
{
Pair pair = savedState as Pair;
if (pair != null)
{
base.LoadViewState(pair.First);
this.VirtualPath = pair.Second as string;
}
}

protected override void CreateChildControls()
{
this.Controls.Clear();

if (string.IsNullOrEmpty(this.VirtualPath))
{
return;
}

if (this.Page == null)
{
throw new Exception("ViewPanel.Page is null.");
}

this._view = this.Page.LoadControl(this.VirtualPath);
if (this._view == null)
{
throw new Exception("ViewVirtualPath cannot be loaded.");
}

this._view.ID = "ucView";
this.ClearChildState();
this.Controls.Add(this._view);
}

protected override object SaveViewState()
{
return new Pair(base.SaveViewState(), this.VirtualPath);
}
}
}

 

The original text is also from the blog site. Keep it for research.

Http://www.cnblogs.com/cathsfz/archive/2006/12/10/587925.html ()

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.