The original post can be found here, last updated: 2011-11-12
Introduction/Catalog
Due to limited time, synchronization cannot be guaranteed in more than one blog article. At the following address, you can view up-to-date content, hope you understand:
Download sample: JQueryElementDemo-en.zip, the directory is /repeater/Default.aspx.
This article will explain in detail how to bind and handle field in the Repeater
control. The directory is as follows:
Please view the prepare section at 30 Minutes Grasp ASP.NET jQuery Repeater.
Can bound fields in the ItemTemplate
/UpdatedItemTemplate
/InsertedItemTemplate
/
RemovedItemTemplate
/EditItemTemplate
template.
Fields can be bound as content or attribute of the tag.
You can use #{<field name>}
to bind a field, such as:
<ItemTemplate>
<td>
#{bookname}
</td>
<td>
<strong>Evaluate:</strong> <span class="rank rank#{rank}"></span>
</td>
</ItemTemplate>
In the example above, the field bookname
is bound to the contents of tag, the rank
field is bound in the tag's class
attribute.
Using *`#{<field name>[,<field expression>]}`*
to convert the value in the field, and then outputs, such as:
<ItemTemplate>
<td>
<strong>Discount:</strong>
#{discount,Math.floor(# * 100) / 10}
#{discount,convertDiscount(#)}
</td>
</ItemTemplate>
In the field expression, use the #
to indicate the binding field. discount
field in the above code through a JavaScript expression and a method to convert the value and output, as follows:
<script type="text/javascript">
function convertDiscount(discount) {
return discount >= 0.7 ? '<strong>Clearance</strong>' : 'Sale';
}
</script>
Using default format to return JSON, the return value of date field may be similar to "\/Date(xxxxxxxxxx)\/
", you can use jQuery.panzer.formatDate
method to format the date, or use jQuery.panzer.convertToDate
to format "\/Date(xxxxxxxxxx)\/
" into a date
type, such as:
<ItemTemplate>
<td>
<strong>Publication Date:</strong>
<span class="publishdate">
#{publishdate,jQuery.panzer.formatDate(#,'yyyy-M-d')}
</span>
</td>
</ItemTemplate>
The first parameter of method jQuery.panzer.formatDate
is a Date
or a string
like "\/Date(xxxxxxxxxx)\/
". The second parameter is a format string
, which is similar to the ToString
method of DateTime
class in .NET, for example:
<script type="text/javascript">
var date = new Date(2011, 0, 1, 20, 1, 3);
$.panzer.formatDate(date, 'y');
$.panzer.formatDate(date, 'yy');
$.panzer.formatDate(date, 'yyyy');
$.panzer.formatDate(date, 'M');
$.panzer.formatDate(date, 'MM');
$.panzer.formatDate(date, 'yyyy-MM');
$.panzer.formatDate(date, 'd');
$.panzer.formatDate(date, 'dd');
$.panzer.formatDate(date, 'yyyy-MM-dd');
$.panzer.formatDate(date, 'H');
$.panzer.formatDate(date, 'HH');
$.panzer.formatDate(date, 'yyyy-MM-dd HH');
$.panzer.formatDate(date, 'h');
$.panzer.formatDate(date, 'hh');
$.panzer.formatDate(date, 'yyyy-MM-dd hh');
$.panzer.formatDate(date, 'm?');
$.panzer.formatDate(date, 'mm');
$.panzer.formatDate(date, 'yyyy-MM-dd hh:mm');
$.panzer.formatDate(date, 's');
$.panzer.formatDate(date, 'ss');
$.panzer.formatDate(date, 'yyyy-MM-dd hh:mm:ss');
</script>
In the fields
expression, you should use jQuery instead of $
, to prevent problems caused by the compressed script.
If you need to display a different style based on the value of the field, you can bind fields in the class attribute, such as:
<ItemTemplate>
<td>
<strong>Evaluate:</strong>
#{rank}
<span class="rank rank#{rank}"></span>
</td>
</ItemTemplate>
At the beginning of the page, define some styles of rank
:
<style type="text/css">
.rank
{
background-color: #cc0000;
height: 15px;
display: inline-block;
}
.rank1
{
width: 10px;
}
.rank2
{
width: 30px;
}
.rank3
{
width: 50px;
}
.rank4
{
width: 70px;
}
.rank5
{
width: 90px;
}
</style>
rank
field in the examples is likely to be 1
to 5
, so styles also define rank1
to rank5
.
Comment