2008年2月27日 星期三

把網頁當作物件來操作

重點在兩個語法:Server.Transfer和Context.Handler

下面是簡單範例

網頁A按下按鈕後導向到網頁B
private void Button1_Click(object sender, System.EventArgs e)
{
  Server.Transfer("anotherwebform.aspx");
}

網頁B把網頁A當作物件來使用
private void Page_Load(object sender, System.EventArgs e)
{
  //create instance of source web form
  WebForm1 wf1;
  //get reference to current handler instance
  wf1=(WebForm1)Context.Handler;
  Label1.Text=wf1.Name;
  Label2.Text=wf1.EMail;
}

不過盡量別濫用
畢竟網頁應該是屬於使用者介面層物件
還是要跟商業邏輯層的物件區隔
不要包成一大包會比較清楚明白

2008年2月14日 星期四

為何gridview無法依照我指定的itemstyle對齊

試了半天才發現
原來是因為所有的欄位都指定寬度造成的
請不要指定最後一欄的寬度
問題就會消失囉
希望20年(?)後若發生同樣問題 我還記得回來查詢原因 不要又試半天

如何於updatepanel1中按下按鈕後更新updatepanel2

updatepanel1內含button1
updatepanel2內含sqldatasource1,gridview1
於Button1_Click()中會異動資料庫並造成sqldatasource1的結果有所改變,必須即時更新gridview1內容
則於更新資料後要執行SqlDataSource1.DataBind(),GridView1.DataBind()
且updatepanel2加入非同步回傳觸發針對button1之click事件
如此便可即時更新gridview1

Entity Framework 建立新物件並儲存後馬上取得關聯資料

使用 CreateProxy 建立物件,不要直接 new var newmodel = _contextXXX.CreateProxy<yyy>(); ... _contextXXX.yyy.Add(newmodel); await _contextXXX.SaveC...