【问题标题】:bind repeater with multi list<>将中继器与多列表<>绑定
【发布时间】:2017-10-04 11:45:29
【问题描述】:

我想用转发器绑定多列表。但是我在绑定时遇到了一些错误。请帮助

此列表作为父子列表。 我收到错误

“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”的属性。

【问题讨论】:

  • Eval("Answer") 试图访问 Answer 类上的 Answer 属性,而该类中没有此类属性,这就是您收到此错误的原因。你需要避免这种情况。你能告诉我们你为什么要访问Answer属性Questionm吗?

标签: c# sql sql-server repeater generic-list


【解决方案1】:

从异常中可以清楚地看出,您正在从数据源列表中访问无效的属性名称。列表对象中不存在属性名称 'Answer'

Questionm 没有“Answer”属性。

编辑:由于您在 Questionm 类中声明了一个列表类型属性,因此您需要遍历该类以访问“Answer”属性。

例子:

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

【讨论】:

  • 我已经列出了这个公共列表 ChildLayers { get;放; } 在 Questionm 类 .so 子层参数应该是可访问的。
猜你喜欢
  • 1970-01-01
  • 2011-01-11
  • 2017-11-03
  • 1970-01-01
  • 2016-08-19
  • 2011-06-28
  • 1970-01-01
  • 2011-04-01
  • 1970-01-01
相关资源
最近更新 更多