0

I have sucessfully redirect one of my page to 404 error page using redirection in sitecore. Problem is when i am selecting any country from dropdown list i am getting below error :

"Invalid postback or callback argument. Event validation is enabled using in configuration or <%@ Page EnableEventValidation="true" %> in a page.
For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them.
If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation."

I don't want to compromise with security by setting 'enableEventValidation = false'. By doing so its working fine but i am in need of some alternate solution. And, yes my dropdown binding is inside !postback too.

and even I tried to register ClientScriptManager.RegisterForEventValidation method as suggested in one of blog but it did not worked.

Some code part of 404 page is :

using System;

using Business;

using Sitecore.Data.Items;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Sitecore.Web.UI.WebControls;
using System.Collections.Generic;
using System.Linq;
using Sitecore.Diagnostics;
using System.Web;
using Sitecore.Data.Fields;

/// <summary>
/// title link list control
/// </summary>
public partial class ErrorPage_Link_List : System.Web.UI.UserControl
{
    int counter = 0;

    /// <summary>
    /// Gets the datasource item.
    /// </summary>
    /// <value>
    /// The datasource item.
    /// </value>
    internal Item DatasourceItem
    {
        get { return this.GetDataSourceItem(); }
    }

    /// <summary>
    /// Gets the title.
    /// </summary>
    /// <value>
    /// The title.
    /// </value>
    protected string Title
    {
        get { return DatasourceItem.Name; }
    }

    protected override void Render(System.Web.UI.HtmlTextWriter writer)
    {
        base.Render(writer);

        // Apply Response Headers  
        HttpContext.Current.Response.TrySkipIisCustomErrors = true;
        HttpContext.Current.Response.StatusCode = 404;
        HttpContext.Current.Response.StatusDescription = "404 Not Found";
        HttpContext.Current.Response.End();
    }

    /// <summary>
    /// Handles the Load event of the Page control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
    private void Page_Load(object sender, EventArgs e)
    {
        try
        {
            //gets the items that has the Checkbox "Show In Sitemap" Checked from, current item datasource
            //and innovative aproach and about avanade
            var source = DatasourceItem.Children.Where(c => c["Show in SiteMap"] == "1").ToList();

            Item innovApproach = Sitecore.Context.Database.GetItem(Constants.ItemsID.InnovativeApproach);
            foreach (Item item1 in innovApproach.Children.Where(c => c["Show in SiteMap"] == "1"))
            {
                source.Add(item1);
            }



            source.Add(Sitecore.Context.Database.GetItem(Constants.ItemsID.CustomerResults));

            Item servAndSol = Sitecore.Context.Database.GetItem(Constants.ItemsID.AboutAvanade);
            foreach (Item item in servAndSol.Children.Where(c => c["Show in SiteMap"] == "1"))
            {
                source.Add(item);
            }



            rptSection.DataSource = source;
            rptSection.DataBind();
        }
        catch (Exception ex)
        {
            Log.Error(string.Format("ErrorPage_Link_List.PageLoad() - {0}", ex.Message), ex, ex.TargetSite);
        }
    }

