天天躁日日躁狠狠躁AV麻豆-天天躁人人躁人人躁狂躁-天天澡夜夜澡人人澡-天天影视香色欲综合网-国产成人女人在线视频观看-国产成人女人视频在线观看

深入ASP.NET數(shù)據(jù)綁定

  在ASP.NET我們在使用Repeater,DetailsView,F(xiàn)ormView,GridView等數(shù)據(jù)綁定模板時(shí),都會使用<%# Eval("字段名") %>或<%# Bind("字段名") %>這樣的語法來單向或雙向綁定數(shù)據(jù)。但是我們卻很少去了解,在這些語法的背后,ASP.NET究竟都做了哪些事情來方便我們使用這樣的語法來綁定數(shù)據(jù)。究竟解析這樣的語法是在編譯時(shí),還是運(yùn)行時(shí)?如果沒有深入去了解,我們肯定不得而知。這個(gè)簡短的系列文章就是帶我們大家一起去深入探究一下ASP.NET綁定語法的內(nèi)部機(jī)理,以讓我們更加全面的認(rèn)識和運(yùn)用它。

  事件的起因是,我希望動(dòng)態(tài)的為Repeater控件添加行項(xiàng)模板,我可以通過實(shí)現(xiàn)ITempate接口的方式來動(dòng)態(tài)添加行模板。并希望它通過普通的頁面綁定語法來完成數(shù)據(jù)字段的綁定功能,如下就是一個(gè)簡單的例子:

 /// <summary>
/// Summary description for DynamicTemplate
/// </summary>
public class DynamicTemplate : ITemplate
{
public DynamicTemplate()
{
//
// TODO: Add constructor logic here
//
}
#region ITemplate Members

public void InstantiateIn(Control container)
{
TextBox textBox
= new TextBox();
textBox.Text
= @"<%# Eval(""ID"") %>";
container.Controls.Add(textBox);
}
#endregion
}

  在.NET 2.0中新增了雙向的數(shù)據(jù)綁定方式,主要用在GridView,DetailsView,FormView等數(shù)據(jù)容器控件中,結(jié)合DataSourceControl就可以非常輕松的完成數(shù)據(jù)的更新和提交工作,而不需要我們手工去遍歷輸入控件的值。那在這樣的雙向數(shù)據(jù)綁定中,ASP.NET又是做了哪些工作,來為我們透明輸入控件與字段的取值與對應(yīng)關(guān)系,讓我們可以在DataSouceControl中方便得到數(shù)據(jù)項(xiàng)修改前的值和修改后的值?下面就讓我們一起來從一段頁面代碼開始吧:

