C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » ASP.NET/Web Applications »

Display Main Categories and Sub Categories


Posted Date: 28 Jul 2004    Resource Type: Articles    Category: ASP.NET/Web Applications
Author: Sweet CousinMember Level: Bronze    
Rating: 1 out of 5Points: 10



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.



Responses

Author: critic    28 Jul 2004Member 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 2004Member Level: Silver   Points : 0
You can use it with links on Pages..
It serves as a meu
WHY NOT!


Author: naijacoder    31 May 2005Member 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


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Response.Redirect is not working?? Check out your form may be posted more than Once !!
Previous Resource: A dozen Asp.net Tips and Tricks
Return to Discussion Resource Index
Post New Resource
Category: ASP.NET/Web Applications


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use