VisualStudio2008環境下WebPart使用小結
A.使用WebPart的頁面必須放置一個WebPartManager,用來管理頁面上所有的WebPart,還可以設置
WebPart頁的模式,字段佈局頁面只用到了兩個狀態,如下
LayoutWebPartManager.DisplayMode = WebPartManager.DesignDisplayMode;
LayoutWebPartManager.DisplayMode = WebPartManager.BrowseDisplayMode;
上面的兩行代碼分別將頁面的顯示模式設置爲設計模式和瀏覽模式,設計模式下,可以將一個WebPart
拖動到頁面上的任意一個WebPartZone中,在瀏覽模式下,可以看到拖動後頁面的佈局效果。
B.拖動WebPart時會觸發WebPartManager的WebPartMoving事件,在這個事件中,可以利用參數
WebPartMovingEventArgs e取得如下內容
① 正在移動WebPart e.WebPart
② 藉助WebPartManager取得正在移動WebPart的Index
int nWebPartIndex = LayoutWebPartManager.WebParts.IndexOf(e.WebPart);
③ 移動後WebPart所在的目標區域 e.Zone
④ 藉助WebPartManager取得移動後WebPart所在的目標區域的Index
int nZoneIndex = LayoutWebPartManager.Zones.IndexOf(e.Zone) ;
⑤ 移動後WebPart在目標區域所處的位置 e.ZoneIndex
⑥ 滿足一定的條件時,可以通過e.Cancel = true取消該事件
C.拖動WebPart成功後會觸發WebPartManager的WebPartMoved事件,在這個事件中可以做一些移動成
功後需要處理的事情。比如在元素字段佈局頁面移動字段對應的WebPart成功後,它所在WebPartZone
中的所有WebPart的位置序號Index需要重新設置,保證這些位置序號Index連續。
D .利用WebPartManager的DeleteWebPart方法清除WebPartManager自己在WebPartZone加載的控件,
如下:
foreach (WebPartZone wpzItem in LayoutWebPartManager.Zones)
{
for (int n = wpzItem.WebParts.Count - 1; n >= 0; n--)
{
LayoutWebPartManager.DeleteWebPart(wpzItem.WebParts[n]);
}
}
E.利用WebPartManager的AddWebPart方法在WebPartZone中動態加載控件,這個方法需要三個參數,
第一個是需要加載的WebPart控件--WebPart webPart,第二個參數指定在哪個WebPartZone中添加--
WebPartZoneBase zone,第三個參數決定加到WebPartZone中的哪個位置--int zoneIndex,如下:
AddWebPartToZone(WebPart wpItem)
{
int zonePosition = Convert.ToInt32(wrpItem.zonePosition);
string strZoneID = "WebPartZone" + wrpItem.zoneInex;
WebPartZoneBase wpZone = LayoutWebPartManager.Zones[strZoneID];
LayoutWebPartManager.AddWebPart(wpItem, wpZone, zonePosition);
}
F.動態加載控件之後用FindControl方法通過ID找到對應控件,再設置這個控件的一些屬性。如果在
加到WebPartZone之前,將WebPart中的控件的屬性設置好後再加到WebPartZone中,會出現奇怪的問題
,目前也不知道爲什麼。
// 設置WebPart中Label的顯示內容
Control ctrLabel = LayoutWebPartManager.Zones[strZoneID].WebParts
[zonePosition].FindControl("DisplayContent");
if (ctrLabel != null)
{
Label lbl = ctrLabel as Label;
lbl.Text = wrpItem.displayName;
}
// 設置WebPart中DropDownList的寬度
Control ctrInput = LayoutWebPartManager.Zones[strZoneID].WebParts
[zonePosition].FindControl("InputControl");
if (ctrInput != null)
{
DropDownList ddl = ctrInput as DropDownList;
ddl.Width = Convert.ToInt32(wrpItem.defaultWidth);
}
G.修改WebPartZone的標題和WebPartZone中沒有控件時的提示信息,如下:
private void ModifyEmptyZoneText()
{
int nIndex = 1;
foreach (WebPartZone item in LayoutWebPartManager.Zones)
{
// WebPartZone的標題
item.HeaderText = PCConst.ELEMENTLAYOUTSET_WEBPARTZONE_HEADERTEXT +
nIndex.ToString();
// WebPartZone中沒有控件時的提示信息
item.EmptyZoneText = PCConst.ELEMENTLAYOUTSET_WEBPARTZONE_EMPTYZONETEXT;
nIndex++;
}
}
H.動態生成WebPart
字段佈局頁面中使用WebPart佈局,由於通過WebPartManager的AddWebPart方法只能在WebPartZone中
加入WebPart控件,所以將每個自定義字段類型對應的控件都封裝成爲一個獨立的WebPart類,如下
public class FloatTextBoxWP : VWebPart
{
VTextBoxValidator txtInput;
public FloatTextBoxWP()
: base()
{
}
protected override void CreateChildControls()
{
base.CreateChildControls();
tdControl = new TableCell();
txtInput = new VTextBoxValidator();
txtInput.ID = "InputControl";
txtInput.MatchType = VTextBoxValidator.enMatchType.DecimalNumber;
Panel pnl = new Panel();
pnl.Controls.Add(txtInput);
pnl.Enabled = false;
tdControl.Controls.Add(pnl);
trItem.Cells.Add(tdControl);
this.Controls.Add(trItem);
ChildControlsCreated = true;
}
}
在頁面中使用WebPartManager的AddWebPart方法動態加載WebPart
LayoutWebPartManager.AddWebPart(wpItem, wpZone, zonePosition);
第一個參數是動態生成的WebPart控件,第二個參數是頁面上放置的某個WetPartZone(目前由於無法動
態生成WetPartZone,所以在頁面上靜態放置了足夠多的WetPartZone);第三個參數是WebPart在
WebPartZone中的位置。
在PageLoad事件中也需要每次都重新加載一次WebPart,否則找不到WebPart中的控件。