<% # databinder.eval (container.dataitem, "name")%> td>
<% # databinder.eval (container.DataItem, "HExValue")%> td>
TR>
Itemtemplate>
ask: repeater>
TABLE>
form>
This produces the web form shown below. In this example we have a Repeater control displaying color names and their hexadecimal equivalents. We know we can bind a Repeater to DataTable objects, DataView objects, SqlDataReader objects, and others. One of the nice features of data binding is how ASP.NET abstracts away the ultimate source of the data and we do not need to know the exact type of the object we are binding against. Our first data binding tip is that you can also bind against a collection of custom Objects. for Instance, IN OUR Sample Web Form We Are Using An ArrayList of Color Classes, WHERE The Color Class Is Our Own Custom Class, Defined Below. Public Class Color {
Public Color (String Name, Byte R, Byte G, BYTE B)
{
THIS.NAME = Name;
HEXVALUE = String.Format
"# {0: x2} {1: x2} {2: x2}",
R, g, b
);
}
Public String Name
{
Get {return name;}
}
Public String HEXVALUE
{
Get {return hexvalue;}
}
PRIVATE STRING NAME;
PRIVATE STRING HEXVALUE;
}
The Color Class Constructor Takes A Name, And The Red, Green, And Blue Values To Describe The Color. We can build an arraylist of the class using the follion code. Public static arraylist getColors ()
{
ArrayList List = New ArrayList ();
List.add (new color (system.drawing.color.AliceBlue.name,
System.drawing.color.AliceBlue.r,
System.drawing.color.AliceBlue.g,
System.drawing.color.AliceBlue.b)
);
List.add (new color (system.drawing.color.beige.name,
System.drawing.color.beige.r,
System.drawing.color.beige.g,
System.drawing.color.beige.b)
);
List.add (new color (system.drawing.color.chocolate.name,
System.drawing.color.chocolate.r,
System.drawing.color.chocolate.g,
System.drawing.color.chocolate.b)
);
List.add (new color (system.drawing.color.darkmagenta.name, system.drawing.color.darkmagenta.r,
System.drawing.color.darkMagenta.g,
System.drawing.color.darkMagenta.b)
);
List.add (new color (system.drawing.color.fuchsia.name,
System.drawing.color.fuchsia.r,
System.drawing.color.fuchsia.g,
System.drawing.color.fuchsia.b)
);
List.add (new color (system.drawing.color.paPayawhip.name,
System.drawing.color.paPayawhip.R,
System.drawing.color.paPayawhip.g,
System.drawing.color.paPayawhip.b)
);
List.add (new color (system.drawing.color.violet.name,
System.drawing.color.violet.r,
System.drawing.color.violet.g,
System.drawing.color.violet.b
)
);
List.add (new color (system.drawing.color.black.name,
System.drawing.color.black.R,
System.drawing.color.black.g,
System.drawing.color.black.b
)
);
Return List;
}
Displaying values inside of
tags is not the only place to use a data binding expression. You can also use data binding to change the appearance of a control. In the next sample we will set the background color of a row using data binding .
">>
<% # databinder.eval (container.dataitem, "name")%> td>
<% # databinder.eval (container.DataItem, "HExValue")%> td>
TR>
Itemtemplate>
ask: repeater>
This gives us the following form. In the next section we will dig into see how data binding happens at runtime. Under The Covers Of The Data Binding Expression In order to really understand what happens inside of the data binding expression, let's take a look at The code the runtime generates for the aspx file. public void __databind__control3 (Object Sender, System.EventArgs E) {system.web.ui.webcontrols.repeater constainer;
System.web.ui.database;
Target = (System.Web.ui.Database) (SENDER);
Container = (System.Web.ui.WebControls.repeater) (Target.BindingContainer);
#LINE 17 "E: /DEV/Xprmnt/ASPNet/Database"
Target.SetDatabaseTring (0,
System.convert.toString (DataBinder.eval (ContaValue))))))))));
#LINE 18 "E: /DEV/XPRMNT/ASPNET/DatabaseD"
Target.SetDatabasetring (1,
System.convert.toString (dataBinder.eval (Container.DataItem, "Name")))
#Line 19 "E: /DEV/Xprmnt/ASPNet/Databases/simpleData.aspx"
Target.SetDatabasetring (2,
System.convert.toString (DataBinder.eval (ContaValue))))))))));
}
The above is an excerpt from the code generated for our web form into the temporary ASP.NET files directory. It shows us how the Container variable that we use in the call to Eval is set to reference a BindingContainer setup by the runtime. This method , an event handler, will fire for each item the control needs to bind (once for each row, or once for each list item in this example). The most important point to take from the above code is how the expression we use for data binding is placed as a parameter to Convert.ToString This means we can use any expression that will yield a string For example, the following ItemTemplate would produce the same screen we saw in the last screenshot
if the Color type is in a class file under a namespace of aspnet.DataBinding. Using the DataBinder.Eval technique allows us to avoid putting messy casts into the ASPX. Instead of casts, Eval uses reflection techniques to dynamically find a property by name at runtime. Because reflection is inherently slow, Eval is relatively slower than using a cast. On the other hand, one advantage to DataBinder.Eval is that the syntax will work in an ASPX compiled for either C # or VB.NET. in the form shown above the casting syntax will only work if the ASPX page compiles with the C # compiler. While the above example demonstrates how we do not necessarily need to use DataBinder.Eval in our data Binding Expressions, Let's Take There Con CEPT One Step Further and Call a Method IN Our Code-Behind Class from The Data Binding Expression.
i = i 2;
}
Else
{
i = i 1;
}
WHILE (I
}
Return Name;
}
The method above will take the name of the color "PapayaWhip" and insert spaces in front of all uppercase letters after the first letter, yielding "Papaya Whip". Hopefully this article has demonstrated some additional techniques you can use in data binding scenarios. Don 'T limit your self to databinder.eval if the scenario calls for some additional formatting logic!
转载请注明原文地址:https://www.9cbs.com/read-79430.html