2010-04-20 34 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") %> 
相關問題