Surendra Sharma

Surendra Sharma

Search This Blog

Tuesday, May 5, 2015

Tab order is changing on partial postback

I have three dropdown list on my page for country, state, city, area and society. All these 5 dropdown list are in update panel so that I can use partial rendering features of AJAX. Its working fine. All 5 dropdown list are working great and I was feeling proud that I implemented it.

But fuck to my tester who find out issue related with these dropdown list as well. Whenever he select state, my tab order is changed and instead of state itself, my default focus set to next control after society. However in any situation it should focus on current dropdown list.

I tried to solve it though javascript and JQuery but none of them work.

To solve this just write below code in current postback event as

        protected void ddlState_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList ddlList = (DropDownList)sender;
            ScriptManager.GetCurrent(Page).SetFocus(ddlList);
        }



Please leave your comments or share this code if it’s useful for you.

Page title is changing on partial postback

I have three dropdown list on my page for country, state and city. All these 3 dropdown list are in update panel so that I can use partial rendering features of AJAX. Its working fine. All 3 dropdown list are working great and I was feeling proud that I implemented it.

But fuck to my tester who find out issue related with these dropdown list as well. He has eagle eyes. Whenever he selected any of these 3 dropdown list, my webpage title changed to current URL of the page.

I debugged my code from start to end but unable to find out the reason? Why?

But fortunately its work around is just to set the page title again in the end of page load event as below.

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                    //Some code here;
            }
            else
            {
                    Page.Title = "This is page title";
            }
}

Let me know if you have any better solution or particular reason of this behavior.

Please leave your comments or share this code if it’s useful for you.

Thursday, April 30, 2015

How to add HTML page in Sitecore?

My client requested to add HTML page in Sitecore project and link it to one of the item in footer. I asked to myself WTF, what kind of requirement is this? How to do this?

But finally I completed this. Here are the quick steps.

1. Add HTML page in any folder of your .NET project. Let’s say it is "/HTMLPages/faq.htm"
2. Add complete path of this HTML page in “IgnoreUrlPrefixes“ in web.config file so that Sitecore should not process this page request and ignore it. Let’s ASP.NET should serve this page request without using Sitecore.

<setting name="IgnoreUrlPrefixes" value="/sitecore/default.aspx|/trace.axd|/webresource.axd|/sitecore/shell/Controls/Rich Text Editor/Telerik.Web.UI.DialogHandler.aspx|/sitecore/shell/applications/content manager/telerik.web.ui.dialoghandler.aspx|/sitecore/shell/Controls/Rich Text Editor/Telerik.Web.UI.SpellCheckHandler.axd|/Telerik.Web.UI.WebResource.axd|/sitecore/admin/upgrade/|/layouts/testing|/HTMLPages/faq.htm"/>

3.    Specify this page in General Link field of Sitecore item. But the problem is it’s neither the internal link nor the external link. So how to provide the link so that it should take protocol and domain name automatically.

Here I use the Insert JavaScript option of General Link field as

javascript:window.open(window.location.protocol + "//" + window.location.host   + '/HTMLPages/faq.htm');

Here is screenshot of the General link field setting



Now when it renders, the item open this page in new window and take this page as a part of current website.

Sometimes client requirement plays a major role to improve our skills.

Please leave your comments or share this trick if it’s useful for you.

Friday, April 24, 2015

How to create Page Not Found in Sitecore

In every Sitecore application, if end user type some page name that is not exist then we need to redirect user to some custom page which inform user that requested page is not available.

Here are the steps for it.

·         Create template, layout and content item for Page not found.
·         Write below code in your project

namespace MyProject.CodeFiles
{
    /// <summary>
    /// 404 class
    /// </summary>
    public class Page404Resolver : Sitecore.Pipelines.HttpRequest.HttpRequestProcessor
    {
        public override void Process(Sitecore.Pipelines.HttpRequest.HttpRequestArgs args)
        {
            if (Sitecore.Context.Item != null || Sitecore.Context.Site == null || Sitecore.Context.Database == null)
            {
                return;
            }

            // If current item not available in Sitecore, then
            if (Sitecore.Context.Item == null)
            {
                Sitecore.Diagnostics.Log.Info("Page404Resolver=" + System.Web.HttpContext.Current.Request.Url, this);

                // Find an error-page item and set it to context Item
                Item item404 = args.GetItem(new ID("{XXXXX1D2-8CDB-4461-93CE-501905DDSSSS}"));
                if (item404 != null)
                {
                    Sitecore.Context.Item = item404;
                    Sitecore.Context.Items["is404"] = "true";
                    Sitecore.Diagnostics.Log.Info("CUSTOM ERROR HANDLER: 404 :  Item is not null - Setting Context item to 404 page", this);
                }
            }
        }
    }
}


·         Open web.config file and add this class just below ItemResolver line of pipeline section

<processor type="Sitecore.Pipelines.HttpRequest.ItemResolver, Sitecore.Kernel"/>
<processor type="MyProject.Web.Present.CodeFiles.Page404Resolver, MyProject.Web.UI"/>

·         Implement below code in page load section of File Not Found page. Keep response status as 404 and also set status of response.

    public partial class NotFound : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
                if (Sitecore.Context.Items["is404"].Equals("true"))
                {
                    try
                    {
                        Response.StatusCode = 404;
                        Response.TrySkipIisCustomErrors = true;
                        Response.StatusDescription = "File not found";
                        //Response.End();
                    }
                    catch (Exception ex)
                    {
                        // Log error
                    }
                }
            }
        }

Please leave your comments or share this code if it’s useful for you.