ASP.Net CascadingDropDown and EnableEventValidation="false"
Asked Answered
W

1

3

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);
        }
Wife answered 31/1, 2012 at 22:43 Comment(7)
What kind of text do you have in that dropdown? Give me a few examples.Surinam
The first ddl has the series names of Epson printers ie: Stylus, Stylus Colour, Stylus Photo. And the second ddl has the corresponding model numbers: BX535WD, DX4000, DX7400 etx. There's 8 series and there's 229 printers in total. 'Stylus Colour' is the longest.Wife
With EnableEventValidation = "true" I get the following JScript Runtime error: Sys.WebForms.PageRequestManagerServerErrorException: Invalid postback or callback argument. Event validation is enabled using <pages enableEventValidation="true"/> 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... blah blah blah!Wife
@ Hari I should also point out that the select values are integers - printerIDsWife
@Wife - have you tried using regular dropdown lists inside a lightweight update panel? If your page is optimized, a few extra async postbacks won't hurt anything and will probably fix the issue. Of course, this wouldn't be a good option if your page lifecycle is expensive.Winery
@ Tim Medora Thanks for the suggestion. But the point is why provide a cascadedropdown in the AJAX toolkit that you can't do anything with? Surely you must be allowed to postback?!Wife
I've just read this on [asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/Walkthrough/… Finally, in order for the values to be submitted, EventValidation needs to be disabled for the page. EventValidation ensures that the values in each control match the values that were present when the page was rendered, but since these drop downs are populating on the client side, this is never true. We’re hoping to find a way to resolve this issue. That's pants!!Wife
W
2

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...

Wife answered 21/2, 2012 at 12:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.