[C#.NET][ASP.NET] 如何取得 FormView 綁定物件

[C#.NET][ASP.NET] 如何取得 FormView 綁定物件

實作方式很簡單,花了我一點點時間,記錄一下,

1.前端欄位的綁定需要使用Bind/BindItem

2.後端才能透過 IBindableTemplate 取得欄位及內容

{
    IOrderedDictionary fieldValues = new OrderedDictionary();
    
    IBindableTemplate itemTemplate = null;

    if (sourceControl.CurrentMode == FormViewMode.ReadOnly && sourceControl.ItemTemplate != null)
    {
        itemTemplate = sourceControl.ItemTemplate as IBindableTemplate;
    }
    else if (sourceControl.CurrentMode == FormViewMode.Edit && sourceControl.EditItemTemplate != null)
    {
        itemTemplate = sourceControl.EditItemTemplate as IBindableTemplate;
    }
    else if (sourceControl.CurrentMode == FormViewMode.Insert && sourceControl.InsertItemTemplate != null)
    {
        itemTemplate = sourceControl.InsertItemTemplate as IBindableTemplate;
    }

    if (itemTemplate != null)
    {
        foreach (DictionaryEntry entry in itemTemplate.ExtractValues(sourceControl))
        {
            fieldValues[entry.Key] = entry.Value;
        }
    }
    return fieldValues;
}

 

3.欄位及內容拿到之後,再透過下篇把 IDictionary 轉成物件

http://www.dotblogs.com.tw/yc421206/archive/2014/12/31/147889.aspx


文章出自:http://www.dotblogs.com.tw/yc421206/archive/2015/02/15/149493.aspx

若有謬誤,煩請告知,新手發帖請多包涵


Microsoft MVP Award 2010~2017 C# 第四季
Microsoft MVP Award 2018~2022 .NET

Image result for microsoft+mvp+logo