Thursday 17 May 2012

Problem Sorting With Multiple SPGridViews

I have experienced a funny behavior by having two SPGridviews along with ObjectDataSource on the same page. Both the SPGirdViews are enabled with sorting, filtering and pagination. Filtering and Pagination worked perfectly without any issues. If it comes to sorting point of view, whenever you try to sort the lower SPGridView it gets applied to upper SPGridView.

The Hack for this issue:
              protected override void OnPreRender(EventArgs e)
        {           
            if (SPGridView1.HeaderRow != null)
            {
                foreach (WebControl control in this.SPGridView1.HeaderRow.Controls)
                {
                    UpdateTemplateClientID(control, SPGridView1.ClientID);
                }
            }
            if (SPGridView2.HeaderRow != null)
            {
                foreach (WebControl control in this.SPGridView2.HeaderRow.Controls)
                {
                    UpdateTemplateClientID(control, SPGridView2.ClientID);
                }
            }
            base.OnPreRender(e);
        }

        private void UpdateTemplateClientID(Control control, string clientID)
        {
            if (control is Microsoft.SharePoint.WebControls.Menu)
            {
                Microsoft.SharePoint.WebControls.Menu menuControl = control as Microsoft.SharePoint.WebControls.Menu;
                string jsFunctionCall = menuControl.ClientOnClickPreMenuOpen;
                menuControl.ClientOnClickPreMenuOpen = jsFunctionCall.Replace("%TEMPLATECLIENTID%", clientID + "_SPGridViewFilterMenuTemplate");
            }
            else if (control.HasControls())
            {
                foreach (WebControl c in control.Controls)
                {
                    UpdateTemplateClientID(c, clientID);
                }
            }
        }

Tuesday 15 May 2012

Sample Examples of ShowModalDialog

For showing the div:
<script type="text/javascript">
function showDialog() {
var _html = document.createElement('div');
_html.innerHTML = document.getElementById('WfHistory').innerHTML;
var _options = { html: _html , title: "View History",
allowMaximize: false,
showClose: true,
autoSize: true};
SP.UI.ModalDialog.showModalDialog(_options);}
</script>
<asp:LinkButton id="hlViewHistory" runat="server" OnClientClick="showDialog();return false;" Text="Show Full History" />
<div id="WfHistory" style="display:none" >
....
</div>

For showing the aspx page:
<script type="text/javascript">
function showDialog() {
var list = document.getElementById("<%= hdnParamList.ClientID %>").value;
var _options = { title: "View History",
allowMaximize: false,
showClose: true,
autoSize: true,
url: "ViewHistory.aspx?List=" + list };
SP.UI.ModalDialog.showModalDialog(_options);}
</script>
<asp:LinkButton id="lbViewHistory" runat="server" OnClientClick="showDialog();return false;" Text="Show Full History" />script>div id="WfHistory" style="display:none" >

Close Popup inside CodeBehind:
StringBuilder sbScript1 = new StringBuilder();
sbScript1.Append("<script language='javascript'>");
sbScript1.Append("window.frameElement.commitPopup();<");
sbScript1.Append("/script>");
ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "closepopup", sbScript1.ToString(), false);

Thursday 3 May 2012

Reusable Code Snippet to disable EventFiring

For disabling the events inside the extended class of SPItemEventReceiver  then we will have a property named EventFiringEnabled. But if our class doesn’t extend SPItemEventReceiver  then we can use the object of below class.

public class SPItemEventReceiverHandling : SPItemEventReceiver, IDisposable
    {
        public SPItemEventReceiverHandling(bool disableImmediately)
        {
            EventFiringEnabled = !disableImmediately;
        }       

        public void Dispose()
        {
            EventFiringEnabled = true;
        }
    }

Example: item represents SPListItem

using (var eventReceiverManager = new SPItemEventReceiverHandling(true))
{
     item.Update();
}

Note:
If the version of MOSS is 2007 then use EnableEventFiring and DisableEventFiring methods instead of EventFiringEnabled property.