ASP.Net CascadingDropDown and EnableEventValidation="false"

4.7k views Asked by At

I have just got a CascadingDropDown from the AJAX Toolkit working with SelectedIndexChanged to redirect to a page passing a querystring of the selected value. I'm well chuffed!

However, I only got the SelectedIndexChanged event working by adding EnableEventValidation="false" to the page. The problem is the CascadingDropDown will be placed in the MasterPage of my website as a product selector.

I'm not keen on adding EnableEventValidation="false" to my MasterPage! I've looked at the ClientScriptManager.RegisterForEventValidation method on MSDN and it's gone right over my head.

What's the best thing to do? Is there a simple example of using ClientScriptManager.RegisterForEventValidation?

Cheers...

EDIT: Here's the code:

<asp:ScriptManager ID="asm" runat="server" />
<div>
    Series:     <asp:DropDownList ID="SeriesList" runat="server" /><br />
    Printers:   <asp:DropDownList ID="PrinterList" runat="server" 
                 onselectedindexchanged="PrinterList_SelectedIndexChanged"
             AutoPostBack="True" /><br />
</div>

    <asp:CascadingDropDown ID="ccd1" runat="server"
        ServicePath="CascadingDropdown1.cs.asmx" ServiceMethod="GetSeries" 
        TargetControlID="SeriesList" Category="Series"
        PromptText="Select Series" />
    <asp:CascadingDropDown ID="ccd2" runat="server"
        ServicePath="CascadingDropdown1.cs.asmx" ServiceMethod="GetPrintersForSeries"
        TargetControlID="PrinterList" ParentControlID="SeriesList" Category="Printer" 
        PromptText="Select Printer" />

    <asp:UpdatePanel ID="UpdatePanel1" runat="server">

        <Triggers>
            <asp:AsyncPostBackTrigger ControlID="PrinterList" EventName="SelectedIndexChanged" />
        </Triggers>
    </asp:UpdatePanel>

And here's the event:

protected void PrinterList_SelectedIndexChanged(object sender, EventArgs e)
        {
            int printerID = Convert.ToInt32(PrinterList.SelectedValue);
            System.Web.HttpContext.Current.Response.Redirect("Default.aspx?PID="+printerID);
        }
1

There are 1 answers

0
ComfortablyNumb On

The answer to this pain in the neck problem is custom dropdown controls!

So to close off this question and hopefully help someone else get round this issue here is what I did:

I created a cs file called NoValidationDropDownList.cs with the following code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;

namespace My.Namespace.Controls
{
    public class DdlNoEventValidation : DropDownList
    {
    }
}

Then on the aspx page where the dropdown controls reside (in my case a MasterPage) I placed this:

<%@ Register TagPrefix="asp" Namespace="My.Namespace.Controls" %>

Next I amended the cascade dropdown boxes like so:

<p><asp:DdlNoEventValidation ID="DD1" runat="server" /></p>
<p><asp:DdlNoEventValidation ID="DD2" runat="server" 
   onselectedindexchanged="My_SelectedIndexChanged"
   AutoPostBack="True"
   /></p>

As I understand it, creating a custom dropdown control circumvents event validation. In this way you don't need to switch off event validation for the entire page. In my case as the controls are sitting in the MasterPage, event validation would have been switched off for the entire site!

Alas this is not my original work so here is the original reference: http://johanleino.wordpress.com/2009/11/17/cascadingdropdown-casues-invalid-postback-or-callback-argument-error/

Thanks Johan!

Hope this helps...