Invalid asp.net Radiobutton server side enabled on client side

I am having a strange problem with the state of radiobutton with asp.net enabled.

Code on the .aspx page:

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb" Inherits="testradioButton.WebForm1" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <script type="text/javascript" src="https://code.jquery.com/jquery-1.11.1.min.js"></script> <script type="text/javascript"> $(document).ready(function () { $('#radYes').change(function () { gererEtat(); }); $('#radNo').change(function () { gererEtat(); }); }); function gererEtat() { $('#radDisabledYes').prop('disabled', !$('#radYes').prop('checked')); $('#radDisabledNo').prop('disabled', !$('#radYes').prop('checked')); } </script> </head> <body> <form id="form1" runat="server"> <div> <asp:radiobutton ID="radYes" runat="server" GroupName="test" Text="yes"></asp:radiobutton> <asp:radiobutton ID="radNo" runat="server" GroupName="test" Text="No"></asp:radiobutton> </div> <div> <asp:radiobutton ID="radDisabledYes" runat="server" GroupName="test2" Text="yes"></asp:radiobutton> <asp:radiobutton ID="radDisabledNo" runat="server" GroupName="test2" Text="No"></asp:radiobutton> </div> <asp:LinkButton ID="lnktoto" runat="server" Text="Submit"></asp:LinkButton> </form> </body> </html> 

And the code behind:

 Public Class WebForm1 Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Not IsPostBack Then radDisabledYes.Enabled = radYes.Checked radDisabledNo.Enabled = radYes.Checked End If Stop End Sub Private Sub Radiobutton1_CheckedChanged(sender As Object, e As EventArgs) Handles radYes.CheckedChanged, radNo.CheckedChanged radDisabledYes.Enabled = radYes.Checked radDisabledNo.Enabled = radYes.Checked End Sub Private Sub lnktoto_Click(sender As Object, e As EventArgs) Handles lnktoto.Click Stop End Sub End Class 

On the server side, disable the switches on first boot. I have the same conditions in javascript. Therefore, on the client side, if the user clicks Yes, I turn on some controls. I am doing the same thing on the server side. It works great with all types of controls except Radio Button.

Say that when I boot, I turn off the switches. On the client side, based on user input, I include them (in javascript). The user set the switch and sent the page. On the server side, the switch is disabled and not installed. I have a code that checks the conditions and activates it. But the switch is not yet set. I understand why the server ignores the checked state, because at first it considers that it is disconnected. Is there any way to make this work? I know that the server receives the checked state, because when I check Request.Form, I see radDisable = Yes.

Thank you for your help.

+6
source share
6 answers

Here is your hack you are looking for :)

 Public Class WebForm1 Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If radYes.Checked Then radDisabledYes.InputAttributes.Remove("disabled") radDisabledNo.InputAttributes.Remove("disabled") Else radDisabledYes.InputAttributes("disabled") = "disabled" radDisabledNo.InputAttributes("disabled") = "disabled" End If Stop End Sub Private Sub Radiobutton1_CheckedChanged(sender As Object, e As EventArgs) Handles radYes.CheckedChanged, radNo.CheckedChanged ' not really needed anymore, but lets leave it in here for fun! End Sub Private Sub lnktoto_Click(sender As Object, e As EventArgs) Handles lnktoto.Click Stop End Sub End Class 

This substantially eliminates the .NET security check for disabled controls. The controls are still enabled / disabled appropriately, but you do this without going into the WebControl.Enabled property.

+1
source
 $(document).ready(function () { $('#<%=radYes.ClientID%>').change(function () { gererEtat(); }); $('#<%=radNo.ClientID%>').change(function () { gererEtat(); }); function gererEtat() { $('#<%=radDisabledYes.ClientID%>').prop('disabled', !$('#<%= radYes.ClientID%>').prop('checked')); $('#<%=radDisabledNo.ClientID%>').prop('disabled', !$('#<%=radYes.ClientID%>').prop('checked')); } }); 

Use "ClientID" to select your control in jquery code, because control is server management.

+1
source

ASP.NET can be really paranoid (and rightfully) about sending back initially disabled controls. Therefore, instead of actually turning them off, they imitate the effect. For instance. set attribute onfocus = "this.blur()" - this will prevent the button from being clicked. And when you need to enable the button - remove the attribute.

0
source

It seems to me that the behavior of your code is acceptable until the server can detect the state changes of the switches that it assumes are disabled.

Why not just keep the hidden and disabled radio buttons in the background? When you want to turn off the look, display this set and hide the real ones using JavaScript. Then you never run into a problem with disabled forms going to the server.

Edit: Having reviewed your question again, I wonder about the need to disable something on the server side to get started. There may be a security risk that I don’t know about. An alternative choice would be to leave the server name on and disable javascript when loading the document.

0
source

I have one solution, this is a little tricky, but it can help you

 <script type="text/javascript"> $(document).ready(function () { $('#radYes').change(function () { gererEtat(); }); $('#radNo').change(function () { gererEtat(); }); // restore radio button disable status on document ready var status = $("#txtradioStatus").val() == "true" ? true : false; $('#radDisabledYes').prop('disabled', !status); $('#radDisabledNo').prop('disabled', !status); }); function gererEtat() { $('#radDisabledYes').prop('disabled', !$('#radYes').prop('checked')); $('#radDisabledNo').prop('disabled', !$('#radYes').prop('checked')); // store radio button status in hidden field $("#txtradioStatus").val($('#radYes').prop('checked')); } </script> <div> <asp:radiobutton ID="radYes" runat="server" GroupName="test" Text="yes"></asp:radiobutton> <asp:radiobutton ID="radNo" runat="server" GroupName="test" Text="No"></asp:radiobutton> </div> <div> <asp:radiobutton ID="radDisabledYes" runat="server" GroupName="test2" Text="yes"></asp:radiobutton> <%-- take hidden field that you can keep status of radio button after postback--%> <input type="hidden" runat="server" value="true" id="txtradioStatus" /> <asp:radiobutton ID="radDisabledNo" runat="server" GroupName="test2" Text="No"></asp:radiobutton> </div> <asp:LinkButton ID="lnktoto" runat="server" Text="Submit"></asp:LinkButton> 
0
source

submitdisabledcontrols = "true" , this will solve your problem. Follow these steps.

The reason is that you turned off server-side control, even if you turned it on using a client using jQuery, SERVER DOES NOT KNOW ABOUT IT. Therefore, it retains the same value as it does not expect to change the value of the disabled control.

There are two things.

First remove the page load code. (The Radiobutton1_CheckedChanged event is also not required)

 Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load 'If Not IsPostBack Then ' radDisabledYes.Enabled = radYes.Checked ' radDisabledNo.Enabled = radYes.Checked 'End If End Sub 

Then change the client-side code to

  <script type="text/javascript"> $(document).ready(function () { $('#radYes').change(function () { gererEtat(!$('#radYes').prop('checked')); }); $('#radNo').change(function () { gererEtat(!$('#radYes').prop('checked')); }); if (!$('#radYes').prop('checked')) { gererEtat(true); } }); function gererEtat(isEnable) { $('#radDisabledYes').prop('disabled', isEnable); $('#radDisabledNo').prop('disabled', isEnable); } </script> 

Finally, in the form element, add the attribute submitdisabledcontrols = "true".

 <form id="form1" runat="server" submitdisabledcontrols="true" > 

run and test the code in which it will work.

0
source

Source: https://habr.com/ru/post/971387/


All Articles