Prasad Bolla's SharePoint Blog

Click Here to go through the Interesting posts within my Blog.

Click Here to go through the new posts in my blog.

Monday, February 04, 2013

Binding Data to Ajax Acordin Control from SharePoint List Fetching Countries Based on Region



Note:-
1.       The Parent List Used Here is Region.
2.       The Child List which using Region as Lookup is lst_Country.
We are fetching and binding countries based on Region to Ajax Accordin Control.
Ascx
<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="AccordinUserControl.ascx.cs" Inherits="Accordin.Accordin.AccordinUserControl" %>
<%@ Register Assembly="AjaxControlToolkit, Version=3.0.30930.28736, Culture=neutral, PublicKeyToken=28f01b0e84b6d53e" Namespace="AjaxControlToolkit" TagPrefix="AjaxControlToolkit" %>
<style type="text/css">
 .accordionContent {
background-color: #D3DEEF;
border-color: -moz-use-text-color #2F4F4F #2F4F4F;
border-right: 1px dashed #2F4F4F;
border-style: none dashed dashed;
border-width: medium 1px 1px;
padding: 10px 5px 5px;
width:20%;
color:Red;
font-weight:bold;
}
.accordionHeaderSelected {
background-color: #5078B3;
border: 1px solid #2F4F4F;
color: white;
cursor: pointer;
font-family: Arial,Sans-Serif;
font-size: 12px;
font-weight: bold;
margin-top: 5px;
padding: 5px;
width:20%;
}
.accordionHeader {
background-color: #2E4D7B;
border: 1px solid #2F4F4F;
color: white;
cursor: pointer;
font-family: Arial,Sans-Serif;
font-size: 12px;
font-weight: bold;
margin-top: 5px;
padding: 5px;
width:20%;
}
.href
{
color:White;
font-weight:bold;
text-decoration:none;
}
</style>
Ascx.cs
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using System.Security;
using AjaxControlToolkit.Design;

namespace Accordin.Accordin
{
    public partial class AccordinUserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                BindData();
            }
            catch (Exception Ex)
            {
                Response.Write(Ex.ToString());
            }
        }
        string strCountry = "";
        public string getCountry(int regionItemID)
        {
            SPWeb currentWeb = SPContext.Current.Web;
            SPList lst = currentWeb.Lists["lst_Country"];
            SPQuery sQuery = new SPQuery();
            sQuery.Query = "<Where><Eq><FieldRef Name='Region' LookupId='TRUE' /><Value Type='Lookup'>" + regionItemID + "</Value></Eq></Where>";
            SPListItemCollection myColl = lst.GetItems(sQuery);
            strCountry = "";
            strCountry += "<Table>";
            foreach (SPListItem item in myColl)
            {
                strCountry += "<Tr>";
                strCountry += "<Td>";
                strCountry += item.Title.ToString();
                strCountry += "</Td>";
                strCountry += "</Tr>";
            }
            strCountry += "</Table>";
            return strCountry;
        }
        public void BindData()
        {
            SPWeb currentWeb = SPContext.Current.Web;
            SPList lstRegion = currentWeb.Lists["Region"];
            SPQuery sQuery = new SPQuery();
            sQuery.Query = "";
            SPListItemCollection RegionColl = lstRegion.GetItems(sQuery) ;
            AjaxControlToolkit.AccordionPane pn=new AjaxControlToolkit.AccordionPane();;
            AjaxControlToolkit.Accordion obj = new AjaxControlToolkit.Accordion();
            obj.ID = "obj";
            obj.SelectedIndex = 0;
            this.Controls.Add(obj);
            obj.HeaderCssClass = "accordionHeader";
            obj.HeaderSelectedCssClass = "accordionHeaderSelected";
            obj.ContentCssClass = "accordionContent";
            obj.AutoSize = (AjaxControlToolkit.AutoSize)1;
            this.Controls.Add(obj);
            if (RegionColl.Count > 0)
            {
                int i = 0;
                Label lblRegion;
                Label lblCountry;
                foreach (SPListItem regionItem in RegionColl)
                {
                    lblRegion = new Label();
                    lblCountry = new Label();
                    lblRegion.Text = regionItem.Title.ToString();
                    pn = new AjaxControlToolkit.AccordionPane();
                    pn.ID = "Pane" + regionItem.ID.ToString();
                    pn.HeaderContainer.Controls.Add(lblRegion);
                    int regionID = int.Parse(regionItem.ID.ToString());
                    getCountry(regionID);
                    lblCountry.Text = strCountry;
                    pn.ContentContainer.Controls.Add(lblCountry);
                    pn.ContentContainer.Height = 200;
                    obj.Panes.Add(pn);
                    i++;
                    if (i >= RegionColl.Count)
                    {
                        break;
                    }
                }
            }
           
        }
    }
}

No comments:

Post a Comment