ASP.NET 페이지간에 값을 유지하거나 전달하는 방법에는 여러 가지가 있습니다.
자세한 내용은 here을 참조하십시오.
저장 및 복원 할 수있는 "톤"이 있다고 언급 했으므로이 프로세스를 자동화하는 방법을 찾으려고했습니다.
저장 방법으로 세션을 사용하여 모든 컨트롤의 값을 IPostBackDataHandler
으로 유지합니다. 어쨌든 테스트는 아니지만 잘하면 도움이됩니다.
private FormStorage storage;
protected void Page_PreInit(object sender, EventArgs e)
{
storage = new FormStorage(this);
}
protected void BtnRedirect_Click(object sender, EventArgs e)
{
Response.Redirect("RedirectForm.aspx");
}
주 그것이로드 및 Page_Load/Page_PreRender
에 묵시적으로 저장합니다
using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
public class FormStorage
{
private System.Web.UI.Page _page = null;
public Dictionary<String, Dictionary<String, String>> storage
{
get { return (Dictionary<String, Dictionary<String, String>>)_page.Session["FormStorage"]; }
set { _page.Session["FormStorage"] = value; }
}
public FormStorage(System.Web.UI.Page page)
{
_page = page;
initHandler();
if (this.storage == null)
{
this.storage = new Dictionary<String, Dictionary<String, String>>();
}
if(!this.storage.ContainsKey(_page.Request.Path))
this.storage.Add(_page.Request.Path, new Dictionary<String, String>());
}
private void initHandler()
{
_page.Init += Init;
_page.Load += Load;
}
private void Init(Object sender, EventArgs e)
{
loadForm();
}
private void Load(Object sender, EventArgs e)
{
saveForm();
}
private void loadForm()
{
var pageStorage = storage[_page.Request.Path];
var e = pageStorage.GetEnumerator();
while (e.MoveNext())
{
loadControl(e.Current.Key, e.Current.Value);
}
}
private void loadControl(String ID, String value)
{
Control control = findControlRecursively(_page, ID);
if (control != null)
{
setControlValue(control, value);
}
}
private void setControlValue(Control control, String value)
{
if (control is ITextControl)
{
ITextControl txt = (ITextControl)control;
txt.Text = value == null ? "" : value;
}
else if (control is ICheckBoxControl)
{
ICheckBoxControl chk = (ICheckBoxControl)control;
chk.Checked = value != null;
}
else if (control is ListControl)
{
ListControl ddl = (ListControl)control;
if (value == null)
ddl.SelectedIndex = -1;
else
ddl.SelectedValue = value;
}
}
public void saveForm()
{
saveControlRecursively(this._page);
}
private void saveControlRecursively(Control rootControl)
{
if (rootControl is IPostBackDataHandler)
{
var postBackData = _page.Request.Form[rootControl.ID];
storage[_page.Request.Path][rootControl.ID] = postBackData;
}
if (rootControl.HasControls())
foreach (Control subControl in rootControl.Controls)
saveControlRecursively(subControl);
}
private static Control findControlRecursively(Control rootControl, String idToFind)
{
if (rootControl.ID == idToFind)
return rootControl;
foreach (Control subControl in rootControl.Controls)
{
Control controlToReturn = findControlRecursively(subControl, idToFind);
if (controlToReturn != null)
{
return controlToReturn;
}
}
return null;
}
}
여기에 또 다른 페이지로 리디렉션 페이지의 예시적인 구현이다. 따라서 FormStorage
인스턴스는 Page_PreInit
에 만들어야합니다. 프로그래밍 Page_Load
후에 값을 변경하려면
, 당신은 FormStorage
이 Page_Load
모든 포스트 백 데이터를 자동 저장되므로,이 값은 무시되도록 (Page_PreRender
예를 들면) 수동 storage.saveForm()
를 호출해야합니다.
편집 : sh4nx0r의 history.go
접근 방식은 더 확장 성이 좋기 때문에 아마 더 좋습니다. 내 접근 방식은 세션을 사용하고 엄청난 양의 (가능한) 사용자가있는 인터넷 응용 프로그램에 적합하지 않습니다.
내 장점 중 하나는 자바 스크립트가 사용 중지 된 경우에도 작동한다는 것입니다. 하나의 큰 이점은 리디렉션 할 페이지를 제어 할 수 있다는 것입니다. 여러 리디렉션에서 값을 복원 할 수도 있습니다.
이 ASP.NET 또는 ASP.NET-MVC입니까? –
ASP.NET 3.5, MVC가 아님 – markzzz
http://msdn.microsoft.com/ko-kr/library/6c3yckfw%28v=vs.90%29.aspx –