In short we can term it as "Grouping of Information". The code given here is used to display both the master record and the child records together. For example http://www.knowledgestorm.com. Here you can see that they have displayed the main categories in their directory and at the same time the subcategories for that main category.
This scenario also arises in the case of forums too.. where you hold discussions on some topics based on some categories. This scenario arises whereever "Grouping of Information" is required.
Master Record 1 child record 1.1 child record 1.2 etc Master Record 2 child record 2.1 child record 2.2 etc
//ASPX File
<form id="WebForm3" method="post" runat="server"> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <asp:Repeater ID="repMainCat" runat="server"> <ItemTemplate> <tr> <td colspan="2"> <b><u> <%# DataBinder.Eval(Container,"DataItem.MCatName") %> </u></b> </td> </tr> <asp:Repeater ID="repSubCat" DataSource='<%# (GetRecords(((Int32)(DataBinder.Eval(Container.DataItem, "MCatID")))))%>' runat="server"> <ItemTemplate> <tr valign="top"> <td> <a href="#"> <%# DataBinder.Eval(Container.DataItem, "SCatName") %> </a> </td> </tr> </ItemTemplate> </asp:Repeater> </ItemTemplate> </asp:Repeater> </table> </form>
//ASPX.CS File
protected System.Web.UI.WebControls.Repeater repMainCat; protected System.Web.UI.WebControls.Repeater repSubCat;
private void Page_Load(object sender, System.EventArgs e)
{ // Put user code to initialize the page here repMainCat.DataSource = GetRecords(0); repMainCat.DataBind(); }
public OleDbDataReader GetRecords(int iID) { //iId=0 MainCategory records. //iId other values for SubCategory records. string CommandText = "";
if(iId==0) { CommandText = "SELECT * FROM MCatTable"; } else { CommandText = "SELECT * FROM SCatTable WHERE (MCatID = " + iID + ")"; } OleDbConnection myConnection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\db1.mdb; Persist Security Info=False"); OleDbCommand myCommand = new OleDbCommand(CommandText, myConnection);
myConnection.Open(); OleDbDataReader result = myCommand.ExecuteReader(); // Return the datareader result return result; }
I came across many guys facing this issue.. That tempted me to write this article. Anyways this is just the one method of getting the result. There are many ways even.
|
| Author: critic 28 Jul 2004 | Member Level: Bronze Points : 0 |
I am trying to figure out what is this article is all about. The following explanation doesn't make any sense:
Introduction Main Category 1 Sub category 1.1 Sub category 1.2 etc Main Category 2 Sub category 2.1 Sub category 2.2 etc
If you can give a very detailed explanation about what you mean by this "displaying category/sub category", where it is usefull, what kind of scenario etc, that would be helpfull.
|
| Author: naijacoder 02 Sep 2004 | Member Level: Silver Points : 0 |
You can use it with links on Pages.. It serves as a meu WHY NOT!
|
| Author: naijacoder 31 May 2005 | Member Level: Silver Points : 0 |
I really enjoyed the article but can you please give an idea on how the Daybase looks like it would be good to know and that will complete the article Thanks
|