Use the IF statement in the REPEATER control (reproduced from Afritxia blog)

xiaoxiao2021-03-06  49

I have recently been writing a BBS project, when I display the topic list, I have encountered a problem. BBS_TOPIC data sheet structure definition is roughly the following:

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)

)

Below is some of the part of the listtopic.aspx file:

<% # DataBinder.eval (Container.DataItem, "Title")%>

<% # DataBinder.eval (Container.DataItem, "Author")%>

<% # DataBinder.eval (Container.DataItem, "Clicked")%>

<% # DataBinder.eval (Container.DataItem, "Recount")%>

<% IF ((int) (Container.eval ("Recount") == 0) {%>

----

<%} else {%>

<% # DataBinder.eval (Container.DataItem, "LastReplyer")%>

<%}%>

The purpose of using the IF statement is to determine that the number of times the current post is 0, the LastReplyer is displayed as "----". When the number of reply to the current post is not 0, the name of the reply is displayed. However, such a practice is not enough. Similar warning compiler error messages in IE: CS0246: Can't find the type or namespace name "Container" (whether it is missing the USING directive or assembly reference?) Even if this error prompt, put all possible Namespaces all import into this file, or other error messages will still be prompted. As for why? It may be because DataBinder.eval and Container.DataItem are the members of the Repeater class. "<% #%>" Is "role" on the REPEATER, but the "if ... Else ..." statement is different, it is "role" on the entire page. This is like a partial variable directly in the global process. Of course, the above functions can also be achieved with a statement similar to the following (three-metronal operation):

<% # ((int) (dataBinder.eval (ContaNd ") == 0)

? "----": DataBinder.eval (Container.DataItem, "LastReplyer")%>

The problem is that for complex judgment, such a practice is quite difficult. <% # If ... else ...%> Why is this usage? Because <% #%> is equivalent to <% =%>, <% =%> is response.write (), then the previous judgment is coming: response.write (if ... Else ...); if If you want to use the IF statement in the REPEater to determine the data, then the following method can be used:

<% int _nIndex = 0;%>

<% # 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 also be divided into a few words to write

// DataView DV = (DataView) _topicrepeater.datasource;

// DV.Table.Rows [_NINDEX ] ["Recount"];

IF (NRECOUNT == 0) {%>

----

<%} else {%>

<% # DataBinder.eval (Container.DataItem, "LastReplyer")%>

<%}%>

转载请注明原文地址:https://www.9cbs.com/read-40762.html

New Post(0)