2014-03-31 29 views
0

我有此listViewtextBoxASP.NET運行時錯誤:碼塊未在該支持上下文與ListView的

 <table> 
     <tr><td>Reciver:<table><tr> 
     <asp:ListView ID="showRecivers" runat="server"><td><%# Eval("name")%></td>  </asp:ListView> 
     </tr></table> 
     <asp:TextBox ID="reciver" runat="server" OnTextChanged="style_Recivers" AutoPostBack="true"></asp:TextBox> 
     </td></tr></table> 

list所述的listview勢必:

public List<Reciver> recivers = new List<Reciver>(); 

和功能style_Recivers

protected void style_Recivers(object sender, EventArgs e) 
{ 
    string[] separator = new string[] { "," }; 
    string[] reciversArray = reciver.Text.ToString().Split(separator, StringSplitOptions.None); 
    reciversArray = reciversArray.Distinct().ToArray(); 
    for (int i = 0; i < reciversArray.Length; i++) 
    { 
     recivers.Add(new Reciver(reciversArray[i])); 
    } 
    this.showRecivers.DataSource = recivers; 
    this.showRecivers.DataBind(); 
} 

和類Reciver

public class Reciver 
{ 
    public string name; 
    public Reciver(string name) 
    { 
     this.name = name; 
    } 
    public string getName() 
    { 
     return this.name; 
    } 
    public void setName(string name) 
    { 
     this.name = name; 
    } 
} 

我的想法是什麼,當這幾個名字eneted到TextBox與 saperator的style_Reciver功能被激活,每名在ListView立即顯示。

,但它不工作,它給我的錯誤

ASP.NET runtime error:code blocks are not supported in this context

,標誌着該行:
<asp:ListView ID="showRecivers" runat="server"><td><%# Eval("name")%></td> </asp:ListView>

的首發。可能更多的東西不會工作,但這是第一件事。

我該如何解決它?感謝您的幫助

編輯: 它的作品,我增加<ItemTemplate> 之後,現在它給了我一個不同的錯誤:

Reciver' does not contain a property with the name 'name' 

whhat現在是什麼問題?

回答

1

列表視圖內容這裏應該裹進ItemTemplate

<asp:ListView ID="showRecivers" runat="server"> 
    <ItemTemplate> 
     <td><%# Eval("name")%></td> 
    </ItemTemplate> 
</asp:ListView> 

更新。您的類聲明也存在問題。這裏是它應該如何用C#傳統的方式聲明:

public class Reciver 
{ 
    public string _name; 
    public Reciver(string name) 
    { 
     this.name = name; 
    } 

    public string name 
    { 
     get { return this._name; } 
     set { this._name = value; } 
    } 
} 
+0

ohhh我不能相信我忘了那個。非常感謝你!別的,現在它給了我錯誤:**'Reciver'不包含名稱爲'name'**的屬性。你能幫助我嗎? – Cooper

+0

@Cooper,錯誤讀取本身 - 沒有這樣的屬性。你可以刪除你的getter和setter,並用屬性'name'替換它們。 – Andrei

+0

我雖然這樣,它很好,改變它,它的工作原理。謝謝!我會在4分鐘內接受你的回答 – Cooper

相關問題