    /// <summary>
    /// Working with sections
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void rptSection_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        try
        {

            //Finding item title, rptLinkItem repeater and the actual item to work with.
            HtmlGenericControl clearDiv = e.Item.FindControl("clearDiv") as HtmlGenericControl;
            HtmlGenericControl itemTitle = e.Item.FindControl("title") as HtmlGenericControl;
            HtmlGenericControl subtitle = e.Item.FindControl("subtitle") as HtmlGenericControl;
            Repeater rptLinkItem = e.Item.FindControl("rptLinkItem") as Repeater;
            Item srcItem = (Item)e.Item.DataItem;
            IEnumerable<Item> dataSource = null;
            if (srcItem.ID.ToString().Equals(Constants.ItemsID.Careers2))
                dataSource = ((Sitecore.Data.Fields.MultilistField)srcItem.Fields[Constants.Fields.SiteMap]).GetItems().ToList();
            else
                dataSource = SitecoreExtensions.GetItemsFromCustomMultiList(srcItem, Constants.Fields.SiteMap).Where(c => c.IsActive()).ToList();
            if (dataSource.Count() > 0)
            {
                if (!string.IsNullOrEmpty(srcItem[Constants.Fields.SectionName]))
                {
                    itemTitle.InnerHtml = srcItem[Constants.Fields.SectionName];
                }
                else
                {
                    itemTitle.InnerHtml = srcItem[Constants.Fields.Title];
                }


                //Bind the rptLinkItem repeater with the item children that are active
                rptLinkItem.DataSource = dataSource;
                rptLinkItem.DataBind();

                counter++;
                if (counter == 4)
                {
                    clearDiv.Attributes.Add("class", "clearBoth");
                    counter = 0;
                }


            }

        }
        catch (Exception ex)
        {
            Log.Error(string.Format("ErrorPage_Link_List.rptSection_ItemDataBound() - {0}", ex.Message), ex, ex.TargetSite);
        }
    }


    /// <summary>
    /// Working with link items
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void rptLinkItem_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        try
        {
            //Finding item title and the actual item to work with.
            HtmlAnchor itemTitle = e.Item.FindControl("linkItem") as HtmlAnchor;
            Item srcItem = (Item)e.Item.DataItem;

            itemTitle.InnerText = srcItem["Title"];



            if (srcItem.HasPresentation())
            {
                //itemTitle.HRef = srcItem.Paths.FullPath;
                itemTitle.HRef = Sitecore.Links.LinkManager.GetItemUrl(srcItem);
            }
            else
            {
                var itemChild = ((Sitecore.Data.Fields.MultilistField)srcItem.Fields["Position override"]).GetItems().First();
                itemTitle.HRef = Sitecore.Links.LinkManager.GetItemUrl(itemChild);
            }
            if (srcItem.ID.ToString().Equals(Business.Constants.ItemsID.SearchJobsPage))
            {

                LinkField url = srcItem.Fields[Business.Constants.Fields.LinkUrlSection];
                itemTitle.HRef = url.Url;
                itemTitle.Target = url.Target;
            }


            if (srcItem.ID.ToString().Equals(Business.Constants.ItemsID.FeaturedCaseStudies))
            {
                //Redirect Featured-Case-Studies to Customer-Results
                Item item = Sitecore.Context.Database.Items.GetItem(Constants.ItemsID.CustomerResults);
                itemTitle.InnerText = itemTitle.Title = srcItem[Constants.Fields.Title];
                itemTitle.HRef = Sitecore.Links.LinkManager.GetItemUrl(item);
            }   /* End Defect no 208947 and */
        }
        catch (Exception ex)
        {
            Log.Error(string.Format("ErrorPage_Link_Lis.rptLinkItem_ItemDataBound() - {0}", ex.Message), ex, ex.TargetSite);
        }
    }

}

}`

2 Answers2

0

It looks like you need to do your databinding within a !IsPostback block rather than every page load.

There are similar scenarios here.

"Invalid postback or callback argument" with Databound controls

Community
  • 1
  • 1
Ian Graham
  • 3,206
  • 1
  • 15
  • 23
  • OP mentions that their real code has databinding inside !IsPostback, but otherwise this would explain it – geedubb May 14 '15 at 17:09
  • Yes, real code for country selector is in countryselector.ascx.cs where i am binding the data inside !IsPostback and which is working fine with every other pages, its just showing above error on 404 page and thats why provided code of 404 page. – Sumit Pandey May 15 '15 at 06:56
  • Stack trace of error is : System.Web.UI.ClientScriptManager.ValidateEvent(String uniqueId, String argument) +190 System.Web.UI.WebControls.DropDownList.LoadPostData(String postDataKey, NameValueCollection postCollection) +80 System.Web.UI.Page.ProcessPostData(NameValueCollection postData, Boolean fBeforeLoad) +574 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2773 – Sumit Pandey May 15 '15 at 06:56
0

I got it on code review of above code, the response headers:

HttpContext.Current.Response.TrySkipIisCustomErrors = true; HttpContext.Current.Response.StatusCode = 404; HttpContext.Current.Response.StatusDescription = "404 Not Found"; HttpContext.Current.Response.End();

were the main issue of this, presence of HttpContext.Current.Response.End(); was being harmful in code and it was never needed for any type of connection in website so I removed the same and problem got resolved. I have understood that any code after Response.End() will not execute. Hope its right to do so. Now my error page is working fine.

Can get more explanation from [Response.End() vs HttpContext.Current.ApplicationInstance.CompleteRequest()

Community
  • 1
  • 1