2013-10-19 60 views
0

我對asp.net仍然陌生。我有一些問題,我想問一下,我來自PHP和C++。asp datalist任何人都可以詳細解釋

第一個問題。在html或aspx中。如果我想編寫C#代碼,通常<%%>應該工作,但什麼是底部3

<% %> 
<%@ 
<%# %> 

的Container.DataItem來自數據源來自Datalist中之間的不同。

其次,代碼的底部給我錯誤。因此我想對此作出解釋。這在C++中似乎是合法的。錯誤無效的爭論?從MSDN評估和演示應該返回對象或字符串

<div class="newPanel"> 
    <asp:DataList ID="DataList_News" runat="server" CssClass="newPanel"> 
     <ItemTemplate> 
      <div class="news"></div> 

      <div id="news_space" runat="server" visible="<%# DisplaySpace(System.Web.UI.DataBinder.Eval(Container.DataItem, "product_id")); %>"></div> 
     </ItemTemplate> 
    </asp:DataList> 
</div> 


public partial class CommonCx_captcha_displaynews : System.Web.UI.UserControl 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     DataList_News.DataSource = MySqlManager.ExecuteSelectCommand("select * from news;"); 
     DataList_News.DataBind(); 
    } 

    public bool DisplaySpace(System.String id) 
    { 
     return false; 
    } 
} 

回答

0

你的方法期望的字符串,那麼發送參數轉換爲字符串

visible = '<%# DisplaySpace(System.Web.UI.DataBinder.Eval(Container.DataItem, "product_id").ToString())%>' 
+0

「>什麼是服務器標籤不正常形成 – Benson

+0

總之服務器標籤沒有正確形成 – Benson

+0

對不起,提出奇怪的問題,但根據http://msdn.microsoft.com/en- us/library/2d76z3ck.aspx它說return system.string – Benson

2

這些被稱爲內嵌的服務器變量,基本上它們用於;

<% %>  -- For embeding a code block 
<%@ %> -- For defining a page directive 
<%# %> -- For data binding 
<%= %> -- For accessing a variable or return value of a method 
<%$ %> -- For accessing AppSettings 
<%-- --%> -- For commenting 

這兩個鏈接都有精確定義/語法的好例子。 Link1Link2

對於第二個問題,我認爲這將是足夠的

visible='<%# DisplaySpace(Eval("product_id")) %>' 
+0

因此,如果我打電話給自己的功能,例如visible =「.... all the code」,我應該使用<%= %>而不是<%# %>? – Benson

+0

最後一點是,爲什麼<%# %>爲什麼<%# %>爲什麼我們不允許放分號在Eval的末尾?我們正在調用一個函數? – Benson

+0

在這種情況下,由於您位於DataList中並且應該使用'#'使用數據綁定語法。「 – Kaf

0

Anwsearing您的問題:

1)在線路服務器標籤用於編寫服務器代碼到標記:

<%= %> - is used to resolve an expression and return its value into the block. 
<%@ %> - attributes used by the ASP.NET page parser and compiler. 
<%$ %> - expression sintax to reuse code in the markup. 
<%# %> - data binding syntax 

2)必須使用周圍的ASPX C#代碼單引號,這就是爲什麼你的錯誤:

'<%# DisplaySpace(System.Web.UI.DataBinder.Eval(Container.DataItem, "product_id")); %>' 
相關問題