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, November 07, 2012

Using SpGridView and SpMenuField within an UserControl


Using Grouping and SpMenuField in SpGridView
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="GroupByUserControl.ascx.cs" Inherits="SpDemo.GroupBy.GroupByUserControl" %>
<script type="text/javascript">
    function ShowCollapsed() {
        var rows = document.getElementsByTagName('tr');
        var numRows = rows.length;
        for (var i = 0; i < numRows; i++) {
            if (rows[i].getAttribute("isexp") != null && rows[i].getAttribute("isexp").toLowerCase() == "true") {
                if (rows[i].firstChild.firstChild != null && rows[i].firstChild.firstChild.tagName.toLowerCase() == "a") {
                    if (rows[i].firstChild.firstChild.title.toLowerCase() != "expand/collapse") {
                        rows[i].firstChild.firstChild.fireEvent("onclick");
                    }
                }
            }

        }
    }
    _spBodyOnLoadFunctionNames.push("ShowCollapsed");
</script>

<SharePoint:SPGridView ID="sgvGroupBy" runat="server" AutoGenerateColumns="false" EnableViewState="false" DataKeyNames="ID">
    <AlternatingRowStyle CssClass="ms-alternating" />
    <Columns>
    </Columns>
    <SelectedRowStyle CssClass="ms-selectednav" Font-Bold="True" />
</SharePoint:SPGridView>
<SharePoint:MenuTemplate runat="server" ID="SampleMenuTemplate">
<SharePoint:MenuItemTemplate ID="ViewMenuTemplate"  runat="server" Text="View"   />
<SharePoint:MenuItemTemplate ID="EditMenuTemplate"  runat="server" Text="Edit" ImageUrl="/_layouts/Images/edititem.gif"    />
</SharePoint:MenuTemplate>
<asp:Label ID="lblMessage" runat="server"></asp:Label>


Ascx.cs
SPMenuField sField = new SPMenuField();
                sField.HeaderText = "Task Name";
                sField.NavigateUrlFields = "ID";
                sField.NavigateUrlFormat=SPContext.Current.Web.Url + "/Lists/Tasks/DispForm.aspx?ID={0}&Source=" + Page.Request.Url;
                sField.TextFields = "Title";
                sField.MenuTemplateId = "SampleMenuTemplate";
                sField.TokenNameAndValueFields = "ItemID=ID";
                sgvGroupBy.Columns.Add(sField);
                ViewMenuTemplate.ClientOnClickNavigateUrl = SPContext.Current.Web.Url + "/Lists/Tasks/DispForm.aspx?ID=%ItemID%&Source=" + Page.Request.Url;
                EditMenuTemplate.ClientOnClickNavigateUrl = SPContext.Current.Web.Url + "/Lists/Tasks/EditForm.aspx?ID=%ItemID%&Source=" + Page.Request.Url;
                BoundField bPriorty = new BoundField();
                bPriorty.HeaderText = "Priority";
                bPriorty.DataField = "Priority";
                sgvGroupBy.Columns.Add(bPriorty);
                sgvGroupBy.AllowGrouping = true;
                sgvGroupBy.AllowGroupCollapse = true;
                sgvGroupBy.GroupFieldDisplayName = "Status";
                sgvGroupBy.GroupField = "Status";
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    SPWeb objWeb = SPContext.Current.Web;
                    SPList lst = objWeb.Lists["Tasks"];
                    SPQuery sQuery = new SPQuery();
                    sQuery.Query = "<OrderBy><FieldRef Name='Status' Ascending='True' /></OrderBy>";
                    sQuery.ViewFields = "<FieldRef Name='Status'  /><FieldRef Name='Title'  /><FieldRef Name='Priority'  /><FieldRef Name='EncodedAbsUrl'  />";
                    SPListItemCollection myColl = lst.GetItems(sQuery);
                    if (myColl.Count > 0)
                    {
                        sgvGroupBy.DataSource = myColl.GetDataTable();
                        sgvGroupBy.DataBind();
                    }

                });


No comments:

Post a Comment