Control | Statement I recently was writing a BBS project, and I ran into a problem when I displayed a list of topics. The Bbs_topic data table structure is defined roughly as follows:
TABLE Bbs_topic
(
TopicID INT not NULL IDENTITY (1, 1) PRIMARY KEY,
Title NVARCHAR (40),
Author NVARCHAR (20),
Postdate DATETIME not NULL DEFAULT GETDATE (),
Content NTEXT,
Clicked INT not NULL DEFAULT 0,
Recount INT not NULL DEFAULT 0,
Lastreplyer NVARCHAR (20)
)
The following are some of the contents of the Listtopic.aspx file:
<asp:repeater id= "_topicrepeater" runat= "SERVER" datasource= "..." >
<ItemTemplate>
<%# DataBinder.Eval (Container.DataItem, "Title")%>
<%# DataBinder.Eval (Container.DataItem, "Author")%>
<%# DataBinder.Eval (Container.DataItem, "clicked")%>
<%# DataBinder.Eval (Container.DataItem, "recount")%>
<!--Note the following if statement-->
<% if ((int) (DataBinder.Eval (Container.DataItem, "recount")) ==0) {%>
----
<%} else {%>
<%# DataBinder.Eval (Container.DataItem, "Lastreplyer")%>
<%}%>
</ItemTemplate>
</asp:Repeater>
The purpose of using an if statement is to determine that the current post has a reply number of 0 o'clock, the Lastreplyer is displayed as "----." And the current post response times is not 0 o'clock, then show the name of the reply person. However, such a practice is impracticable. A similar warning will appear in IE
Compiler error Message: CS0246: Cannot find type or namespace name ' Container ' (is missing a using directive or assembly reference?) )
Even if, according to this error hint, all possible namespaces are Import into the file, other error messages will be prompted. And why is that? Probably because DataBinder.Eval and Container.DataItem are members of the Repeater class. "<%#%>" is "action" on Repeater, but "if ... else ..." Statement is different, it is "acting" on the entire page. This is like accessing local variables directly in the global process. Of course, you can do this with a statement similar to the following (three-mesh operations):
<%# ((int) (DataBinder.Eval (Container.DataItem, "recount")) ==0)
? "----"
: DataBinder.Eval (Container.DataItem, "Lastreplyer")%>
The problem is that it is difficult to deal with complex judgments.
and <%# If ... else ...%>. Why not? Because the <%#%> is equivalent to <%=%>, and <%=%> is Response.Write (), then the previous judgment is laughable:
Response.Write (If ... else ...);
If you do not want to use the IF statement in Repeater to determine the data in it, you can use the following method:
<% int _nindex=0; %> <!--define a temporary integer variable-->
<asp:repeater id= "_topicrepeater" runat= "SERVER" datasource= "..." >
<ItemTemplate>
<%# DataBinder.Eval (Container.DataItem, "Title")%>
<%# DataBinder.Eval (Container.DataItem, "Author")%>
<%# DataBinder.Eval (Container.DataItem, "clicked")%>
<%# DataBinder.Eval (Container.DataItem, "recount")%>
<%
int nrecount= (int) ((DataView) _topicrepeater.datasource). table.rows[_nindex++]["Recount"]);
Can be divided into a few sentences to write
DataView dv= (DataView) _topicrepeater.datasource;
Dv. table.rows[_nindex++]["Recount"];
if (nrecount==0) {%>
----
<%} else {%>
<%# DataBinder.Eval (Container.DataItem, "Lastreplyer")%>
<%}%>
</ItemTemplate>
</asp:Repeater>
The basic idea is to get the _topicrepeater data source (DataSource)
(Note: When I bind a Repeater data source, I use the dataset.tables["..."]. DefaultView. If you are using a different data source, take a little notice when you give Repeater.datasource a forced transition.
and return the current row (rows[_nindex++]) of the table to determine whether the recount column is 0? At the beginning, the _nindex is assigned a value of 0, and then each time the ItemTemplate is added. The purpose is to have _nindex record "rows" of the record currently being accessed.
This method is also feasible for DataGrid and DataList.
Finally, this approach is effective when it comes to binding data in complex judgments, but I don't recommend it! Because this does not conform to object-oriented encapsulation characteristics, or it is to destroy the encapsulation characteristics of the practice to make it transparent, to complete the judgment function. My recommended approach is to use the custom user control to complete the complex decision binding task.