PDA

View Full Version : Hỏi : Repeater Control



nquocbao
17-08-2005, 08:36
Trong cuốn sách mình tham khảo , nó có giới thiệu về cách lấy dữ liệu trong ItemTemplate của Repeater là



<%# Container.DataItem("ProductName")%>


Nhưng khi mình dùng thì báo lỗi



CS0118: 'System.Web.UI.WebControls.RepeaterItem.DataItem' denotes a 'property' where a 'method' was expected


Dựa vào thông báo lỗi mình fix lại



<%# Container.DataItem["ProductName"]%>


Nhưng lại bị lỗi



CS0021: Cannot apply indexing with [] to an expression of type 'object'


Đọc trong MSDN thì nó dùng



<%# DataBinder.Eval(Container, "DataItem.name") %>


Dùng cách này thì ok :(

Mình ko hiểu tại sao 2 cách trên ko dùng được :( ( cuốn sách mình đang tham khảo là VB còn mình code theo C# )

Nhân tiện hỏi luôn

<% và <%# khác nhau thế nào ?

vdhieu
17-08-2005, 19:04
VB và C# khác nhau nhiều àh. ví dụ một số thuộc tính chỉ có trong VB nhưng C# thì không thể dùng thuộc tính đó mặc dù trong MSDN có nói về thuộc tính đó.

nquocbao
17-08-2005, 20:24
Vậy dùng cách nào thì chính xác ?

depgai_lamnha
19-08-2005, 19:11
<%# DataBinder.Eval(Container, "DataItem.name") %>
Ý bác muốn viết như thế này chăng:
<%# DataBinder.Eval(Container.DataItem, "name") %>

nquocbao
19-08-2005, 23:18
ặc . 2 cách đó giống hệt nhau :( , trong VB thì dùng Container.DataItem("key") , vậy trong C# du`ng sao cho đúng

lh8x
19-08-2005, 23:34
<%# DataBinder.Eval(Container, "DataItem.name") %>

<%# ((IDataRecord)Container.DataItem)["Name"] %>

depgai_lamnha
20-08-2005, 09:02
In contrast, DataBinder.Eval is simply a method with three arguments: the naming container for the data item, the data field name, and a format string. In a templated list like DataList, DataGrid, or Repeater, the naming container is always Container.DataItem. Page is another naming container that can be used with DataBinder.Eval
<%# DataBinder.Eval(Container.DataItem, "IntegerValue", "{0:c}") %>

The format string argument is optional. If it is omitted, DataBinder.Eval returns a value of type object, as shown in the following example:
(C#)
<%# (bool)DataBinder.Eval(Container.DataItem, "BoolValue") %>

(VB)
<%# CType(DataBinder.Eval(Container.DataItem, "BoolValue"), Boolean) %>

(JScript)
<%# Boolean(DataBinder.Eval(Container.DataItem, "BoolValue")) %>