<ASP:DetailsDataSouce ID="DetailsDataSouce1" runat="server">
</ASP:DetailsDataSouce>
<ASP:DetailsView ID="detailsView" runat="server" DefaultMode="Edit" DataSourceID="DetailsDataSouce1">
<Fields>
<ASP:TemplateField>
<HeaderTemplate>
電流:
</HeaderTemplate>
<EditItemTemplate>
<ASP:TextBox ID="textBox1" runat="server" Text='<%# Bind("[電流{a}]")%>'></ASP:TextBox>
</EditItemTemplate>
</ASP:TemplateField>
</Fields>
</ASP:DetailsView>

  在了解了數(shù)據(jù)綁定語法的原理后,我還想來談?wù)勎抑袑?shí)踐過程中遇到的一些問題以及其它實(shí)用的綁定技巧。首先我們就來說說,特殊字段名的問題。我們知道在數(shù)據(jù)庫當(dāng)中,如果表名或字段名中包含有一些特殊的不能是合法的字符時(shí),都會使用[]將它們引起來,以便他們能夠正常使用。但是在<%# Eval("")%>的綁定語句當(dāng)中,同時(shí)可以使用[],但是對于字段名中包含"(",")","[","]"這4個(gè)字符卻始終運(yùn)行出錯(cuò)。假設(shè)像我下面這樣來綁定"電壓(V)":<%# Eval("[電壓(V)]")%> 

  那么就會得到一個(gè)運(yùn)行時(shí)錯(cuò)誤:

  DataBinding:“System.Data.DataRowView”不包含名為“電壓”的屬性。

  表明括號是被認(rèn)為是一個(gè)特殊字符,那我們?nèi)绻o字段名加上[],如下:<%# Eval("[電壓(V)]")%>

  此時(shí),我們會得到另一個(gè)運(yùn)行時(shí)錯(cuò)誤:

  電壓(V 既不是表 DataTable1 的 DataColumn 也不是 DataRelation。

  表明,即使加上[]也無法解決這個(gè)特殊字段名的問題。同時(shí)字段名中如果也存在中括號,也是會出現(xiàn)這樣的問題的。但是這樣的字段名卻在GridView的自動(dòng)生成列中能被正常綁定呢?問題會出現(xiàn)在哪里呢?分析和對比GridView的自動(dòng)生成列與Eval這樣的綁定語法在最終執(zhí)行綁定代碼上的不同,我們可以發(fā)現(xiàn),GridView的自動(dòng)生成列取值并不是使用DataBinder.Eval這個(gè)方法,它內(nèi)部有自己的取值方式,但是在實(shí)現(xiàn)上卻是大同小異的。那究竟是在哪里出現(xiàn)了問題呢?我們找出DataBinder類的定義:

代碼
[ASPNETHostingPermission(SecurityAction.LinkDemand, Level=200)]

public sealed class DataBinder

{

// Fields

private static readonly char[] expressionPartSeparator = new char[] { '.' };

private static readonly char[] indexExprEndChars = new char[] { ']', ')' };

private static readonly char[] indexExprStartChars = new char[] { '[', '(' };



// Methods

public static object Eval(object container, string expression)

{

if (expression == null)

{

throw new ArgumentNullException("expression");

}

expression
= expression.Trim();

if (expression.Length == 0)

{

throw new ArgumentNullException("expression");

}

if (container == null)

{

return null;

}

string[] expressionParts = expression.Split(expressionPartSeparator);

return Eval(container, expressionParts);

}



private static object Eval(object container, string[] expressionParts)

{

object propertyValue = container;

for (int i = 0; (i < expressionParts.Length)&& (propertyValue != null); i++)

{

string propName = expressionParts[i];

if (propName.IndexOfAny(indexExprStartChars)< 0)

{

propertyValue
= GetPropertyValue(propertyValue, propName);

}

else

{

propertyValue
= GetIndexedPropertyValue(propertyValue, propName);

}

}

return propertyValue;

}



public static string Eval(object container, string expression, string format)

{

object obj2 = Eval(container, expression);

if ((obj2 == null)||(obj2 == DBNull.Value))

{

return string.Empty;

}

if (string.IsNullOrEmpty(format))

{

return obj2.ToString();

}

return string.Format(format, obj2);

}



public static object GetDataItem(object container)

{

bool flag;

return GetDataItem(container, out flag);

}



public static object GetDataItem(object container, out bool foundDataItem)

{

if (container == null)

{

foundDataItem
= false;

return null;

}

IDataItemContainer container2
= container as IDataItemContainer;

if (container2 != null)

{

foundDataItem
= true;

return container2.DataItem;

}

string name = "DataItem";

PropertyInfo property
= container.GetType().GetProperty(name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);

if (property == null)

{

foundDataItem
= false;

return null;

}

foundDataItem
= true;

return property.GetValue(container, null);

}



public static object GetIndexedPropertyValue(object container, string expr)

{

if (container == null)

{

throw new ArgumentNullException("container");

}

if (string.IsNullOrEmpty(expr))

{

throw new ArgumentNullException("expr");

}

object obj2 = null;

bool flag = false;

int length = expr.IndexOfAny(indexExprStartChars);

int num2 = expr.IndexOfAny(indexExprEndChars, length + 1);

if (((length < 0)||(num2 < 0))||(num2 == (length + 1)))

{

throw new ArgumentException(SR.GetString("DataBinder_Invalid_Indexed_Expr", new object[] { expr }));

}

string propName = null;

object obj3 = null;

111: string s = expr.Substring(length + 1,(num2 - length)- 1).Trim();

if (length != 0)

{

propName
= expr.Substring(0, length);

}

if (s.Length != 0)

{

if (((s[0] == '"')&& (s[s.Length - 1] == '"'))||((s[0] == '/'')&& (s[s.Length - 1] == '/'')))

{

obj3
= s.Substring(1, s.Length - 2);

}

else if (char.IsDigit(s[0]))

{

int num3;

flag
= int.TryParse(s, NumberStyles.Integer, CultureInfo.InvariantCulture, out num3);

if (flag)

{

obj3
= num3;

}

else

{

obj3
= s;

}

}

else

{

obj3
= s;

}

}

if (obj3 == null)

{

throw new ArgumentException(SR.GetString("DataBinder_Invalid_Indexed_Expr", new object[] { expr }));

}

object propertyValue = null;

if ((propName != null)&& (propName.Length != 0))

{

propertyValue
= GetPropertyValue(container, propName);

}

else

{

propertyValue
= container;

}

if (propertyValue == null)

{

return obj2;

}

Array array
= propertyValue as Array;

if ((array!= null)&& flag)

{

return array.GetValue((int) obj3);

}

if ((propertyValue is IList)&& flag)

{

return ((IList) propertyValue)[(int) obj3];

}

PropertyInfo info
= propertyValue.GetType().GetProperty("Item", BindingFlags.Public | BindingFlags.Instance, null, null, new Type[] { obj3.GetType()}, null);

if (info == null)

{

throw new ArgumentException(SR.GetString("DataBinder_No_Indexed_Accessor", new object[] { propertyValue.GetType().FullName }));

}

return info.GetValue(propertyValue, new object[] { obj3 });

}



public static string GetIndexedPropertyValue(object container, string propName, string format)

{

object indexedPropertyValue = GetIndexedPropertyValue(container, propName);

if ((indexedPropertyValue == null)||(indexedPropertyValue == DBNull.Value))

{

return string.Empty;

}

if (string.IsNullOrEmpty(format))

{

return indexedPropertyValue.ToString();

}

return string.Format(format, indexedPropertyValue);

}



public static object GetPropertyValue(object container, string propName)

{

if (container == null)

{

throw new ArgumentNullException("container");

}

if (string.IsNullOrEmpty(propName))

{

throw new ArgumentNullException("propName");

}

PropertyDescriptor descriptor
= TypeDescriptor.GetProperties(container).Find(propName, true);

if (descriptor == null)

{

throw new HttpException(SR.GetString("DataBinder_Prop_Not_Found", new object[] { container.GetType().FullName, propName }));

}

return descriptor.GetValue(container);

}



public static string GetPropertyValue(object container, string propName, string format)

{

object propertyValue = GetPropertyValue(container, propName);

if ((propertyValue == null)||(propertyValue == DBNull.Value))

{

return string.Empty;

}

if (string.IsNullOrEmpty(format))

{

return propertyValue.ToString();

}

return string.Format(format, propertyValue);

}

internal static bool IsNull(object value)

{

if ((value != null)&& !Convert.IsDBNull(value))

{

return false;

}

return true;

}

}

NET技術(shù)深入ASP.NET數(shù)據(jù)綁定,轉(zhuǎn)載需保留來源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時(shí)間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: free高跟丝袜秘书hd | 柠檬福利精品视频导航 | 亚洲精品久久午夜麻豆 | 老师我好爽再深一点老师好涨 | 国产午夜精品久久久久婷婷 | 久久久免费观看 | 久久久亚洲国产精品主播 | 欧美做真爱欧免费看 | 最近的2019中文字幕国语版 | 偷拍久久国产视频免费 | 亚洲第一区欧美日韩精品 | 亚洲 国产 日韩 欧美 在线 | 5g天天影院天天看天天爽 | free性中国hd护士高清 | 姑娘视频日本在线播放 | caoporm国产精品视频免费 | 亚洲看片无码免费视频 | 国产精品视频第一区二区三区 | 人成片在线观看亚洲无遮拦 | 国产 精品 亚洲 欧美 高清 | 国产精品亚欧美一区二区三区 | 亚洲 日韩 欧美 国产专区 | 久久久黄色片 | 中国成人在线视频 | 被同桌摸出水来了好爽的视频 | 国产午夜精AV在线麻豆 | 亚洲一区在线观看无码欧美 | 免费可以看污动画软件 | 亚洲国产货青视觉盛宴 | 国产精品自产拍在线观看网站 | 久久综合色一综合色88中文 | 午夜噜噜噜私人影院在线播放 | 久久99久久成人免费播放 | 最新快播网站 | 国产成人刺激视频在线观看 | 欧美高清18| 久久精品国产欧美成人 | 国产久久精品热99看 | 在线播放国产视频 | 国产线精品视频在线观看 | 亚洲欧美日韩人成 |