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.

Tuesday, February 12, 2013

Generate Asp.Net Server Side Table Dynamically using SharePoint Data



Ascx
<style type="text/css">
.Table
{
    background-color:Aqua;
    width:100%;
    color:Red;
}

.TableHeaderRow
{
    background-color:Aqua;
    color:Red;
    font-size:13px;
    font-family:Calibri;
    font-weight:bold;
    text-align:center;
}

.TableInnerRow
{
    background-color:Aqua;
    color:Green;
    font-size:13px;
    font-family:Calibri;
    text-align:center;
}
</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;

namespace TableObject.TableObject
{
    public partial class TableObjectUserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                getData();
            }
        }

        public void getData()
        {
            Table tblCountries = new Table();
            tblCountries.ID = "tblCountries";
            tblCountries.CssClass = "Table";
            this.Controls.Add(tblCountries);

            TableHeaderRow tHR = new TableHeaderRow();
            tHR.ID = "tHR";
            tblCountries.Controls.Add(tHR);

            TableHeaderCell thRegion = new TableHeaderCell();
            thRegion.ID = "thRegion";
            thRegion.CssClass = "TableHeaderRow";
            thRegion.Text = "Region";
            tHR.Controls.Add(thRegion);

            TableHeaderCell thCountry = new TableHeaderCell();
            thCountry.ID = "thCountry";
            thCountry.Text = "Country";
            thCountry.CssClass = "TableHeaderRow";
            tHR.Controls.Add(thCountry);

            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                SPQuery sQuery = new SPQuery();
                sQuery.Query = "<OrderBy><FieldRef Name=\"Title\" /></OrderBy>";
                SPListItemCollection myColl = SPContext.Current.Web.Lists["lst_Country"].GetItems(sQuery);
                if (myColl.Count > 0)
                {
                    foreach (SPListItem item in myColl)
                    {
                        SPFieldLookupValue value = new SPFieldLookupValue(item["Region"].ToString());
                        TableRow tr=new TableRow();
                        tr.ID = "tr" + item.ID.ToString();
                        tblCountries.Controls.Add(tr);

                        TableCell tcRegion = new TableCell();
                        tcRegion.ID = "tcRegion" + item.ID.ToString();
                        tcRegion.CssClass = "TableInnerRow";
                        tcRegion.Text = value.LookupValue.ToString();
                        tr.Controls.Add(tcRegion);

                        TableCell tcCountry = new TableCell();
                        tcCountry.ID = "tcCountry" + item.ID.ToString();
                        tcCountry.CssClass = "TableInnerRow";
                        tcCountry.Text = item.Title.ToString();
                        tr.Controls.Add(tcCountry);
                    }
                }
            });
        }
    }
}

No comments:

Post a Comment