c# - Ordering child pages underneath parent pages -
i trying create database driven menu unsure how arrange child pages underneath parent pages way have implemented code.
sql call:
/// <summary> /// of pages on website /// </summary> /// <returns></returns> public static datatable getallwebsitepages() { datatable returnval = new datatable(); using (sqlconnection sqlcon = new sqlconnection(configurationmanager.connectionstrings["websitecontent"].connectionstring)) { sqlcon.open(); string sql = "select * website_pages"; using (sqlcommand sqlcomm = new sqlcommand(sql, sqlcon)) { using (sqldataadapter dataadapter = new sqldataadapter(sqlcomm)) { dataadapter.fill(returnval); } } sqlcon.close(); } return returnval; } c#:
private void refreshpages() { lvpages.datasource = cms.websitepages.pages.getallwebsitepages(); lvpages.databind(); } asp.net:
<itemtemplate> <tr> <td> <asp:label runat="server" id="lbltitle" cssclass="cmstitle"><%#eval("website_page_name")%></asp:label> </td> <td> <asp:imagebutton runat="server" id="btnedit" commandargument='<%#eval("website_page_id")%>' commandname="editpage" imageurl="../../images/ntu-cms-edit.png" /> <asp:imagebutton runat="server" id="btndelete" commandargument='<%#eval("website_page_id")%>' commandname="deletepage" onclientclick="return confirm('do want delete page?');" imageurl="../../images/ntu-cms-delete.png" /> </td> </tr> </itemtemplate> in database, of pages have id , child pages have parent id. e.g our history has parent page of 2, us.
there old technique grouped data - first thing sql output this...
parent child ---------- ---------- home home products products products widget products midget products fidget us history contact then loop through , consume each row build menu... following pseudo-code...
string currentgroup = ""; menuitem currentparentmenuitem = null; foreach(dbrow r in results) { if (currentgroup != r.group) { //create new group currentgroup = r.group; //add group "parent" item //store parent in menuitem can add children next time through currentparentmenuitem = newparentwecreatedabove; } else { //add current row child current parent menu group currentparentmenuitem.addchild(r.child); } } basically is: every time group name changes, create new group , add children group until group name changes again.
Comments
Post a Comment