ASP.Net CascadingDropDown 和 EnableEventValidation=“false”

发布于 2024-12-29 21:59:12 字数 1909 浏览 0 评论 0 原文

我刚刚从 AJAX 工具包中获得了 CascadingDropDown 与 SelectedIndexChanged 一起使用,以重定向到传递所选值的查询字符串的页面。我好高兴啊!

但是,我只能通过向页面添加 EnableEventValidation="false" 来使 SelectedIndexChanged 事件正常工作。问题是 CascadingDropDown 将作为产品选择器放置在我网站的 MasterPage 中。

我不热衷于将 EnableEventValidation="false" 添加到我的 MasterPage!我查看了 MSDN 上的 ClientScriptManager.RegisterForEventValidation 方法,它完全超出了我的理解范围。

最好的做法是什么?有使用 ClientScriptManager.RegisterForEventValidation 的简单示例吗?

干杯...

编辑:这是代码:

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

这是事件:

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

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);
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

邮友 2025-01-05 21:59:12

这个令人头疼的问题的答案是自定义下拉控件!

因此,为了结束这个问题并希望帮助其他人解决这个问题,我所做的就是:

我使用以下代码创建了一个名为 NoValidationDropDownList.cs 的 cs 文件

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
    {
    }
}

然后在下拉控件所在的 aspx 页面上(在我的例子中是 MasterPage )我放置了这个:

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

接下来我修改了级联下拉框,如下所示:

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

据我了解,创建自定义下拉控件可以规避事件验证。这样您就不需要关闭整个页面的事件验证。就我而言,由于控件位于 MasterPage 中,整个站点的事件验证将被关闭!

唉,这不是我的原创作品,所以这里是原始参考: http://johanleino.wordpress.com/2009/11/17/cascadingdropdown-casues-invalid-postback-or-callback-argument-error/

谢谢约翰!

希望这有帮助...

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文