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, December 11, 2012

Example to use HTML Tags in Asp.Net GridView or SharePoint GridView


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="VisualWebPart1UserControl.ascx.cs" Inherits="dgvHTMLBinding.VisualWebPart1.VisualWebPart1UserControl" %>
<style type="text/css">
.HeaderStyle
{
background-color:Navy;
color:white;
}
.ItemTemplateStyle
{
background-color:Orange;
color:white;
padding-left:5px;
/*text-align:Center;*/
}
.AlternativeTemplateStyle
{
background-color:green;
color:white;
padding-left:5px;
/*text-align:Center;*/
}
.BlankTr
{
    background-color:Red;
}
</style>
<asp:GridView ID="dgvBreakingNews" runat="server" AutoGenerateColumns="false" Width="100%" CellPadding="0" CellSpacing="0">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<div class="BlankTr" style="width:100%;height:2px;">&nbsp;</div>
<div style="width:100%;text-align:center;" class="HeaderStyle">
Breaking News
</div>
<div class="BlankTr" style="width:100%;height:2px;">&nbsp;</div>
</HeaderTemplate>
<ItemTemplate>
<div style="width:100%;" class="ItemTemplateStyle">
<span>
<%# Eval("ID") %>.
</span>
<span>
<strong><%# Eval("Title") %></strong>
</span>
</div>
<div class="BlankTr" style="width:100%;height:2px;">&nbsp;</div>
</ItemTemplate>
<AlternatingItemTemplate>
<div style="width:100%;" class="AlternativeTemplateStyle">
<span>
<%# Eval("ID") %>.
</span>
<span>
<strong><%# Eval("Title") %></strong>
</span>
</div>
<div class="BlankTr" style="width:100%;height:2px;">&nbsp;</div>
</AlternatingItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

Ascx.cs
if (!IsPostBack)
            {
                SPWeb currentWeb = SPContext.Current.Web;
                SPList lst = currentWeb.Lists["Breaking News"];
                SPQuery sQuery = new SPQuery();
                sQuery.Query = "<OrderBy><FieldRef Name='ID' Ascending='False' /></OrderBy>";
                SPListItemCollection myColl = lst.GetItems(sQuery);
                dgvBreakingNews.DataSource = myColl.GetDataTable();
                dgvBreakingNews.DataBind();
            }


No comments:

Post a Comment