首页 > 解决方案 > 为什么我的单选按钮 asp 代码得到“无效的回发或回调参数”?

问题描述

即使我添加了与复选框控件一起使用的步骤,为什么我的单选按钮 asp 代码仍因“无效的回发或回调参数”而失败?

在这里的一个相关问题中,我最终采用了一种三重方法来解决我的代码隐藏没有被调用的问题。步骤如下:

  1. 通过将此 (AutoPostBack="true") 添加到复选框中,将已接受的答案纳入其中,如下所示:

    <asp:checkbox id="ckbxAllGenres" runat="server" Checked="True" class="genres" OnCheckedChanged="ckbxAllGenres_CheckedChanged" AutoPostBack="true" /> 所有

  2. 将 EnableEventValidation="true" 添加到页面的 to 中,使其变为:

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" EnableEventValidation="true" Inherits="Flix4Fams_WebForms.WebForm1" %>

  3. 覆盖代码隐藏文件中的 Render 方法,如此处所述:

protected override void Render(HtmlTextWriter writer) { // 为事件验证注册控件 foreach (Control c in this.Controls) { this.Page.ClientScript.RegisterForEventValidation( c.UniqueID.ToString()); } base.Render(作家);}

如上所示,这与 Checkbox 控件完美配合,但现在我已经使用单选按钮,在进行这些更改之前,我得到了与之前使用复选框类似的异常,即:

[ArgumentException:回发或回调参数无效。使用配置或页面中的 <%@ Page EnableEventValidation="true" %> 启用事件验证。出于安全目的,此功能验证回发或回调事件的参数是否源自最初呈现它们的服务器控件。如果数据有效且符合预期,请使用 ClientScriptManager.RegisterForEventValidation 方法注册回发或回调数据以进行验证。]
System.Web.UI.ClientScriptManager.ValidateEvent(String uniqueId, String argument) +9830282
System.Web.UI .Control.ValidateEvent(String uniqueID, String eventArgument)
+114 System.Web.UI.WebControls.RadioButton.LoadPostData(String postDataKey, NameValueCollection postCollection) +77
System.Web.UI.WebControls.RadioButton.System.Web.UI.IPostBackDataHandler.LoadPostData(字符串 postDataKey,NameValueCollection postCollection)+15
System.Web.UI.Page.ProcessPostData(NameValueCollection postData,布尔 fBeforeLoad)+471
System.Web。 UI.Page.ProcessRequestMain(布尔 includeStagesBeforeAsyncPoint,布尔 includeStagesAfterAsyncPoint)+1897

在出现此错误之前(当它工作时),单选按钮代码是这样的:

<input type="radio" id="rdbtnAllIMDB" />
<label for="rdbtnAllIMDBA" class="marginR20">All</label>
<input type="radio" id="rdbtnGOE" checked="checked" />
<label for="rdbtnGOE">>= </label>
<input type="number" step="0.1" value="7.5" />

现在是这样,具有类似类型的变形:

<asp:radiobutton name="imdb" id="rdbtnAllIMDB" runat="server" AutoPostBack="true" />
<label for="rdbtnAllIMDBA" class="marginR20">All</label>
<asp:radiobutton name="imdb" id="rdbtnGOE" Checked="True" runat="server" AutoPostBack="true" />
<label for="rdbtnGOE">>= </label>
<asp:textBox runat="server" type="number" AutoPostBack="true" step="0.1" value="7.5" />

为什么我所做的更改使复选框起作用,但它不适用于单选按钮?我还需要做什么才能让它工作?

标签: asp.netradio-buttonrenderinghtml-rendering

解决方案


我在某处读到,要使 RadioButtons 互斥,它们必须具有相同的 Name 值(但不同的 ID 值)。

显然这个人在谈论 GroupName 属性(这确实有道理),因为当我将代码更改为以下代码时它起作用了:

<asp:RadioButton GroupName="imdbGroup" id="rdbtnAllIMDB" runat="server" />
<label for="rdbtnAllIMDBA" class="marginR20">All</label>
<asp:RadioButton GroupName="imdbGroup" id="rdbtnGOE" Checked="True" runat="server" />

推荐阅读