2010-04-20 49 views
1
<asp:Repeater ID="rptrParent" runat="server"> 
<ItemTemplate> 
     <li> 
      <a href="<% =ResolveUrl("~/cPanel/UserView.aspx?User=")%><%# Eval("StudentUserName") %>"> 
       <span> 
        <% ProfileCommon pc = new ProfileCommon(); 
         pc.GetProfile(Eval("StudentUserName").ToString()); 
         Response.Write(pc.FirstName + "" + pc.LastName); 
        %> 
       </span> 
      </a> 
     </li> 
</ItemTemplate> 

使用eval在服务器端代码asp.net页面

以下错误

Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control. 

上这部分

<% ProfileCommon pc = new ProfileCommon(); 
    pc.GetProfile(Eval("StudentUserName").ToString()); 
    Response.Write(pc.FirstName + "" + pc.LastName); 
%> 
+3

@The岩:ASP.Net的'Eval'不是你想象的那样。 – SLaks 2010-04-20 02:31:01

+0

你得到的错误是什么? – Luis 2010-04-20 02:31:52

+0

嘿家伙,我知道我可以使用 <%#Eval(“StudentUserName”)%> 我想获得用户的配置文件的名字和姓氏与它一起打印。 – Maverick 2010-04-20 02:45:17

回答

0

好的球员,感谢您的帮助,但我得到了解决办法:

<asp:Repeater ID="rptrParent" runat="server" onitemdatabound="rptrParent_ItemDataBound"> 
<ItemTemplate> 
     <li> 
      <a href="<% =ResolveUrl("~/cPanel/UserView.aspx?User=")%><%# Eval("StudentUserName") %>"> 
       <span> 
        <asp:Literal ID="lblUserName" runat="server"></asp:Literal> 
       </span> 
      </a> 
     </li> 
</ItemTemplate> 

和隐藏文件的代码,我写了下面的功能:

protected void rptrParent_ItemDataBound(object sender, RepeaterItemEventArgs e) 
{ 
    if (e.Item.ItemType == ListItemType.Item) 
    { 
     Literal UserName = e.Item.FindControl("lblUserName") as Literal; 
     String uName = DataBinder.Eval(e.Item.DataItem, "StudentUserName").ToString(); 
     ProfileCommon pc = Profile.GetProfile(uName); 
     UserName.Text = pc.FirstName + " " + pc.LastName + " [ " + uName + " ]"; 
    } 
} 
4

未来在这方面,你需要完整请拨打电话:

<%# Databinder.Eval(Container.DataItem,"StudentUserName") %> 
相关问题