Sunday, June 29, 2008

Accordion AJAX Control Runtime Data display from Database

Accordion AJAX Control Example to Create AccordionPane Runtime with Data from Database.

Accordion Control is one of the best AJAX Control as we can use in many different ways to serve our needs. Please refer to this video if you are new to Accordion AJAX Control.

Many time we generate Menu Runtime for that we make use of Treeview Control, but with this example you will understand how Accordion AJAX Control is better than other solution. In this example I have fetch the Country wise data from Northwind Customer Table and display Customer Information CountryWise.

Example 1: Displaying All the customer group by country, belgium



Example 2: Displaying All the customer group by country, brazil


Note: here the content portion is not showing actual customer Id, but you can display easily using the same way i have used to display countries and attach a querystring to display customer information. This piece of code is reusable.

On .aspx page, declare Accordion control.

<asp:Panel ID="pnlShowCountry" runat="server">
<cc1:Accordion ID="acc" runat="server" SelectedIndex="0" AutoSize="none" FadeTransitions="false" FramesPerSecond="50" TransitionDuration="10" HeaderCssClass="AccordionPaneHeaderCSS" ContentCssClass="AccordionPaneContentCSS">
</cc1:Accordion>
</asp:Panel>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>

On .aspx.cs page (code behind) actual logic to create accordianpane runtime and binding logic.
protected void Page_Load(object sender, EventArgs e)
{
PopulateCountry();
}

private void PopulateCountry()
{
DataTable dtCountry = GetCountry();
DataRow drCountry;
string CountryName, CountryCount;

for(int i=0;i< dtCountry.Rows.Count;i++)
{
drCountry = dtCountry.Rows[i];
CountryName = drCountry["CountryName"].ToString();
CountryCount = drCountry["CountryCount"].ToString();

Label lblHeader = new Label();
lblHeader.ID = "lblHeader" + i.ToString();
lblHeader.Text = "<a href='' onclick='return false;' Class='AccordionLink'>" + CountryName + "</a> (" + CountryCount + ")";

//For Content
int iCountryCount = Convert.ToInt16(drCountry["CountryCount"]);
Label lblContent = new Label();
lblContent.ID = "lblContent" + i.ToString();
for (int j = 1; j <= iCountryCount; j++)
{
if(j==1)
lblContent.Text = "<a href='' onclick='return false;' Class='AccordionLink'>Customer " + j.ToString() + "</a><br>Blah Blah Blah....<br>Blah Blah Blah....Blah Blah Blah....";
else
lblContent.Text = lblContent.Text + "<br><a href='' onclick='return false;' Class='AccordionLink'>Customer " + j.ToString() + "</a><br>Blah Blah Blah....<br>Blah Blah Blah....Blah Blah Blah....";
}

AccordionPane accp = new AccordionPane();
accp.ID = "accp" + i.ToString();
accp.HeaderContainer.Controls.Add(lblHeader);
accp.ContentContainer.Controls.Add(lblContent);

acc.Panes.Add(accp);
}
pnlShowCountry.Controls.Add(acc);
}

private DataTable GetCountry()
{
string strConn = ConfigurationManager.AppSettings["connStr"].ToString();
SqlConnection conn = new SqlConnection(strConn);
conn.Open();
SqlDataAdapter da = new SqlDataAdapter("select Country as 'CountryName', count(customerId) as 'CountryCount' from customers group by country",conn);
DataSet ds = new DataSet();
da.Fill(ds,"Country");
DataTable dtCountry = ds.Tables["Country"];
return dtCountry;
}

5 comments:

DotNetGuts said...

I have not polished the code (running out of time), but if you try to understand it, it is very easy and reusable with little modification.

webeye said...

Thanks.....This help me a lot...

Anonymous said...

wht a artcile.....gr88888 man...plzz keep it up..it helped me a lot to solve one important issue

Raj said...

it really helped me a lot...thanx to writer..keep it up mate

Anonymous said...

Hi all! i am making an auction site(asp.net, c#) in which i declared some categories in header of the accordion pane and sub categories in content.But i wanna do same thing dynamically and i want to take values from Categories and Subcategories table of Ms SQl 2005 database.So that if admin adds or deletes the categories the site should be updated. please help me regarding this.Any example link or source code will be appreciated. Thanks in advance.

Most Recent Post

Community Updates

Subscribe Blog via Email

Enter your email address:



Disclaimers:We have tried hard to provide accurate information, as a user, you agree that you bear sole responsibility for your own decisions to use any programs, documents, source code, tips, articles or any other information provided on this Blog.
Page copy protected against web site content infringement by Copyscape