使用Repeater模版绑定数据时,使用 <%# ((DataRowView)Container.DataItem)["name"] %> 语句来显示绑定的数据。
只能在<%# ... %>中使用((DataRowView)Container.DataItem)["name"],如果在<% ... %>使用就会提示错误:CS0246: 找不到类型或命名空间名称“Container”(是否缺少 using 指令或程序集引用?)据说是因为<%# ... %>中使用的是局部变量;而<% ... %>使用的是全局变量,因此找不到Container。那么,这里的Container是引用自哪里呢?从哪里来?

解决方案 »

  1.   

    "Container" is a local variable, see my blog:
    http://blog.joycode.com/saucer/archive/2004/04/14/19456.aspx
      

  2.   

    思归:
    看了你的Blog
    System.Web.UI.WebControls.DataGridItem Container;
    既然Container是个局部变量,那么能否在<% ... %>中直接引用呢?
      

  3.   

    <asp:DataGrid id="DataGrid1" runat="server" AutoGenerateColumns="false" GridLines="Both">
    <Columns>
     <asp:TemplateColumn HeaderText="Name">
     <ItemTemplate><%# DataBinder.Eval(Container.DataItem,"Name")%></ItemTemplate>
     </asp:TemplateColumn>
    </Columns>
    </asp:DataGrid>
      

  4.   

    先来看DatBDind的用法。
        绑定属性:<%#Property%>
        绑定集合:<asp:dataGrid id = "dg" datasource="<%#mydataview%>" runat=server/>
        绑定表达式:<%#container.dataItem%>
        绑定函数结果:<%getForm(user)%>
        从上面的例子可以看出,绑定的数据包含在〈%#%〉标签之内,这与〈%%〉标签有些相
    似,千万小要以为它们的“样子”差不多就认为它们的功能也是一样的,〈%#%〉与〈%%〉
    之间的差别非常大。〈%%〉之间的代码将会被直接执行.而〈%#%〉之间包含的代码、仅仅
    在DataBind()方法调用后,才会被执行。Page以及所有server控件都拥有DataBind万法。
      

  5.   

    happyjun2000(蓝色游侠∮质量@速度)  :
    你的意思是说<% %>执行的时候 container.dataItem 还不存在吗?
      

  6.   

    是的,我是看的《ASP.Net亲密接触》
      

  7.   

    那就是说根本就没有办法在<% %>使用Container喽?
      

  8.   

    Container只在Template里中<%# ... %>才有效,你在哪里用它?把你的编码贴出来
      

  9.   

    <% if (((DataRowView)Container.DataItem)["reply"].ToString())=="") {
    %>
    <table class="gueststyleline" width="100%" border="0" cellpadding="1" cellspacing="0" >
     <tr> 
     <td> 
     <table class="gueststylerebg" width="100%" border="0" cellspacing="0" cellpadding="6">
      <tr> 
      <td valign="top"> 
       <table width="100%" border="0" cellpadding="2" cellspacing="0" style="TABLE-LAYOUT: fixed" class="replytext">
        <tr> 
        <td>站长回复:</td>
        </tr>
        <tr> 
        <td valign="bottom">
         &nbsp;&nbsp;&nbsp;&nbsp;<%# ((DataRowView)Container.DataItem)["reply"] %></td>
        </tr>
        <tr align="right"> 
         <td valign="bottom" align="right"> 
         <hr class="gueststylehr" size="0.1" width="70%" noshade align="right" >
         回复于: <%# ((DataRowView)Container.DataItem)["redate"] %></td>
        </tr>
       </table>
      </td>
      </tr>
     </table>
    </td>
    </tr>
    </table>
    <% }%>
      

  10.   

    错了重发:<% if (((DataRowView)Container.DataItem)["reply"].ToString()=="") {
    %>
    <table class="gueststyleline" width="100%" border="0" cellpadding="1" cellspacing="0" >
     <tr> 
     <td> 
     <table class="gueststylerebg" width="100%" border="0" cellspacing="0" cellpadding="6">
      <tr> 
      <td valign="top"> 
       <table width="100%" border="0" cellpadding="2" cellspacing="0" style="TABLE-LAYOUT: fixed" class="replytext">
        <tr> 
        <td>站长回复:</td>
        </tr>
        <tr> 
        <td valign="bottom">
         &nbsp;<%# ((DataRowView)Container.DataItem)["reply"] %></td>
        </tr>
        <tr align="right"> 
         <td valign="bottom" align="right"> 
         <hr class="gueststylehr" size="0.1" width="70%" noshade align="right" >
         回复于: <%# ((DataRowView)Container.DataItem)["redate"] %></td>
        </tr>
       </table>
      </td>
      </tr>
     </table>
    </td>
    </tr>
    </table>
    <% }%>
      
      

  11.   

    look through the error page, click on "ow Complete Compilation Source" link, you will see your code is wrong, use a placeholder
    <asp:PlaceHolder id="ph" runat="server" Visible='<%# ((DataRowView)Container.DataItem)["reply"].ToString().Length == 0  %>'><table class="gueststyleline" width="100%" border="0" cellpadding="1" cellspacing="0" >
     <tr> 
     <td> 
     <table class="gueststylerebg" width="100%" border="0" cellspacing="0" cellpadding="6">
      <tr> 
      <td valign="top"> 
       <table width="100%" border="0" cellpadding="2" cellspacing="0" style="TABLE-LAYOUT: fixed" class="replytext">
        <tr> 
        <td>站长回复:</td>
        </tr>
        <tr> 
        <td valign="bottom">
         &nbsp;<%# ((DataRowView)Container.DataItem)["reply"] %></td>
        </tr>
        <tr align="right"> 
         <td valign="bottom" align="right"> 
         <hr class="gueststylehr" size="0.1" width="70%" noshade align="right" >
         回复于: <%# ((DataRowView)Container.DataItem)["redate"] %></td>
        </tr>
       </table>
      </td>
      </tr>
     </table>
    </td>
    </tr>
    </table></asp:PlaceHolder>