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.

Wednesday, March 27, 2013

How to show New Image Next to Hyperlink for a Newly Created SharePoint List Item within a 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="HyperlinkNewImageUserControl.ascx.cs" Inherits="HyperlinkNewImage.HyperlinkNewImage.HyperlinkNewImageUserControl" %>
<asp:GridView ID="dgvHyperlinkNewImage" runat="server"
    AutoGenerateColumns="False" CellPadding="4" EnableModelValidation="True"
    ForeColor="#333333" GridLines="Both" Width="100%" DataKeyNames="ItemID"
    onrowdatabound="dgvHyperlinkNewImage_RowDataBound">
    <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:TemplateField HeaderText="New Thought">
<ItemTemplate>
<asp:Panel ID="pnlNewImage" runat="server" Visible="false"><a href=<%# Eval("ItemURL").ToString() %>><%# Eval("ItemTitle").ToString() %></a>&nbsp;<img src="http://i5z1rq.bay.livefilestore.com/y1pt0XbZdz1DUmR9_DD7U07Hlzmj6O-Ll8oRJAfA4s3jjcHiXw4qaERRMv37IVB97cE2JofYooCer5XPzkkREMe88yVA51AmX7a/new.gif" /></asp:Panel>
<asp:Panel ID="pnlOldImage" runat="server"><a href=<%# Eval("ItemURL").ToString() %>><%# Eval("ItemTitle").ToString() %></a></asp:Panel>
</ItemTemplate>
</asp:TemplateField>
</Columns>
    <EditRowStyle BackColor="#999999" />
    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
    <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
    <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
</asp:GridView>
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 System.Linq;

namespace HyperlinkNewImage.HyperlinkNewImage
{
    public partial class HyperlinkNewImageUserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                getData();
            }
            catch (Exception Ex)
            {
                Response.Write(Ex.ToString());
            }
        }

        public void getData()
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                string ItemURL, ItemTitle,ItemID = string.Empty;
                var strQuery = from SPListItem item in SPContext.Current.Web.Lists["New Ideas"].Items
                               orderby item.ID.ToString() descending
                               select new
                               {
                                   ItemURL=SPContext.Current.Web.Url+"/Lists/New Ideas/DispForm.aspx?ID="+item.ID.ToString()+"&Source="+Page.Request.Url,
                                   ItemTitle=item.Title.ToString(),
                                   ItemID=item.ID.ToString()
                               };
                dgvHyperlinkNewImage.DataSource = strQuery;
                dgvHyperlinkNewImage.DataBind();
            });
        }

        protected void dgvHyperlinkNewImage_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    SPWeb currentWeb = SPContext.Current.Web;
                    SPList lst = currentWeb.Lists["New Ideas"];
                    SPQuery sQuery = new SPQuery();
                    sQuery.Query = "<Where><Eq><FieldRef Name=\"ID\" /><Value Type=\"Counter\">" + dgvHyperlinkNewImage.DataKeys[e.Row.RowIndex].Value.ToString() + "</Value></Eq></Where>";
                    SPListItemCollection myColl = lst.GetItems(sQuery);
                    if (myColl.Count > 0)
                    {
                        SPListItem item = myColl[0];
                        DateTime dtCreatedDate = Convert.ToDateTime(item["Created"].ToString());
                        DateTime dtConvertedDateTime = Convert.ToDateTime(dtCreatedDate.ToShortDateString());
                        DateTime dtToday = Convert.ToDateTime(DateTime.Now);
                        DateTime dtConvertedToday = Convert.ToDateTime(dtToday.ToShortDateString());
                        Panel pNew = (Panel)e.Row.Cells[0].FindControl("pnlNewImage");
                        Panel pOld = (Panel)e.Row.Cells[0].FindControl("pnlOldImage");
                        if (dtConvertedDateTime == dtConvertedToday)
                        {
                            pNew.Visible = true;
                            pOld.Visible = false;
                        }
                        else
                        {
                            pNew.Visible = false;
                            pOld.Visible = true;
                        }
                    }
                }
            }
            catch (Exception Ex)
            {
                Response.Write(Ex.ToString());
            }
        }
    }
}

No comments:

Post a Comment