2017-10-04 74 views
1

我要綁定多單與repeater.But我得到的一些binding.Please錯誤幫助多表綁定轉發<>

這份名單是父子名單。 我收到錯誤

類型的異常 'System.Web.HttpException' 發生在 System.Web.dll中

但在用戶代碼

其他信息沒有處理: DataBinding:'Questionm'不包含名爲'Answer'的屬性。

**code** 
    **class Name:** 

    public class Questionm 
    { 
     public int QID { get; set; } 
     public string Question { get; set; } 
     public int AnswerType { get; set; } 
     public List<ChildLayers> ChildLayers { get; set; } 
     public Questionm() 
     { 
      ChildLayers = new List<ChildLayers>(); 
     } 

    } 

    public class ChildLayers 
    { 
     public int QuestionID { get; set; } 
     public string Answer { get; set; } 
    } 

**ASPX.cs Code** 

public void daya() 
    { 
     SqlConnection con = new SqlConnection("Data Source=ADMIN-PC;Initial Catalog=Test;Integrated Security=True"); 
     SqlCommand cmd = new SqlCommand(); // 
     cmd.CommandText = "selectdata"; 
     cmd.Connection = con; 
     cmd.CommandType = CommandType.StoredProcedure; 
     con.Open(); 
     SqlDataAdapter da = new SqlDataAdapter(cmd); 
     DataSet ds = new DataSet(); 
     da.Fill(ds); 

     DataTable DT = new DataTable(); 
     DT = ds.Tables["Table"]; 
     DataTable DT1 = new DataTable(); 
     DT1 = ds.Tables["Table1"]; 

     IList<Questionm> data = ConvertDataTable<Questionm>(DT); 
     IList<ChildLayers> cdata = ConvertDataTable<ChildLayers>(DT1); 
     IList<Questionm> hierarcy = new List<Questionm>(); 

     List<Questionm> dsfsadsad = new List<Questionm>(); 
     foreach (var layer in data) 
     { 
     Questionm obj = new Questionm(); 

      obj.QID = layer.QID; 
      obj.Question = layer.Question; 
      obj.AnswerType =layer.AnswerType;  
     var sublayers1 = cdata.Where(i => i.QuestionID == layer.QID && i.QuestionID != 0); 

     foreach (var sublayer in sublayers1) 
     { 
      ChildLayers obj1 = new ChildLayers(); 
      obj1.Answer = sublayer.Answer; 
      obj1.QuestionID = sublayer.QuestionID; 

      obj.ChildLayers.Add(obj1); 

     } 
     hierarcy.Add(obj); 

    } 

     rptparent.DataSource = hierarcy; 
     rptparent.DataBind(); 
} 

**.aspx code** 
<asp:repeater id="rptparent" runat="server"> 
      <HeaderTemplate> 
    <table> 
     <tr> 
       <th>Question</th> 

     </tr> 
    </HeaderTemplate> 
     <ItemTemplate> 
     <tr> 
      <td> 
      <%# ((Questionm)Container.DataItem).QID %> 
      </td> 

      <%# ((Questionm)Container.DataItem).Question %> 

      <asp:RadioButton ID="RadioButton1" runat="server" Text='<%# Eval("Answer") %>' /> 
      <%--Answer Text='<%# Eval("FileName") %>'>--%> 


     </tr> 
     </ItemTemplate> 
     <FooterTemplate>  
     </table><br /> 
    </FooterTemplate> 
    </asp:repeater></td></tr> 

現在我是個收到此錯誤 enter image description here

類型「System.Web.HttpException」的異常出現在System.Web.dll中,但在用戶代碼中

其他信息沒有處理:DataBinding:'Questionm'不包含名爲'Answer'的屬性。

+0

'的eval(「答」)'試圖訪問'Answer'上'Questionm'類屬性上,但該類這就是爲什麼你收到此錯誤沒有這樣的屬性。你需要避免這種情況。你能告訴我們爲什麼你要訪問'Answer'屬性'Questionm'嗎? –

回答

0

從例外情況可以清楚地看到,您正在訪問數據源列表中的無效屬性名稱。屬性名稱'Answer'不存在於您的列表對象中。

問題沒有「應答」屬性。

編輯:正如你已經在Questionm類中聲明瞭一個列表類型屬性,你需要遍歷這個類來訪問'Answer'屬性。

例子:

((Questionm)Container.DataItem).Question.ChildLayers[0].Answer 
+0

我已列出此公開列表 ChildLayers {get;組; }在Questionm class .so中應該可以訪問子圖層參數。 –

+0

分享你的代碼和.aspx代碼 –

+0

都已經分享。 –