我想用repeater绑定多个列表。但是我在绑定时遇到了一些错误。请帮助
此列表作为父子列表。 我收到错误
发生了'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>
System.Web.dll中发生了'System.Web.HttpException'类型的异常,但未在用户代码中处理
其他信息:DataBinding:'Questionm'不包含名为'Answer'的属性。
答案 0 :(得分:0)
从例外情况可以清楚地看出,您正在从数据源列表中访问无效的属性名称。列表对象中不存在属性名'Answer'
。
问题没有“答案”属性。
编辑:正如您在Questionm类中声明了一个列表类型属性,您需要遍历该类以访问“Answer”属性。
示例:
((Questionm)Container.DataItem).Question.ChildLayers[0].Answer