Bind data repeater inside data repeater in ASP.NET -
i'm learning .net @ moment, forgive me if silly question.
i have 2 mssql tables, 1 called 'comment' , other called 'commentadditionalauthor'. comment table holds data comment form, such comment title, date, main author name etc. commentadditionalauthor table contains information other authors contributed comment. there one-to-many relationship there - each comment can have 1 or more additional authors.
i have repeater control binds data comment table, works nicely. want though output author details alongside comment. repeater within repeater.
i've tried clean code as possible sorry it's still pretty lengthy i'd keep relevant i'm doing can understand solution. having found other examples of this, i'm struggling implement them bind data in different ways, can't make work in application.
the code in aspx file this:
<div id="comments"> <asp:repeater id="rptcomments" runat="server"> <itemtemplate> <div class="comment-data"> <h3 class="item">submitted <%# getpagedetails(eval("nodeid")) %> article on <%# eval("created") %></strong></h3> <p class="item"><strong>name</strong> <%# eval("firstname") %> <%# eval("surname") %></p> <p class="item"><strong>occupation</strong> <%# eval("occupation") %></p> <p class="item"><strong>affiliation</strong> <%# eval("affiliation") %></p> <p class="item"><strong>email</strong> <a href='mailto:<%# eval("email") %>'><%# eval("email") %></a> <em>publish email: <%# eval("publishemail") %></em></p> <p class="item"><strong>competing interests?</strong> <%# eval("competingintereststext") %> </p> <p class="item"><strong>eletter title</strong> <%# eval("title") %></p> <p><%# eval("comment").tostring().replace("\n", "<br/>")%></p> <!-- want do, can't bind: --> <div class="additional-authors"> <h3>additional authors</h3> <asp:repeater id="rptadditionalauthors" runat="server"> <itemtemplate> <%# eval("firstnameother")%><br> <%# eval("surnameother")%><br> <%# eval("occupationother")%><br> <%# eval("affiliationother")%><br> <%# eval("emailother")%><br> </itemtemplate> </asp:repeater> </div> </div> </itemtemplate> </asp:repeater> and here's code-behind doing:
private void binddata() { var rr = _sqlhelper.executereader(string.format("select * comment {0} order created desc", filter)); var commentdt = new datatable("comments"); commentdt.columns.add("id", type.gettype("system.int32")); commentdt.columns.add("nodeid", type.gettype("system.int32")); commentdt.columns.add("firstname", type.gettype("system.string")); commentdt.columns.add("surname", type.gettype("system.string")); commentdt.columns.add("occupation", type.gettype("system.string")); commentdt.columns.add("affiliation", type.gettype("system.string")); commentdt.columns.add("title", type.gettype("system.string")); commentdt.columns.add("email", type.gettype("system.string")); commentdt.columns.add("publishemail", type.gettype("system.boolean")); commentdt.columns.add("competinginterests", type.gettype("system.boolean")); commentdt.columns.add("competingintereststext", type.gettype("system.string")); commentdt.columns.add("comment", type.gettype("system.string")); commentdt.columns.add("statusid", type.gettype("system.int32")); commentdt.columns.add("spam", type.gettype("system.boolean")); commentdt.columns.add("ham", type.gettype("system.boolean")); commentdt.columns.add("created",type.gettype("system.datetime")); while (rr.read()) { var dr = commentdt.newrow(); dr["id"] = rr.getint("id"); dr["nodeid"] = rr.getint("nodeid"); dr["firstname"] = rr.getstring("firstname"); dr["surname"] = rr.getstring("surname"); dr["occupation"] = rr.getstring("occupation"); dr["affiliation"] = rr.getstring("affiliation"); dr["title"] = rr.getstring("title"); dr["email"] = rr.getstring("email"); dr["publishemail"] = rr.isnull("publishemail") == true ? false : rr.getboolean("publishemail"); dr["competinginterests"] = rr.isnull("competinginterests") == true ? false : rr.getboolean("competinginterests"); dr["competingintereststext"] = rr.getstring("competingintereststext"); dr["comment"] = rr.getstring("comment"); dr["statusid"] = rr.getint("statusid"); dr["spam"] = rr.isnull("spam") == true ? false : rr.getboolean("spam"); dr["ham"] = rr.isnull("ham") == true ? false : rr.getboolean("ham"); dr["created"] = rr.getdatetime("created"); commentdt.rows.add(dr); /* failing attempt @ second bind: var rrauthor = _sqlhelper.executereader(string.format("select * commentotherauthor commentid = 81")); var otherauthordt = new datatable("otherauthors"); otherauthordt.columns.add("firstname", type.gettype("system.string")); otherauthordt.columns.add("surname", type.gettype("system.string")); otherauthordt.columns.add("occupation", type.gettype("system.string")); otherauthordt.columns.add("affiliation", type.gettype("system.string")); otherauthordt.columns.add("email", type.gettype("system.string")); while (rrauthor.read()) { var drauthor = otherauthordt.newrow(); drauthor["firstnameother"] = rr.getstring("firstname"); drauthor["surnameother"] = rr.getstring("surname"); drauthor["occupationother"] = rr.getstring("occupation"); drauthor["affiliationother"] = rr.getstring("affiliation"); drauthor["emailother"] = rr.getstring("email"); otherauthordt.rows.add(drauthor); } rptadditionalauthors.databind(); */ } var pgitems = new pageddatasource { datasource = commentdt.defaultview, allowpaging = true, pagesize = 25, currentpageindex = currentpage }; rptcomments.datasource = pgitems; rptcomments.databind(); if (pgitems.pagecount > 1) { var pages = new arraylist(); (var = 0; < pgitems.pagecount; i++) pages.add((i + 1).tostring()); rptpages.datasource = pages; rptpages.databind(); rptpages.visible = true; } else { rptpages.visible = false; } } the code-behind stuff doesn't work because of second repeater i've set up, first repeater work if remove stuff. thought best keep in there can see 'logic' (probably not logical ;) ). worth noting in second data set i've hard-coded additional author details id 81 - repeater working - need substitute id value comment table, authors current comment in loop.
if can me second repeater working i'd grateful. it's on edge of learning curve @ moment!
thanks folks!
there several articles available online on nested repeaters, including this one.
in essence, need first create dataset , define relationships between datatables in code-behind, , bind child repeater child rows. article linked above has full working example that's prety easy modify.
if article doesn't suit needs, try one of these.
Comments
Post a Comment