0

Hope you are well and thanks for helping me.

I have 4 radio buttons and one button, if there is no one radio button checked, the label would show a message in order for end-user to invite selecting a radio button.

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("");

        }
    }
}

It works very well when any radio button is selected, but when there is no one selected, it shows me an error.

I saw the answer that I have to remove it by:

Disable eventvalidation (bad idea, because you lose a little of security that comes with a very little cost).

So, if it is a bad idea, why is it a good answer?

Link: Invalid postback or callback...

How can solve it using eventValidation = "true"?

How come it works for VS2010 (from a video tutorial)?

System.ArgumentException

Invalid postback or callback argument. Event validation is enabled using 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 is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.

Description: HTTP 500.Error processing request.

Details: Non-web exception. Exception origin (name of application or object): System.Web.

Exception stack trace:

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 
  • Why not just use `if` statements instead of this ugly nested ternary garbage? – maccettura Sep 12 '18 at 17:08
  • You have any other code on the code behind which is creating controls runtime and adding to the page? Or JavaScript code which is doing the same? – Chetan Sep 13 '18 at 00:17

0 Answers0