希望您一切都好,谢谢您的帮助。
我有4个单选按钮和一个按钮,如果未选中任何一个单选按钮,标签将显示一条消息,以便最终用户邀请选择一个单选按钮。
Default.aspx:
<%@ Page Language="C#" Inherits="Challenge12Proj.Default" %>
<!DOCTYPE html>
<html>
<head runat="server">
<title>Default</title>
</head>
<body>
<form id="form1" runat="server">
<h3>Your note taking preferences</h3>
<p><asp:RadioButton id="rdbPencil" Text="Pencil" runat="server" GroupName="NoteGroup" /></p>
<p><asp:RadioButton id="rdbPen" Text="Pen" runat="server" GroupName="NoteGroup" /></p>
<p><asp:RadioButton id="rdbPhone" Text="Phone" runat="server" GroupName="NoteGroup" /></p>
<p><asp:RadioButton id="rdbTablet" Text="Tablet" runat="server" GroupName="NoteGroup" /></p>
<p><asp:Button id="btnOk" runat="server" Text="Ok" OnClick="btnOkClicked" /></p>
<br/>
<p><asp:Image id="imgResult" runat="server" ImageUrl="" /></p>
<p><asp:Label id="lblResult" runat="server" Text=""/></p>
</form>
</body>
</html>
Default.aspx.cs:
using System;
using System.Web;
using System.Web.UI;
namespace Challenge12Proj
{
public partial class Default : System.Web.UI.Page
{
public void btnOkClicked(object sender, EventArgs args)
{
lblResult.Text = (rdbPen.Checked) ? "You selected Pen"
: (rdbPencil.Checked) ? "You selected Pencil"
: (rdbPhone.Checked) ? "You selected Phone"
: (rdbTablet.Checked) ? "You selected Tablet" : "Please select an option";
imgResult.ImageUrl = (rdbPen.Checked) ? "/images/pen_PNG.png"
: (rdbPencil.Checked) ? "/images/pencil_PNG.png"
: (rdbPhone.Checked) ? "/images/smartphone_PNG.png"
: (rdbTablet.Checked) ? "/images/tablet_PNG.png" : "";
imgResult.Width = (rdbPen.Checked) ? new System.Web.UI.WebControls.Unit("450px")
: (rdbPencil.Checked) ? new System.Web.UI.WebControls.Unit("250px")
: (rdbPhone.Checked) ? new System.Web.UI.WebControls.Unit("250px")
: (rdbTablet.Checked) ? new System.Web.UI.WebControls.Unit("300px")
: new System.Web.UI.WebControls.Unit("");
imgResult.Height = (rdbPen.Checked) ? new System.Web.UI.WebControls.Unit("150px")
: (rdbPencil.Checked) ? new System.Web.UI.WebControls.Unit("200px")
: (rdbPhone.Checked) ? new System.Web.UI.WebControls.Unit("350px")
: (rdbTablet.Checked) ? new System.Web.UI.WebControls.Unit("250px")
: new System.Web.UI.WebControls.Unit("");
}
}
}
当选择任何单选按钮时,它的效果很好,但是当没有选择任何单选按钮时,它会向我显示错误。
我看到了必须通过以下方式删除它的答案:
禁用事件验证(不好的主意,因为您损失了很少的安全性,而付出的代价却很小)。
那么,如果这是一个坏主意,为什么它是一个好答案?
链接:Invalid postback or callback...
如何使用eventValidation =“ true”解决该问题?
它如何用于VS2010(来自视频教程)?
System.ArgumentException
无效的回发或回调参数。使用配置或页面中的<%@页面EnableEventValidation =“ true”%>启用事件验证。为了安全起见,此功能验证回发或回调事件的参数源自最初呈现它们的服务器控件。如果数据有效且预期,请使用ClientScriptManager.RegisterForEventValidation方法来注册回发或回调数据以进行验证。
说明: HTTP500。错误处理请求。
详细信息:非网络异常。异常来源(应用程序或对象的名称):System.Web。
异常堆栈跟踪:
at System.Web.UI.ClientScriptManager.ValidateEvent (System.String uniqueId, System.String argument) [0x00060] in /Users/builder/jenkins/workspace/build-package-osx-mono/2018-02/external/bockbuild/builds/mono-x64/mcs/class/System.Web/System.Web.UI/ClientScriptManager.cs:521
at System.Web.UI.Control.ValidateEvent (System.String uniqueId, System.String argument) [0x00012] in /Users/builder/jenkins/workspace/build-package-osx-mono/2018-02/external/bockbuild/builds/mono-x64/mcs/class/System.Web/System.Web.UI/Control.cs:2029
at System.Web.UI.WebControls.RadioButton.LoadPostData (System.String postDataKey, System.Collections.Specialized.NameValueCollection postCollection) [0x0001a] in /Users/builder/jenkins/workspace/build-package-osx-mono/2018-02/external/bockbuild/builds/mono-x64/mcs/class/System.Web/System.Web.UI.WebControls/RadioButton.cs:118
at System.Web.UI.WebControls.RadioButton.System.Web.UI.IPostBackDataHandler.LoadPostData (System.String postDataKey, System.Collections.Specialized.NameValueCollection postCollection) [0x00000] in /Users/builder/jenkins/workspace/build-package-osx-mono/2018-02/external/bockbuild/builds/mono-x64/mcs/class/System.Web/System.Web.UI.WebControls/RadioButton.cs:135
at System.Web.UI.Page.ProcessPostData (System.Collections.Specialized.NameValueCollection data, System.Boolean second) [0x001c3] in /Users/builder/jenkins/workspace/build-package-osx-mono/2018-02/external/bockbuild/builds/mono-x64/mcs/class/System.Web/System.Web.UI/Page.cs:1153
at System.Web.UI.Page.ProcessPostData () [0x00025] in /Users/builder/jenkins/workspace/build-package-osx-mono/2018-02/external/bockbuild/builds/mono-x64/mcs/class/System.Web/System.Web.UI/Page.cs:1360
at System.Web.UI.Page.InternalProcessRequest () [0x001a1] in /Users/builder/jenkins/workspace/build-package-osx-mono/2018-02/external/bockbuild/builds/mono-x64/mcs/class/System.Web/System.Web.UI/Page.cs:1337
at System.Web.UI.Page.ProcessRequest (System.Web.HttpContext context) [0x0005f] in /Users/builder/jenkins/workspace/build-package-osx-mono/2018-02/external/bockbuild/builds/mono-x64/mcs/class/System.Web/System.Web.UI/Page.cs:1190